user.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 { getEntUpdatePasswordCheck } from '@/api/recruit/enterprise/information'
  14. import { getEnterpriseUserAccount, getAccountBalance, getUserAccount, userRegister } from '@/api/common'
  15. import { getEnterpriseBaseInfo } from '@/api/enterprise'
  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 { checkPersonBaseInfo } from '@/utils/check'
  21. import { getStudentInfo } from '@/api/recruit/personal/resume'
  22. import { getSchoolInformation } from '@/api/school'
  23. import router from '@/router'
  24. // import Confirm from '@/plugins/confirm'
  25. // import { useIMStore } from './im'
  26. // const useIM = useIMStore()
  27. export const useUserStore = defineStore('user',
  28. {
  29. state: () => ({
  30. isAdmin: localStorage.getItem('isAdmin') === '1',
  31. token: getToken(),
  32. accountInfo: localStorage.getItem('accountInfo') ? JSON.parse(localStorage.getItem('accountInfo')) : {}, // 登录返回的信息
  33. userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {}, // 当前登录账号信息
  34. baseInfo: localStorage.getItem('baseInfo') ? JSON.parse(localStorage.getItem('baseInfo')) : {}, // 人才信息
  35. entBaseInfo: localStorage.getItem('entBaseInfo') ? JSON.parse(localStorage.getItem('entBaseInfo')) : {}, // 企业个人信息
  36. userAccount: {}, // 用户账户信息
  37. enterpriseUserAccount: {}, // 企业账户信息
  38. studentInfo: localStorage.getItem('studentInfo') ? JSON.parse(localStorage.getItem('studentInfo')) : {}, // 学生信息
  39. schoolInfo: localStorage.getItem('schoolInfo') ? JSON.parse(localStorage.getItem('schoolInfo')) : {}, // 学校信息
  40. }),
  41. actions: {
  42. // 个人用户注册并登录
  43. handleUserRegister (data) {
  44. return new Promise((resolve, reject) => {
  45. userRegister(data).then(async res => {
  46. setToken(res.accessToken)
  47. setRefreshToken(res.refreshToken)
  48. this.accountInfo = res
  49. localStorage.setItem('accountInfo', JSON.stringify(res))
  50. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  51. updateEventList(true) // 获取规则配置跟踪列表
  52. await this.getUserInfos()
  53. await this.getUserBaseInfos()
  54. resolve()
  55. }).catch(err => { reject(err) })
  56. })
  57. },
  58. // 短信登录
  59. handleSmsLogin (data) {
  60. return new Promise((resolve, reject) => {
  61. smsLogin(data).then(async res => {
  62. this.token = res.accessToken
  63. setToken(res.accessToken)
  64. setRefreshToken(res.refreshToken)
  65. this.accountInfo = res
  66. localStorage.setItem('accountInfo', JSON.stringify(res))
  67. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  68. updateEventList(true) // 获取规则配置跟踪列表
  69. await this.getUserInfos()
  70. await this.getUserBaseInfos('', { chooseRole: data.chooseRole })
  71. if (data?.schoolRegister) await this.getSchoolInfo(true)
  72. resolve(res)
  73. }).catch(err => { reject(err) })
  74. })
  75. },
  76. // 密码登录
  77. async handlePasswordLogin(data) {
  78. return new Promise((resolve, reject) => {
  79. data.account = data.phone
  80. passwordLogin(data).then(async res => {
  81. if (data.isEnterprise) { // 企业邮箱登录
  82. localStorage.setItem('emailLoginInfo', JSON.stringify(res))
  83. window.location.href = '/enterpriseVerification'
  84. } else {
  85. // 个人手机号登录
  86. setToken(res.accessToken)
  87. setRefreshToken(res.refreshToken)
  88. this.accountInfo = res
  89. localStorage.setItem('accountInfo', JSON.stringify(res))
  90. localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
  91. updateEventList(true) // 获取规则配置跟踪列表
  92. await this.getUserInfos()
  93. await this.getUserBaseInfos()
  94. }
  95. resolve()
  96. }).catch(err => {
  97. reject(err)
  98. })
  99. })
  100. },
  101. // 获取当前登录账户信息
  102. async getUserInfos () {
  103. try {
  104. const data = await getUserInfo({ id: this.accountInfo.userId })
  105. this.userInfo = data
  106. localStorage.setItem('userInfo', JSON.stringify(data))
  107. this.getUserAccountInfo()
  108. } catch (error) {
  109. Snackbar.error(error.msg)
  110. }
  111. },
  112. // 获取当前登录账户的基本信息(人才信息)
  113. async getUserBaseInfos (userId = null, option) {
  114. try {
  115. let data = await getBaseInfo({ userId: userId || this.accountInfo.userId })
  116. data = data || {}
  117. this.baseInfo = await this.getFieldText(data)
  118. localStorage.setItem('baseInfo', JSON.stringify(this.baseInfo))
  119. localStorage.setItem('necessaryInfoReady', !option?.chooseRole || checkPersonBaseInfo(this.baseInfo) ? 'ready' : 'fddeaddc47868b')
  120. if (option?.chooseRole && import.meta.env.VITE_NODE_ENV !== 'production') {
  121. // // 刚注册时让用户选择学生用户还是求职者用户,角色不同填写的基本信息不同。
  122. localStorage.setItem('chooseRole', 'showChooseRole')
  123. }
  124. // 当前角色若为学生则获取学生信息
  125. if (data?.type && Number(data.type) === 1) this.getStudentInformation()
  126. } catch (error) {
  127. Snackbar.error(error)
  128. }
  129. },
  130. // 字典对应中文
  131. async getFieldText (data) {
  132. if (!data || !Object.keys(data).length) return {}
  133. if (data.birthday && data.birthday !== 0) data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  134. if (data.firstWorkTime && data.firstWorkTime !== 0) data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  135. if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  136. if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  137. if (data.eduType && data.eduType !== 0) await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  138. if (data.expType && data.expType !== 0) await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  139. if (data.sex && data.sex !== 0) await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  140. if (data.jobType && data.jobType !== 0) await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  141. if (data.jobStatus && data.jobStatus !== 0) await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  142. if (data.maritalStatus && data.maritalStatus !== 0) await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  143. return data
  144. },
  145. // 退出登录
  146. async userLogout (type) {
  147. // type: 1求职端 2招聘端
  148. if (type === 1) {
  149. await logout()
  150. } else await logoutToken(getToken(1))
  151. this.handleClearStorage()
  152. },
  153. // 清除缓存
  154. handleClearStorage () {
  155. removeToken()
  156. this.token = ''
  157. this.userInfo = {}
  158. this.baseInfo = {}
  159. this.accountInfo = {}
  160. // 商城模版数据不清除缓存
  161. const mallTemplate = localStorage.getItem('mallTemplate')
  162. localStorage.clear()
  163. localStorage.setItem('mallTemplate', mallTemplate)
  164. },
  165. // 切换为招聘者
  166. async changeRole (res) {
  167. // 切换企业时需将个人的账户信息另外储存起来,以防企业账户有角色无菜单权限返回首页清除企业信息时个人账户信息丢失
  168. const perAccountData = JSON.parse(localStorage.getItem('accountInfo'))
  169. localStorage.setItem('perAccountInfo', JSON.stringify(perAccountData))
  170. let data
  171. if (res?.type === 'emailLogin') {
  172. data = res
  173. } else {
  174. const enterpriseId = localStorage.getItem('enterpriseId') || ''
  175. if (!enterpriseId) return Snackbar.error('切换失败,请重新登录!')
  176. data = await switchLoginOfEnterprise({ enterpriseId })
  177. }
  178. setToken(data.accessToken, 1) // 个人切换企业->存放企业token
  179. setRefreshToken(data.refreshToken, 1) // 个人切换企业->存放企业refreshToken
  180. localStorage.setItem('accountInfo', JSON.stringify(data))
  181. localStorage.setItem('expiresTime', data.expiresTime)
  182. updateEventList(false)
  183. // 企业受邀加入企业,只保存token等操作。
  184. if (res?.onlySetToken) return
  185. await this.updatePasswordCheck() // 检查密码是否需要修改
  186. await this.getEnterpriseInfo()
  187. await this.getEnterpriseUserAccountInfo()
  188. Snackbar.success(res?.type === 'emailLogin' ? '登录成功' : '切换成功')
  189. let href = '/recruit/enterprise'
  190. // 是否存在重定向
  191. if (localStorage.getItem('enterpriseRedirect')) {
  192. href = localStorage.getItem('enterpriseRedirect')
  193. localStorage.setItem('enterpriseRedirect', '')
  194. }
  195. // 人才推荐不需要跳转
  196. if (!res.noJump) {
  197. window.location.href = href
  198. }
  199. },
  200. // 获取当前登录的企业用户信息
  201. async getEnterpriseInfo (check) {
  202. const result = await getEnterprisingUserInfo()
  203. this.entBaseInfo = result
  204. // 是否为企业账号管理员
  205. this.isAdmin = result.userType === '1'
  206. localStorage.setItem('isAdmin', result.userType)
  207. if (this.isAdmin && !check) await this.checkEnterpriseBaseInfo() // 校验企业必填信息
  208. localStorage.setItem('entBaseInfo', JSON.stringify(result))
  209. },
  210. // 获取企业账户信息
  211. async getEnterpriseUserAccountInfo () {
  212. const data = await getEnterpriseUserAccount()
  213. if (!data) return
  214. this.enterpriseUserAccount = data
  215. localStorage.setItem('enterpriseUserAccount', JSON.stringify(data))
  216. return data // 方便直接获取
  217. },
  218. // 获取《企业基本信息》
  219. async checkEnterpriseBaseInfo () {
  220. try {
  221. const data = await getEnterpriseBaseInfo()
  222. if (data?.first === null || data?.first === false) { // null或者为false才弹
  223. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '/recruit/enterprise/systemManagement/entInfoSetting')
  224. }
  225. if (!data?.bizFlag) { // 企业登录免费职位广告提示,除了true都弹窗
  226. localStorage.setItem('positionAd', 'showPositionAd')
  227. }
  228. } catch (error) {
  229. }
  230. },
  231. // 获取用户账户信息
  232. async getUserAccountInfo () {
  233. const data = await getUserAccount()
  234. if (!data) return
  235. this.userAccount = data
  236. this.getUserAccountBalance()
  237. },
  238. // 获取账户余额
  239. async getUserAccountBalance () {
  240. const data = await getAccountBalance()
  241. const obj = Object.assign(this.userAccount, data)
  242. localStorage.setItem('userAccount', JSON.stringify(obj))
  243. },
  244. // 检查密码是否需要修改
  245. async updatePasswordCheck () {
  246. const bool = await getEntUpdatePasswordCheck()
  247. if (bool) {
  248. // 强制修改密码
  249. localStorage.setItem('entUpdatePassword', bool ? 'needChange' : 'doNotNeedChange')
  250. }
  251. },
  252. // 获取学生信息
  253. async getStudentInformation () {
  254. const data = await getStudentInfo()
  255. this.studentInfo = data
  256. localStorage.setItem('studentInfo', data ? JSON.stringify(data) : '{}')
  257. router.push('/recruit/personal/personalCenter/student/information')
  258. },
  259. // 获取学校基本信息
  260. async getSchoolInfo (isRegister = false) {
  261. const data = await getSchoolInformation()
  262. // 存储学校登录标识
  263. localStorage.setItem('isSchool', 1)
  264. this.schoolInfo = data || {}
  265. localStorage.setItem('schoolInfo', data ? JSON.stringify(data) : '{}')
  266. // 注册时执行下方内容
  267. if (!isRegister) return
  268. if (!data || !Object.keys(data).length) {
  269. // console.log('没有注册过,直接跳转到学校注册页面')
  270. router.push({ path: '/register/schoolIndex' })
  271. }
  272. else if (data?.authStatus === '0' || data?.authStatus === '2') {
  273. // console.log('审核中,等待审核 || 审核不通过,重新填写信息提交', data.authStatus)
  274. localStorage.setItem('registerSchoolInfo', JSON.stringify(data))
  275. router.push({ path: '/register/school/inReview' })
  276. }
  277. else if (data?.authStatus === '1') {
  278. // console.log('审核通过直接进入老师页面')
  279. router.push('/recruit/teacher/studentList/index')
  280. }
  281. }
  282. }
  283. },
  284. {
  285. persist: true,
  286. devtools: true
  287. }
  288. )