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 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