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.

57 lines
1.5 KiB

11 months ago
import { ACCOUNT_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
account: string | null
temToken: string | null
}
export const useUserStore = defineStore({
id: 'userStore',
state: (): UserSate => {
const TOKEN = useCookie(TOKEN_KEY)
const ACCOUNT = useCookie(ACCOUNT_KEY)
const USERINFO = useCookie(USERINFO_KEY)
return {
userInfos: USERINFO.value || null,
token: TOKEN.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
},
login(token: string) {
const TOKEN = useCookie(TOKEN_KEY)
this.token = token
TOKEN.value = token
},
logout() {
const TOKEN = useCookie(TOKEN_KEY)
this.token = null
this.userInfos = {}
TOKEN.value = null
window.localStorage.clear();
},
},
})
// // 添加持久化插件到Pinia中
// pinia.use(createPersistedState())
//
// export default pinia