user.js 11 KB

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