123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { defineStore } from 'pinia'
- import { reactive } from 'vue'
- import { setToken, removeToken } from '@/utils/auth'
- import { smsLogin, passwordLogin, getBaseInfo } 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',
- () => {
- let accountInfo = reactive({}) // 登录返回的信息
- let userInfo = reactive({}) // 当前登录账号信息
- let baseInfo = reactive({}) // 人才信息
- // 短信登录
- const handleSmsLogin = async (data) => {
- return new Promise((resolve, reject) => {
- smsLogin(data).then(res => {
- console.log(res, 'login-phone')
- setToken(res.accessToken)
- accountInfo = res
- localStorage.setItem('accountInfo', JSON.stringify(res))
- localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
- getUserInfos()
- getUserBaseInfos()
- resolve()
- }).catch(err => { reject(err) })
- })
- }
- // 密码登录
- const handlePasswordLogin = async (data) => {
- return new Promise((resolve, reject) => {
- passwordLogin(data).then(res => {
- console.log(res, 'login-password')
- setToken(res.accessToken)
- accountInfo = res
- localStorage.setItem('accountInfo', JSON.stringify(res))
- localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
- getUserInfos()
- getUserBaseInfos()
- resolve()
- }).catch(err => { reject(err) })
- })
- }
- // 获取当前登录账户信息
- const getUserInfos = async () => {
- try {
- const data = await getUserInfo({ id: accountInfo.userId })
- userInfo = data
- localStorage.setItem('userInfo', JSON.stringify(data))
- } catch (error) {
- Snackbar.error(error.msg)
- }
- }
- // 获取当前登录账户的基本信息(人才信息)
- const getUserBaseInfos = async (userId = null) => {
- try {
- const data = await getBaseInfo({ userId: userId || accountInfo.userId })
- baseInfo = await getFieldText(data)
- localStorage.setItem('baseInfo', JSON.stringify(baseInfo))
- } catch (error) {
- Snackbar.error(error.msg)
- }
- }
- // 字典对应中文
- const getFieldText = async (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
- }
- // 退出登录
- const userLogout = async () => {
- await logout()
- removeToken()
- userInfo = {}
- baseInfo = {}
- accountInfo = {}
- localStorage.clear()
- }
- return {
- userInfo,
- baseInfo,
- getUserBaseInfos,
- handleSmsLogin,
- userLogout,
- handlePasswordLogin
- }
- },
- {
- persist: true,
- }
- )
|