user.js 11 KB

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