user.js 9.6 KB

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