user.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { defineStore } from 'pinia'
  2. import { setToken, removeToken, setRefreshToken, getToken } from '@/utils/auth'
  3. import { smsLogin, passwordLogin, getBaseInfo, passwordLoginOfEnterprise, smsLoginOfEnterprise, switchLoginOfEnterprise, getEnterprisingUserInfo, logoutToken, logout } from '@/api/common'
  4. import { getUserInfo } from '@/api/personal/user'
  5. import Snackbar from '@/plugins/snackbar'
  6. import { timesTampChange } from '@/utils/date'
  7. import { getBaseInfoDictOfName } from '@/utils/getText'
  8. export const useUserStore = defineStore('user',
  9. {
  10. state: () => ({
  11. loginType: null, // 登录类型 // 330为企业登录
  12. accountInfo: {}, // 登录返回的信息
  13. userInfo: {}, // 当前登录账号信息
  14. baseInfo: {}, // 人才信息
  15. isEnterpriseAdmin: false // 企业管理员
  16. }),
  17. actions: {
  18. // 短信登录
  19. async handleSmsLogin (data) {
  20. return new Promise((resolve, reject) => {
  21. const loginApi = data.loginType === 330 ? smsLoginOfEnterprise : smsLogin
  22. loginApi(data).then(res => {
  23. this.loginType = data.loginType
  24. setToken(res.accessToken)
  25. setRefreshToken(res.refreshToken)
  26. this.accountInfo = res
  27. localStorage.setItem('accountInfo', JSON.stringify(res))
  28. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  29. localStorage.setItem('loginType', 'personal')
  30. this.getUserInfos()
  31. if (!this.loginType) this.getUserBaseInfos()
  32. resolve()
  33. }).catch(err => { reject(err) })
  34. })
  35. },
  36. // // 密码登录
  37. async handlePasswordLogin(data) {
  38. return new Promise((resolve, reject) => {
  39. const loginApi = data.loginType === 330 ? passwordLoginOfEnterprise : passwordLogin
  40. loginApi(data).then(res => {
  41. this.loginType = data.loginType
  42. setToken(res.accessToken)
  43. setRefreshToken(res.refreshToken)
  44. this.accountInfo = res
  45. localStorage.setItem('accountInfo', JSON.stringify(res))
  46. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  47. localStorage.setItem('loginType', 'personal')
  48. this.getUserInfos()
  49. if (!this.loginType) this.getUserBaseInfos()
  50. resolve()
  51. }).catch(err => { reject(err) })
  52. })
  53. },
  54. // 获取当前登录账户信息
  55. async getUserInfos () {
  56. try {
  57. const api = this.loginType ? getEnterprisingUserInfo : getUserInfo
  58. const data = await api({ id: this.accountInfo.userId })
  59. this.userInfo = data
  60. localStorage.setItem('userInfo', JSON.stringify(data))
  61. } catch (error) {
  62. Snackbar.error(error.msg)
  63. }
  64. },
  65. // 获取当前登录账户的基本信息(人才信息)
  66. async getUserBaseInfos (userId = null) {
  67. try {
  68. const api = this.loginType ? null : getBaseInfo
  69. if (!api) return
  70. const data = await api({ userId: userId || this.accountInfo.userId })
  71. if (!data) return
  72. this.baseInfo = await this.getFieldText(data)
  73. localStorage.setItem('baseInfo', JSON.stringify(this.baseInfo))
  74. } catch (error) {
  75. Snackbar.error(error.msg)
  76. }
  77. },
  78. // 字典对应中文
  79. async getFieldText (data) {
  80. if (data.birthday && data.birthday !== 0) data.birthdayText = timesTampChange(data.birthday).slice(0, 10) // 出生日期
  81. if (data.firstWorkTime && data.firstWorkTime !== 0) data.firstWorkTimeText = timesTampChange(data.firstWorkTime).slice(0, 10) // 首次工作时间
  82. if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  83. if (data.eduType && data.eduType !== 0) await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  84. if (data.expType && data.expType !== 0) await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  85. if (data.sex && data.sex !== 0) await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  86. if (data.jobType && data.jobType !== 0) await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  87. if (data.jobStatus && data.jobStatus !== 0) await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  88. if (data.maritalStatus && data.maritalStatus !== 0) await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  89. return data
  90. },
  91. // 退出登录
  92. async userLogout (type) {
  93. // type: 1求职端 2招聘端
  94. if (type === 1) {
  95. await logout()
  96. } else await logoutToken(getToken())
  97. removeToken()
  98. this.userInfo = {}
  99. this.baseInfo = {}
  100. this.accountInfo = {}
  101. localStorage.clear()
  102. },
  103. // 切换为招聘者
  104. async changeRole (enterpriseId) {
  105. // 先退出个人登录
  106. await logout()
  107. const data = await switchLoginOfEnterprise({ enterpriseId })
  108. setToken(data.accessToken)
  109. setRefreshToken(data.refreshToken)
  110. localStorage.setItem('accountInfo', JSON.stringify(data))
  111. localStorage.setItem('expiresTime', data.expiresTime)
  112. localStorage.setItem('currentRole', 'enterprise')
  113. await this.getEnterpriseInfo()
  114. Snackbar.success('切换成功')
  115. },
  116. // 获取当前登录的企业用户信息
  117. async getEnterpriseInfo () {
  118. const result = await getEnterprisingUserInfo()
  119. this.baseInfo = result
  120. // 是否为企业账号管理员
  121. const isAdmin = result.userType === '1'
  122. localStorage.setItem('isAdmin', isAdmin)
  123. localStorage.setItem('baseInfo', JSON.stringify(result))
  124. }
  125. }
  126. },
  127. {
  128. persist: true
  129. }
  130. )