user.js 9.7 KB

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