user.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { defineStore } from 'pinia'
  2. import { setToken, removeToken, setRefreshToken, getToken } from '@/utils/auth'
  3. import {
  4. smsLogin,
  5. passwordLogin,
  6. getBaseInfo,
  7. passwordLoginOfEnterprise,
  8. smsLoginOfEnterprise,
  9. switchLoginOfEnterprise,
  10. getEnterprisingUserInfo,
  11. logoutToken,
  12. logout
  13. } from '@/api/common'
  14. import { getUserInfo } from '@/api/personal/user'
  15. import { getEnterpriseUserAccount, getUserAccount } from '@/api/common'
  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 { getChatKey } from '@/api/common'
  21. export const useUserStore = defineStore('user',
  22. {
  23. state: () => ({
  24. // loginType: null, // 登录类型 // 330为企业登录
  25. accountInfo: {}, // 登录返回的信息
  26. userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {}, // 当前登录账号信息
  27. baseInfo: localStorage.getItem('baseInfo') ? JSON.parse(localStorage.getItem('baseInfo')) : {}, // 人才信息
  28. userAccount: {}, // 用户账户信息
  29. enterpriseUserAccount: {}, // 企业账户信息
  30. IMConfig: localStorage.getItem('IMConfig') ? JSON.parse(localStorage.getItem('IMConfig')) : {} // IM聊天密钥
  31. }),
  32. actions: {
  33. // 短信登录
  34. handleSmsLogin (data) {
  35. return new Promise((resolve, reject) => {
  36. const loginApi = data.loginType === 330 ? smsLoginOfEnterprise : smsLogin
  37. loginApi(data).then(res => {
  38. // this.loginType = data.loginType
  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. localStorage.setItem('loginType', 'personal')
  45. this.getUserInfos()
  46. this.getUserBaseInfos()
  47. resolve()
  48. }).catch(err => { reject(err) })
  49. })
  50. },
  51. // 获取聊天密钥
  52. getChatKey () {
  53. return new Promise((resolve, reject) => {
  54. getChatKey({ userId: this.userInfo.id })
  55. .then(res => {
  56. this.IMConfig = res
  57. localStorage.setItem('IMConfig', JSON.stringify(res))
  58. resolve()
  59. }).catch(error => {
  60. reject(error)
  61. })
  62. })
  63. },
  64. // 密码登录
  65. async handlePasswordLogin(data) {
  66. return new Promise((resolve, reject) => {
  67. const loginApi = data.loginType === 330 ? passwordLoginOfEnterprise : passwordLogin
  68. loginApi(data).then(res => {
  69. setToken(res.accessToken)
  70. setRefreshToken(res.refreshToken)
  71. this.accountInfo = res
  72. localStorage.setItem('accountInfo', JSON.stringify(res))
  73. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  74. localStorage.setItem('loginType', 'personal')
  75. this.getUserInfos()
  76. this.getUserBaseInfos()
  77. resolve()
  78. }).catch(err => { reject(err) })
  79. })
  80. },
  81. // 获取当前登录账户信息
  82. async getUserInfos () {
  83. try {
  84. const api = this.loginType ? getEnterprisingUserInfo : getUserInfo
  85. const data = await api({ id: this.accountInfo.userId })
  86. this.userInfo = data
  87. localStorage.setItem('userInfo', JSON.stringify(data))
  88. // 获取密钥
  89. this.getChatKey()
  90. updateEventList(true) // 获取规则配置跟踪列表
  91. this.getUserAccountInfo()
  92. } catch (error) {
  93. Snackbar.error(error.msg)
  94. }
  95. },
  96. // 获取当前登录账户的基本信息(人才信息)
  97. async getUserBaseInfos (userId = null) {
  98. try {
  99. const api = this.loginType ? null : getBaseInfo
  100. if (!api) return
  101. const data = await api({ userId: userId || this.accountInfo.userId })
  102. if (!data) return localStorage.setItem('baseInfo', JSON.stringify('{}'))
  103. this.baseInfo = await this.getFieldText(data)
  104. localStorage.setItem('baseInfo', JSON.stringify(this.baseInfo))
  105. } catch (error) {
  106. console.log(error, 'error')
  107. Snackbar.error(error)
  108. }
  109. },
  110. // 字典对应中文
  111. async getFieldText (data) {
  112. if (data.birthday && data.birthday !== 0) data.birthdayText = timesTampChange(data.birthday).slice(0, 10) // 出生日期
  113. if (data.firstWorkTime && data.firstWorkTime !== 0) data.firstWorkTimeText = timesTampChange(data.firstWorkTime).slice(0, 10) // 首次工作时间
  114. if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  115. if (data.eduType && data.eduType !== 0) await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  116. if (data.expType && data.expType !== 0) await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  117. if (data.sex && data.sex !== 0) await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  118. if (data.jobType && data.jobType !== 0) await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  119. if (data.jobStatus && data.jobStatus !== 0) await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  120. if (data.maritalStatus && data.maritalStatus !== 0) await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  121. return data
  122. },
  123. // 退出登录
  124. async userLogout (type) {
  125. // type: 1求职端 2招聘端
  126. if (type === 1) {
  127. await logout()
  128. } else await logoutToken(getToken())
  129. removeToken()
  130. this.userInfo = {}
  131. this.baseInfo = {}
  132. this.accountInfo = {}
  133. localStorage.clear()
  134. },
  135. // 切换为招聘者
  136. async changeRole (enterpriseId) {
  137. // 先退出个人登录
  138. await logout()
  139. const data = await switchLoginOfEnterprise({ enterpriseId })
  140. setToken(data.accessToken)
  141. setRefreshToken(data.refreshToken)
  142. localStorage.setItem('loginType', 'enterprise')
  143. localStorage.setItem('accountInfo', JSON.stringify(data))
  144. localStorage.setItem('expiresTime', data.expiresTime)
  145. localStorage.setItem('currentRole', 'enterprise')
  146. await this.getEnterpriseInfo()
  147. await this.getEnterpriseUserAccountInfo()
  148. updateEventList(false)
  149. Snackbar.success('切换成功')
  150. },
  151. // 获取当前登录的企业用户信息
  152. async getEnterpriseInfo () {
  153. const result = await getEnterprisingUserInfo()
  154. this.baseInfo = result
  155. // 是否为企业账号管理员
  156. const isAdmin = result.userType === '1'
  157. localStorage.setItem('isAdmin', isAdmin)
  158. localStorage.setItem('baseInfo', JSON.stringify(result))
  159. },
  160. // 获取企业账户信息
  161. async getEnterpriseUserAccountInfo () {
  162. const data = await getEnterpriseUserAccount()
  163. if (!data) return
  164. this.enterpriseUserAccount = data
  165. localStorage.setItem('enterpriseUserAccount', JSON.stringify(data))
  166. },
  167. // 获取用户账户信息
  168. async getUserAccountInfo () {
  169. const data = await getUserAccount()
  170. if (!data) return
  171. this.userAccount = data
  172. localStorage.setItem('userAccount', JSON.stringify(data))
  173. }
  174. }
  175. },
  176. {
  177. persist: true,
  178. devtools: true
  179. }
  180. )