user.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // 企业受邀加入企业,只保存token等操作。
  172. if (res?.onlySetToken) return
  173. await this.updatePasswordCheck() // 检查密码是否需要修改
  174. await this.getEnterpriseInfo()
  175. await this.getEnterpriseUserAccountInfo()
  176. Snackbar.success(res?.type === 'emailLogin' ? '登录成功' : '切换成功')
  177. let href = '/enterprise'
  178. // 是否存在重定向
  179. if (localStorage.getItem('enterpriseRedirect')) {
  180. href = localStorage.getItem('enterpriseRedirect')
  181. localStorage.setItem('enterpriseRedirect', '')
  182. }
  183. // 人才推荐不需要跳转
  184. if (!res.noJump) {
  185. setTimeout(() => { window.location.href = href }, 1000)
  186. }
  187. },
  188. // 获取当前登录的企业用户信息
  189. async getEnterpriseInfo (check) {
  190. const result = await getEnterprisingUserInfo()
  191. this.entBaseInfo = result
  192. // 是否为企业账号管理员
  193. this.isAdmin = result.userType === '1'
  194. localStorage.setItem('isAdmin', result.userType)
  195. if (this.isAdmin && !check) await this.checkEnterpriseBaseInfo() // 校验企业必填信息
  196. localStorage.setItem('entBaseInfo', JSON.stringify(result))
  197. },
  198. // 获取企业账户信息
  199. async getEnterpriseUserAccountInfo () {
  200. const data = await getEnterpriseUserAccount()
  201. if (!data) return
  202. this.enterpriseUserAccount = data
  203. // this.getUserAccountBalance()
  204. localStorage.setItem('enterpriseUserAccount', JSON.stringify(data))
  205. return data // 方便直接获取
  206. },
  207. // 获取《企业基本信息》
  208. async checkEnterpriseBaseInfo () {
  209. try {
  210. const data = await getEnterpriseBaseInfo()
  211. if (data?.first === null || data?.first === false) { // null或者为false才弹
  212. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '/recruit/enterprise/entInfoSetting')
  213. }
  214. if (!data?.bizFlag) { // 企业登录免费职位广告提示,除了true都弹窗
  215. localStorage.setItem('positionAd', 'showPositionAd')
  216. }
  217. // // 检验必填信息
  218. // const keyArr = ['industryId', 'financingStatus', 'scale', 'introduce', 'logoUrl'] // 必填信息列表
  219. // let href = '/recruit/enterprise/entInfoSetting'
  220. // const valid = Object.keys(data).length && keyArr.every(e => {
  221. // const bool = data[e] && data[e] !== 0
  222. // if (!bool && e === 'logoUrl') href = '/recruit/enterprise/entInfoSetting?tabKey=2'
  223. // return bool
  224. // })
  225. // if (!valid) {
  226. // localStorage.setItem('checkEnterpriseBaseInfoFalseHref', href)
  227. // }
  228. } catch (error) {
  229. // console.log(error)
  230. }
  231. },
  232. // 获取用户账户信息
  233. async getUserAccountInfo () {
  234. const data = await getUserAccount()
  235. if (!data) return
  236. this.userAccount = data
  237. this.getUserAccountBalance()
  238. // localStorage.setItem('userAccount', JSON.stringify(data))
  239. },
  240. // 获取账户余额
  241. async getUserAccountBalance () {
  242. const data = await getAccountBalance()
  243. const obj = Object.assign(this.userAccount, data)
  244. localStorage.setItem('userAccount', JSON.stringify(obj))
  245. },
  246. // 检查密码是否需要修改
  247. async updatePasswordCheck () {
  248. const bool = await getEntUpdatePasswordCheck()
  249. if (bool) {
  250. // 强制修改密码
  251. localStorage.setItem('entUpdatePassword', bool ? 'needChange' : 'doNotNeedChange')
  252. }
  253. }
  254. }
  255. },
  256. {
  257. persist: true,
  258. devtools: true
  259. }
  260. )