user.js 11 KB

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