import { defineStore } from 'pinia' import { setToken, removeToken, setRefreshToken } from '@/utils/auth' import { smsLogin, passwordLogin, getBaseInfo, passwordLoginOfEnterprise, smsLoginOfEnterprise } from '@/api/common/index' import { logout } from '@/api/common/index' import { getUserInfo } from '@/api/personal/user' import Snackbar from '@/plugins/snackbar' import { timesTampChange } from '@/utils/date' import { getBaseInfoDictOfName } from '@/utils/getText' export const useUserStore = defineStore('user', { state: () => ({ accountInfo: {}, // 登录返回的信息 userInfo: {}, // 当前登录账号信息 baseInfo: {} // 人才信息 }), actions: { // 短信登录 async handleSmsLogin (data) { return new Promise((resolve, reject) => { const loginApi = data.type === 330 ? smsLoginOfEnterprise : smsLogin loginApi(data).then(res => { setToken(res.accessToken) setRefreshToken(res.refreshToken) this.accountInfo = res localStorage.setItem('accountInfo', JSON.stringify(res)) localStorage.setItem('expiresTime', res.expiresTime) // token过期时间 this.getUserInfos() this.getUserBaseInfos() resolve() }).catch(err => { reject(err) }) }) }, // // 密码登录 async handlePasswordLogin(data) { return new Promise((resolve, reject) => { const loginApi = data.type === 330 ? passwordLoginOfEnterprise : passwordLogin loginApi(data).then(res => { setToken(res.accessToken) setRefreshToken(res.refreshToken) this.accountInfo = res localStorage.setItem('accountInfo', JSON.stringify(res)) localStorage.setItem('expiresTime', res.expiresTime) // token过期时间 this.getUserInfos() 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)) } catch (error) { Snackbar.error(error.msg) } }, // 获取当前登录账户的基本信息(人才信息) async getUserBaseInfos (userId = null) { try { const data = await getBaseInfo({ userId: userId || this.accountInfo.userId }) this.baseInfo = await this.getFieldText(data) localStorage.setItem('baseInfo', JSON.stringify(this.baseInfo)) } catch (error) { Snackbar.error(error.msg) } }, // 字典对应中文 async getFieldText (data) { if (data.birthday && data.birthday !== 0) data.birthdayText = timesTampChange(data.birthday).slice(0, 10) // 出生日期 if (data.firstWorkTime && data.firstWorkTime !== 0) data.firstWorkTimeText = timesTampChange(data.firstWorkTime).slice(0, 10) // 首次工作时间 if (data.areaId && data.areaId !== 0) await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地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 () { await logout() removeToken() this.userInfo = {} this.baseInfo = {} this.accountInfo = {} localStorage.clear() } } }, { persist: true } )