Files
schisandra-cloud-storage-fr…/src/store/modules/user.ts
2024-03-19 22:51:10 +08:00

54 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {defineStore} from 'pinia'
// 加载storage模块获取token存储token
import {getItem, setItem} from "@/utils/storage/storage";
const TOKEN_KEY: String = "X-Token";
export const useAuthStore = defineStore('user', () => {
const user = ref<any>({
token: getItem(TOKEN_KEY) ? getItem(TOKEN_KEY) : null,
userId: Number,
LOADING: false,
userInfo: Object
})
function setUser(data: any) {
user.value = data
setItem(TOKEN_KEY, user.value.token);
}
function getUser() {
return user.value
}
function clearUser() {
user.value = {
token: null,
userId: null,
isLogin: false,
userInfo: {}
}
}
function showLoading() {
user.value.LOADING = true;
}
function hideLoading() {
user.value.LOADING = false;
}
return {
user,
setUser,
getUser,
clearUser,
showLoading,
hideLoading
}
}, {
// 开启数据持久化
persist: true
}
)