user.js 7.1 KB

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