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.
65 lines
1.4 KiB
65 lines
1.4 KiB
import {acceptHMRUpdate, createPinia, defineStore} from 'pinia';
|
|
|
|
type menu = 'light' | 'dark'
|
|
export interface UserState {
|
|
token: string;
|
|
info: any;
|
|
loginVisible: boolean;
|
|
redirect: any
|
|
}
|
|
const useUserInfo = defineStore('user', {
|
|
state: (): UserState => ({
|
|
token: '',
|
|
info: {},
|
|
loginVisible: false,
|
|
redirect: {}
|
|
}),
|
|
|
|
actions: {
|
|
setToken(token: string):void {
|
|
this.token = token;
|
|
},
|
|
setInfo(info: any):void {
|
|
this.info = info;
|
|
},
|
|
setLoginVisible(val: boolean, redirect?: {} | boolean):void {
|
|
this.loginVisible = val;
|
|
if (redirect) {
|
|
this.redirect = redirect;
|
|
}else {
|
|
this.redirect = false;
|
|
}
|
|
},
|
|
|
|
async login(data: any){
|
|
const { commonApi } = useApi()
|
|
const res = await commonApi.login(data);
|
|
this.token = res.data.userinfo.token;
|
|
this.loginVisible = false
|
|
this.info = res.data.userinfo
|
|
if(this.redirect) {
|
|
useRouter().push(this.redirect)
|
|
this.redirect = false;
|
|
}else {
|
|
location.reload()
|
|
}
|
|
return res;
|
|
}
|
|
|
|
},
|
|
getters:{
|
|
getLoginVisible:(state)=> state.loginVisible,
|
|
getToken:(state)=> state.token,
|
|
},
|
|
// @ts-ignore
|
|
persist: process.client && {
|
|
storage: localStorage,
|
|
paths: ['info', 'token']
|
|
}
|
|
|
|
})
|
|
export default useUserInfo;
|
|
if (import.meta.hot) {
|
|
import.meta.hot.accept(acceptHMRUpdate(useUserInfo, import.meta.hot))
|
|
}
|