import { defineStore } from 'pinia' import { setToken, removeToken, setRefreshToken, getToken } from '@/utils/auth' import { smsLogin, passwordLogin, getBaseInfo, switchLoginOfEnterprise, getEnterprisingUserInfo, logoutToken, logout } from '@/api/common' import { getUserInfo } from '@/api/personal/user' import { getEntUpdatePasswordCheck } from '@/api/recruit/enterprise/information' import { getEnterpriseUserAccount, getAccountBalance, getUserAccount, userRegister } from '@/api/common' import { getEnterpriseBaseInfo } from '@/api/enterprise' import Snackbar from '@/plugins/snackbar' import { timesTampChange } from '@/utils/date' import { updateEventList } from '@/utils/eventList' import { getBaseInfoDictOfName } from '@/utils/getText' import { checkPersonBaseInfo } from '@/utils/check' import { getStudentInfo } from '@/api/recruit/personal/resume' // import Confirm from '@/plugins/confirm' // import { useIMStore } from './im' // const useIM = useIMStore() export const useUserStore = defineStore('user', { state: () => ({ token: getToken(), accountInfo: localStorage.getItem('accountInfo') ? JSON.parse(localStorage.getItem('accountInfo')) : {}, // 登录返回的信息 userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {}, // 当前登录账号信息 baseInfo: localStorage.getItem('baseInfo') ? JSON.parse(localStorage.getItem('baseInfo')) : {}, // 人才信息 entBaseInfo: localStorage.getItem('entBaseInfo') ? JSON.parse(localStorage.getItem('entBaseInfo')) : {}, // 企业个人信息 userAccount: {}, // 用户账户信息 enterpriseUserAccount: {}, // 企业账户信息 studentInfo: localStorage.getItem('studentInfo') ? JSON.parse(localStorage.getItem('studentInfo')) : {}, // 学生信息 }), actions: { // 个人用户注册并登录 handleUserRegister (data) { return new Promise((resolve, reject) => { userRegister(data).then(async res => { setToken(res.accessToken) setRefreshToken(res.refreshToken) this.accountInfo = res localStorage.setItem('accountInfo', JSON.stringify(res)) localStorage.setItem('expiresTime', res.expiresTime) // token过期时间 updateEventList(true) // 获取规则配置跟踪列表 await this.getUserInfos() await this.getUserBaseInfos() resolve() }).catch(err => { reject(err) }) }) }, // 短信登录 handleSmsLogin (data) { return new Promise((resolve, reject) => { smsLogin(data).then(async res => { this.token = res.accessToken setToken(res.accessToken) setRefreshToken(res.refreshToken) this.accountInfo = res localStorage.setItem('accountInfo', JSON.stringify(res)) localStorage.setItem('expiresTime', res.expiresTime) // token过期时间 updateEventList(true) // 获取规则配置跟踪列表 await this.getUserInfos() await this.getUserBaseInfos('', { chooseRole: data.chooseRole }) resolve(res) }).catch(err => { reject(err) }) }) }, // 密码登录 async handlePasswordLogin(data) { return new Promise((resolve, reject) => { data.account = data.phone passwordLogin(data).then(async res => { if (data.isEnterprise) { // 企业邮箱登录 localStorage.setItem('emailLoginInfo', JSON.stringify(res)) window.location.href = '/enterpriseVerification' } else { // 个人手机号登录 setToken(res.accessToken) setRefreshToken(res.refreshToken) this.accountInfo = res localStorage.setItem('accountInfo', JSON.stringify(res)) localStorage.setItem('expiresTime', res.expiresTime) // token过期时间 updateEventList(true) // 获取规则配置跟踪列表 await this.getUserInfos() await this.getUserBaseInfos() } resolve() }).catch(err => { reject(err) }) }) }, // 获取当前登录账户信息 async getUserInfos () { try { const data = await getUserInfo({ id: this.accountInfo.userId }) this.userInfo = data localStorage.setItem('userInfo', JSON.stringify(data)) this.getUserAccountInfo() } catch (error) { Snackbar.error(error.msg) } }, // 获取当前登录账户的基本信息(人才信息) async getUserBaseInfos (userId = null, option) { try { let data = await getBaseInfo({ userId: userId || this.accountInfo.userId }) data = data || {} this.baseInfo = await this.getFieldText(data) localStorage.setItem('baseInfo', JSON.stringify(this.baseInfo)) localStorage.setItem('necessaryInfoReady', checkPersonBaseInfo(this.baseInfo) ? 'ready' : 'fddeaddc47868b') // 校验是否完善人才必填信息 if (option?.chooseRole && import.meta.env.VITE_NODE_ENV !== 'production') { // 刚注册时让用户选择学生用户还是求职者用户,角色不同填写的基本信息不同。 localStorage.setItem('chooseRole', 'showChooseRole') } // 当前角色若为学生则获取学生信息 if (data?.type && Number(data.type) === 1) this.getStudentInformation() } catch (error) { Snackbar.error(error) } }, // 字典对应中文 async getFieldText (data) { if (!data || !Object.keys(data).length) return {} if (data.birthday && data.birthday !== 0) data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期 if (data.firstWorkTime && data.firstWorkTime !== 0) data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间 if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text if (data.eduType && data.eduType !== 0) await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历 if (data.expType && data.expType !== 0) await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验 if (data.sex && data.sex !== 0) await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别 if (data.jobType && data.jobType !== 0) await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型 if (data.jobStatus && data.jobStatus !== 0) await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态 if (data.maritalStatus && data.maritalStatus !== 0) await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况 return data }, // 退出登录 async userLogout (type) { // type: 1求职端 2招聘端 if (type === 1) { await logout() } else await logoutToken(getToken(1)) this.handleClearStorage() }, // 清除缓存 handleClearStorage () { removeToken() this.token = '' this.userInfo = {} this.baseInfo = {} this.accountInfo = {} // 商城模版数据不清除缓存 const mallTemplate = localStorage.getItem('mallTemplate') localStorage.clear() localStorage.setItem('mallTemplate', mallTemplate) }, // 切换为招聘者 async changeRole (res) { let data if (res?.type === 'emailLogin') { data = res } else { const enterpriseId = localStorage.getItem('enterpriseId') || '' if (!enterpriseId) return Snackbar.error('切换失败,请重新登录!') data = await switchLoginOfEnterprise({ enterpriseId }) } setToken(data.accessToken, 1) // 个人切换企业->存放企业token setRefreshToken(data.refreshToken, 1) // 个人切换企业->存放企业refreshToken localStorage.setItem('accountInfo', JSON.stringify(data)) localStorage.setItem('expiresTime', data.expiresTime) updateEventList(false) // 企业受邀加入企业,只保存token等操作。 if (res?.onlySetToken) return await this.updatePasswordCheck() // 检查密码是否需要修改 await this.getEnterpriseInfo() await this.getEnterpriseUserAccountInfo() Snackbar.success(res?.type === 'emailLogin' ? '登录成功' : '切换成功') let href = '/recruit/enterprise' // 是否存在重定向 if (localStorage.getItem('enterpriseRedirect')) { href = localStorage.getItem('enterpriseRedirect') localStorage.setItem('enterpriseRedirect', '') } // 人才推荐不需要跳转 if (!res.noJump) { setTimeout(() => { window.location.href = href }, 1000) } }, // 获取当前登录的企业用户信息 async getEnterpriseInfo (check) { const result = await getEnterprisingUserInfo() this.entBaseInfo = result // 是否为企业账号管理员 const isAdmin = result.userType === '1' localStorage.setItem('isAdmin', isAdmin) if (isAdmin && !check) await this.checkEnterpriseBaseInfo() // 校验企业必填信息 localStorage.setItem('entBaseInfo', JSON.stringify(result)) }, // 获取企业账户信息 async getEnterpriseUserAccountInfo () { const data = await getEnterpriseUserAccount() if (!data) return this.enterpriseUserAccount = data // this.getUserAccountBalance() localStorage.setItem('enterpriseUserAccount', JSON.stringify(data)) return data // 方便直接获取 }, // 获取《企业基本信息》 async checkEnterpriseBaseInfo () { try { const data = await getEnterpriseBaseInfo() if (data?.first === null || data?.first === false) { // null或者为false才弹 localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '/recruit/enterprise/entInfoSetting') } if (!data?.bizFlag) { // 企业登录免费职位广告提示,除了true都弹窗 localStorage.setItem('positionAd', 'showPositionAd') } } catch (error) { } }, // 获取用户账户信息 async getUserAccountInfo () { const data = await getUserAccount() if (!data) return this.userAccount = data this.getUserAccountBalance() // localStorage.setItem('userAccount', JSON.stringify(data)) }, // 获取账户余额 async getUserAccountBalance () { const data = await getAccountBalance() const obj = Object.assign(this.userAccount, data) localStorage.setItem('userAccount', JSON.stringify(obj)) }, // 检查密码是否需要修改 async updatePasswordCheck () { const bool = await getEntUpdatePasswordCheck() if (bool) { // 强制修改密码 localStorage.setItem('entUpdatePassword', bool ? 'needChange' : 'doNotNeedChange') } }, // 获取学生信息 async getStudentInformation () { try { const data = await getStudentInfo() this.studentInfo = data localStorage.setItem('studentInfo', data ? JSON.stringify(data) : '{}') } catch (error) { Snackbar.error(error.msg) } } } }, { persist: true, devtools: true } )