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('user', { 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, 'res.result') 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