|
@@ -1,5 +1,4 @@
|
|
import { defineStore } from 'pinia'
|
|
import { defineStore } from 'pinia'
|
|
-import { reactive } from 'vue'
|
|
|
|
import { setToken, removeToken } from '@/utils/auth'
|
|
import { setToken, removeToken } from '@/utils/auth'
|
|
import { smsLogin, passwordLogin, getBaseInfo } from '@/api/common/index'
|
|
import { smsLogin, passwordLogin, getBaseInfo } from '@/api/common/index'
|
|
import { logout } from '@/api/common/index'
|
|
import { logout } from '@/api/common/index'
|
|
@@ -10,98 +9,75 @@ import { getBaseInfoDictOfName } from '@/utils/getText'
|
|
|
|
|
|
|
|
|
|
export const useUserStore = defineStore('user',
|
|
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)
|
|
|
|
|
|
+ {
|
|
|
|
+ state: () => ({
|
|
|
|
+ accountInfo: {}, // 登录返回的信息
|
|
|
|
+ userInfo: {}, // 当前登录账号信息
|
|
|
|
+ baseInfo: {} // 人才信息
|
|
|
|
+ }),
|
|
|
|
+ actions: {
|
|
|
|
+ // 验证码&密码登录
|
|
|
|
+ async handleTypeLogin (type, data) {
|
|
|
|
+ // type: 1验证码登录 0密码登录
|
|
|
|
+ const api = type ? smsLogin : passwordLogin
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ api(data).then(res => {
|
|
|
|
+ setToken(res.accessToken)
|
|
|
|
+ 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()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // 字典对应中文
|
|
|
|
- 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,
|
|
|
|
|
|
+ persist: true
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
|