user.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { defineStore } from 'pinia'
  2. import { setToken, removeToken, setRefreshToken, getToken } from '@/utils/auth'
  3. import {
  4. smsLogin,
  5. passwordLogin,
  6. getBaseInfo,
  7. switchLoginOfEnterprise,
  8. getEnterprisingUserInfo,
  9. logoutToken,
  10. logout
  11. } from '@/api/common'
  12. import { getUserInfo } from '@/api/personal/user'
  13. import { getEnterpriseUserAccount, getAccountBalance, getUserAccount, userRegister } from '@/api/common'
  14. import { getEnterpriseBaseInfo } from '@/api/enterprise'
  15. import Snackbar from '@/plugins/snackbar'
  16. import { timesTampChange } from '@/utils/date'
  17. import { updateEventList } from '@/utils/eventList'
  18. import { getBaseInfoDictOfName } from '@/utils/getText'
  19. // import Confirm from '@/plugins/confirm'
  20. // import { useIMStore } from './im'
  21. // const useIM = useIMStore()
  22. export const useUserStore = defineStore('user',
  23. {
  24. state: () => ({
  25. token: getToken(),
  26. accountInfo: localStorage.getItem('accountInfo') ? JSON.parse(localStorage.getItem('accountInfo')) : {}, // 登录返回的信息
  27. userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {}, // 当前登录账号信息
  28. baseInfo: localStorage.getItem('baseInfo') ? JSON.parse(localStorage.getItem('baseInfo')) : {}, // 人才信息
  29. entBaseInfo: localStorage.getItem('entBaseInfo') ? JSON.parse(localStorage.getItem('entBaseInfo')) : {}, // 企业个人信息
  30. userAccount: {}, // 用户账户信息
  31. enterpriseUserAccount: {} // 企业账户信息
  32. }),
  33. actions: {
  34. // 个人用户注册并登录
  35. handleUserRegister (data) {
  36. return new Promise((resolve, reject) => {
  37. userRegister(data).then(async res => {
  38. setToken(res.accessToken)
  39. setRefreshToken(res.refreshToken)
  40. this.accountInfo = res
  41. localStorage.setItem('accountInfo', JSON.stringify(res))
  42. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  43. updateEventList(true) // 获取规则配置跟踪列表
  44. await this.getUserInfos()
  45. this.getUserBaseInfos()
  46. resolve()
  47. }).catch(err => { reject(err) })
  48. })
  49. },
  50. // 短信登录
  51. handleSmsLogin (data) {
  52. return new Promise((resolve, reject) => {
  53. smsLogin(data).then(async res => {
  54. this.token = res.accessToken
  55. setToken(res.accessToken)
  56. setRefreshToken(res.refreshToken)
  57. this.accountInfo = res
  58. localStorage.setItem('accountInfo', JSON.stringify(res))
  59. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  60. updateEventList(true) // 获取规则配置跟踪列表
  61. await this.getUserInfos()
  62. this.getUserBaseInfos()
  63. resolve(res)
  64. }).catch(err => { reject(err) })
  65. })
  66. },
  67. // 密码登录
  68. async handlePasswordLogin(data) {
  69. return new Promise((resolve, reject) => {
  70. data.account = data.phone
  71. passwordLogin(data).then(res => {
  72. if (data.isEnterprise) { // 企业邮箱登录
  73. localStorage.setItem('emailLoginInfo', JSON.stringify(res))
  74. window.location.href = '/enterpriseVerification'
  75. } else {
  76. // 个人手机号登录
  77. setToken(res.accessToken)
  78. setRefreshToken(res.refreshToken)
  79. this.accountInfo = res
  80. localStorage.setItem('accountInfo', JSON.stringify(res))
  81. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  82. updateEventList(true) // 获取规则配置跟踪列表
  83. this.getUserInfos()
  84. this.getUserBaseInfos()
  85. }
  86. resolve()
  87. }).catch(err => {
  88. reject(err)
  89. })
  90. })
  91. },
  92. // 获取当前登录账户信息
  93. async getUserInfos () {
  94. try {
  95. // const api = getIsEnterprise() ? getEnterprisingUserInfo : getUserInfo
  96. const data = await getUserInfo({ id: this.accountInfo.userId })
  97. this.userInfo = data
  98. localStorage.setItem('userInfo', JSON.stringify(data))
  99. this.getUserAccountInfo()
  100. } catch (error) {
  101. Snackbar.error(error.msg)
  102. }
  103. },
  104. // 获取当前登录账户的基本信息(人才信息)
  105. async getUserBaseInfos (userId = null) {
  106. try {
  107. // const api = getIsEnterprise() ? null : getBaseInfo
  108. const data = await getBaseInfo({ userId: userId || this.accountInfo.userId })
  109. if (!data) return localStorage.setItem('baseInfo', '{}')
  110. this.baseInfo = await this.getFieldText(data)
  111. localStorage.setItem('baseInfo', JSON.stringify(this.baseInfo))
  112. } catch (error) {
  113. Snackbar.error(error)
  114. }
  115. },
  116. // 字典对应中文
  117. async getFieldText (data) {
  118. if (data.birthday && data.birthday !== 0) data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  119. if (data.firstWorkTime && data.firstWorkTime !== 0) data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  120. if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  121. if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  122. if (data.eduType && data.eduType !== 0) await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  123. if (data.expType && data.expType !== 0) await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  124. if (data.sex && data.sex !== 0) await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  125. if (data.jobType && data.jobType !== 0) await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  126. if (data.jobStatus && data.jobStatus !== 0) await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  127. if (data.maritalStatus && data.maritalStatus !== 0) await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  128. return data
  129. },
  130. // 退出登录
  131. async userLogout (type) {
  132. // type: 1求职端 2招聘端
  133. if (type === 1) {
  134. await logout()
  135. } else await logoutToken(getToken(1))
  136. this.handleClearStorage()
  137. },
  138. // 清除缓存
  139. handleClearStorage () {
  140. removeToken()
  141. this.token = ''
  142. this.userInfo = {}
  143. this.baseInfo = {}
  144. this.accountInfo = {}
  145. localStorage.clear()
  146. },
  147. // 切换为招聘者
  148. async changeRole (res) {
  149. let data
  150. if (res?.type === 'emailLogin') {
  151. data = res
  152. } else {
  153. // 先退出个人登录
  154. // await logout()
  155. const enterpriseId = localStorage.getItem('enterpriseId') || ''
  156. if (!enterpriseId) return Snackbar.error('切换失败,请重新登录!')
  157. data = await switchLoginOfEnterprise({ enterpriseId })
  158. }
  159. setToken(data.accessToken, 1) // 个人切换企业->存放企业token
  160. setRefreshToken(data.refreshToken, 1) // 个人切换企业->存放企业refreshToken
  161. localStorage.setItem('accountInfo', JSON.stringify(data))
  162. localStorage.setItem('expiresTime', data.expiresTime)
  163. updateEventList(false)
  164. await this.getEnterpriseInfo()
  165. await this.getEnterpriseUserAccountInfo()
  166. Snackbar.success(res?.type === 'emailLogin' ? '登录成功' : '切换成功')
  167. // 人才推荐不需要跳转
  168. if (!res.noJump) {
  169. setTimeout(() => { window.location.href = '/enterprise' }, 1000)
  170. }
  171. },
  172. // 获取当前登录的企业用户信息
  173. async getEnterpriseInfo (check) {
  174. const result = await getEnterprisingUserInfo()
  175. this.entBaseInfo = result
  176. // 是否为企业账号管理员
  177. const isAdmin = result.userType === '1'
  178. localStorage.setItem('isAdmin', isAdmin)
  179. if (isAdmin && !check) await this.checkEnterpriseBaseInfo() // 校验企业必填信息
  180. localStorage.setItem('entBaseInfo', JSON.stringify(result))
  181. },
  182. // 获取企业账户信息
  183. async getEnterpriseUserAccountInfo () {
  184. const data = await getEnterpriseUserAccount()
  185. if (!data) return
  186. this.enterpriseUserAccount = data
  187. // this.getUserAccountBalance()
  188. localStorage.setItem('enterpriseUserAccount', JSON.stringify(data))
  189. return data // 方便直接获取
  190. },
  191. // 获取《企业基本信息》
  192. async checkEnterpriseBaseInfo () {
  193. try {
  194. const data = await getEnterpriseBaseInfo()
  195. // 检验必填信息
  196. const keyArr = ['industryId', 'financingStatus', 'scale', 'introduce', 'logoUrl'] // 必填信息列表
  197. let href = '/recruit/enterprise/entInfoSetting'
  198. const valid = Object.keys(data).length && keyArr.every(e => {
  199. const bool = data[e] && data[e] !== 0
  200. if (!bool && e === 'logoUrl') href = '/recruit/enterprise/entInfoSetting?tabKey=2'
  201. return bool
  202. })
  203. if (!valid) {
  204. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', href)
  205. }
  206. } catch (error) {
  207. // console.log(error)
  208. }
  209. },
  210. // 获取用户账户信息
  211. async getUserAccountInfo () {
  212. const data = await getUserAccount()
  213. if (!data) return
  214. this.userAccount = data
  215. this.getUserAccountBalance()
  216. // localStorage.setItem('userAccount', JSON.stringify(data))
  217. },
  218. // 获取账户余额
  219. async getUserAccountBalance () {
  220. const data = await getAccountBalance()
  221. const obj = Object.assign(this.userAccount, data)
  222. localStorage.setItem('userAccount', JSON.stringify(obj))
  223. }
  224. }
  225. },
  226. {
  227. persist: true,
  228. devtools: true
  229. }
  230. )