You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
import { loginApi, getUserInfoApi, logOut } from '@/api/user'
|
|
|
|
import { ElNotification } from 'element-plus'
|
|
|
|
import { setToken, getToken, removeToken } from '@/utils/token'
|
|
|
|
|
|
|
|
const userStore = defineStore('defineStore', {
|
|
|
|
state: (): any => ({
|
|
|
|
token: getToken() || '',
|
|
|
|
userInfo: {},
|
|
|
|
}),
|
|
|
|
actions: {
|
|
|
|
async login(parmas: any) {
|
|
|
|
const res: any = await loginApi(parmas)
|
|
|
|
|
|
|
|
if (res.code === 412) {
|
|
|
|
return 0
|
|
|
|
} else if (res.code === 500) {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
console.log(res.result.userInfo)
|
|
|
|
this.token = res.result.token
|
|
|
|
setToken(this.token)
|
|
|
|
this.userInfo = res.result.userInfo
|
|
|
|
ElNotification({
|
|
|
|
title: '登录成功',
|
|
|
|
message: '欢迎回来' + this.userInfo.realname,
|
|
|
|
type: 'success',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async getUserInfo() {
|
|
|
|
try {
|
|
|
|
const res: any = await getUserInfoApi()
|
|
|
|
this.userInfo = res.result.userInfo
|
|
|
|
} catch (error:any) {
|
|
|
|
this.layOut()
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async layOut() {
|
|
|
|
await logOut()
|
|
|
|
removeToken()
|
|
|
|
this.token = ''
|
|
|
|
this.userInfo = {}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
export default userStore
|