user.js 12 KB

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