user.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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) {
  116. // 刚注册时让用户选择学生用户还是求职者用户,角色不同填写的基本信息不同。
  117. localStorage.setItem('chooseRole', 'showChooseRole')
  118. }
  119. } catch (error) {
  120. Snackbar.error(error)
  121. }
  122. },
  123. // 字典对应中文
  124. async getFieldText (data) {
  125. if (!data || !Object.keys(data).length) return {}
  126. if (data.birthday && data.birthday !== 0) data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  127. if (data.firstWorkTime && data.firstWorkTime !== 0) data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  128. if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  129. if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  130. if (data.eduType && data.eduType !== 0) await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  131. if (data.expType && data.expType !== 0) await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  132. if (data.sex && data.sex !== 0) await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  133. if (data.jobType && data.jobType !== 0) await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  134. if (data.jobStatus && data.jobStatus !== 0) await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  135. if (data.maritalStatus && data.maritalStatus !== 0) await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  136. return data
  137. },
  138. // 退出登录
  139. async userLogout (type) {
  140. // type: 1求职端 2招聘端
  141. if (type === 1) {
  142. await logout()
  143. } else await logoutToken(getToken(1))
  144. this.handleClearStorage()
  145. },
  146. // 清除缓存
  147. handleClearStorage () {
  148. removeToken()
  149. this.token = ''
  150. this.userInfo = {}
  151. this.baseInfo = {}
  152. this.accountInfo = {}
  153. // 商城模版数据不清除缓存
  154. const mallTemplate = localStorage.getItem('mallTemplate')
  155. localStorage.clear()
  156. localStorage.setItem('mallTemplate', mallTemplate)
  157. },
  158. // 切换为招聘者
  159. async changeRole (res) {
  160. let data
  161. if (res?.type === 'emailLogin') {
  162. data = res
  163. } else {
  164. const enterpriseId = localStorage.getItem('enterpriseId') || ''
  165. if (!enterpriseId) return Snackbar.error('切换失败,请重新登录!')
  166. data = await switchLoginOfEnterprise({ enterpriseId })
  167. }
  168. setToken(data.accessToken, 1) // 个人切换企业->存放企业token
  169. setRefreshToken(data.refreshToken, 1) // 个人切换企业->存放企业refreshToken
  170. localStorage.setItem('accountInfo', JSON.stringify(data))
  171. localStorage.setItem('expiresTime', data.expiresTime)
  172. updateEventList(false)
  173. // 企业受邀加入企业,只保存token等操作。
  174. if (res?.onlySetToken) return
  175. await this.updatePasswordCheck() // 检查密码是否需要修改
  176. await this.getEnterpriseInfo()
  177. await this.getEnterpriseUserAccountInfo()
  178. Snackbar.success(res?.type === 'emailLogin' ? '登录成功' : '切换成功')
  179. let href = '/recruit/enterprise'
  180. // 是否存在重定向
  181. if (localStorage.getItem('enterpriseRedirect')) {
  182. href = localStorage.getItem('enterpriseRedirect')
  183. localStorage.setItem('enterpriseRedirect', '')
  184. }
  185. // 人才推荐不需要跳转
  186. if (!res.noJump) {
  187. setTimeout(() => { window.location.href = href }, 1000)
  188. }
  189. },
  190. // 获取当前登录的企业用户信息
  191. async getEnterpriseInfo (check) {
  192. const result = await getEnterprisingUserInfo()
  193. this.entBaseInfo = result
  194. // 是否为企业账号管理员
  195. const isAdmin = result.userType === '1'
  196. localStorage.setItem('isAdmin', isAdmin)
  197. if (isAdmin && !check) await this.checkEnterpriseBaseInfo() // 校验企业必填信息
  198. localStorage.setItem('entBaseInfo', JSON.stringify(result))
  199. },
  200. // 获取企业账户信息
  201. async getEnterpriseUserAccountInfo () {
  202. const data = await getEnterpriseUserAccount()
  203. if (!data) return
  204. this.enterpriseUserAccount = data
  205. // this.getUserAccountBalance()
  206. localStorage.setItem('enterpriseUserAccount', JSON.stringify(data))
  207. return data // 方便直接获取
  208. },
  209. // 获取《企业基本信息》
  210. async checkEnterpriseBaseInfo () {
  211. try {
  212. const data = await getEnterpriseBaseInfo()
  213. if (data?.first === null || data?.first === false) { // null或者为false才弹
  214. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '/recruit/enterprise/entInfoSetting')
  215. }
  216. if (!data?.bizFlag) { // 企业登录免费职位广告提示,除了true都弹窗
  217. localStorage.setItem('positionAd', 'showPositionAd')
  218. }
  219. } catch (error) {
  220. }
  221. },
  222. // 获取用户账户信息
  223. async getUserAccountInfo () {
  224. const data = await getUserAccount()
  225. if (!data) return
  226. this.userAccount = data
  227. this.getUserAccountBalance()
  228. // localStorage.setItem('userAccount', JSON.stringify(data))
  229. },
  230. // 获取账户余额
  231. async getUserAccountBalance () {
  232. const data = await getAccountBalance()
  233. const obj = Object.assign(this.userAccount, data)
  234. localStorage.setItem('userAccount', JSON.stringify(obj))
  235. },
  236. // 检查密码是否需要修改
  237. async updatePasswordCheck () {
  238. const bool = await getEntUpdatePasswordCheck()
  239. if (bool) {
  240. // 强制修改密码
  241. localStorage.setItem('entUpdatePassword', bool ? 'needChange' : 'doNotNeedChange')
  242. }
  243. },
  244. // 获取学生信息
  245. async getStudentInformation () {
  246. try {
  247. const data = await getStudentInfo()
  248. this.studentInfo = data
  249. localStorage.setItem('studentInfo', data ? JSON.stringify(data) : '{}')
  250. } catch (error) {
  251. Snackbar.error(error.msg)
  252. }
  253. }
  254. }
  255. },
  256. {
  257. persist: true,
  258. devtools: true
  259. }
  260. )