user.js 8.7 KB

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