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.
66 lines
1.8 KiB
66 lines
1.8 KiB
import { ACCOUNT_KEY,TOKEN_ADMIN_KEY,USERINFO_KEY, TOKEN_KEY } from '@/enums/cacheEnums' |
|
import {createPinia, defineStore} from 'pinia' |
|
import { createPersistedState } from 'pinia-plugin-persistedstate' |
|
|
|
const pinia = createPinia() |
|
interface UserSate { |
|
userInfos: Record<string, any> |
|
token: string | null |
|
tokenAdmin: string | null |
|
account: string | null |
|
temToken: string | null |
|
} |
|
export const useUserStore = defineStore({ |
|
id: 'userStore', |
|
state: (): UserSate => { |
|
const TOKEN = useCookie(TOKEN_KEY) |
|
const TOKENADMIN = useCookie(TOKEN_ADMIN_KEY) |
|
const ACCOUNT = useCookie(ACCOUNT_KEY) |
|
const USERINFO = useCookie(USERINFO_KEY) |
|
return { |
|
userInfos: USERINFO.value || null, |
|
token: TOKEN.value || null, |
|
tokenAdmin: TOKENADMIN.value || null, |
|
account: ACCOUNT.value || null, |
|
temToken: null, |
|
} |
|
}, |
|
getters: { |
|
isLogin: (state) => !!state.token, |
|
userInfo: (state) => state.userInfos, |
|
}, |
|
actions: { |
|
setUserInfo(userInfos:any) { |
|
this.userInfos = userInfos |
|
const USERINFO = useCookie(USERINFO_KEY) |
|
USERINFO.value = userInfos |
|
}, |
|
setAccount(account: string) { |
|
const ACCOUNT = useCookie(ACCOUNT_KEY) |
|
this.account = account |
|
ACCOUNT.value = account |
|
}, |
|
setTokenAdmin(tokenAdmin: string) { |
|
const TOKENADMIN = useCookie(TOKEN_ADMIN_KEY) |
|
this.tokenAdmin = tokenAdmin |
|
TOKENADMIN.value = tokenAdmin |
|
}, |
|
login(token: string) { |
|
const TOKEN = useCookie(TOKEN_KEY) |
|
this.token = token |
|
TOKEN.value = token |
|
}, |
|
logout() { |
|
const TOKEN = useCookie(TOKEN_KEY) |
|
this.token = null |
|
this.tokenAdmin = null |
|
this.userInfos = {} |
|
TOKEN.value = null |
|
window.localStorage.clear(); |
|
}, |
|
}, |
|
}) |
|
// // 添加持久化插件到Pinia中 |
|
// pinia.use(createPersistedState()) |
|
// |
|
// export default pinia
|