Files
workspace/code/sllog/Sllog-User-Ui/src/store/user.ts
T
2025-06-18 16:30:01 +08:00

63 lines
1.3 KiB
TypeScript

import { defineStore } from "pinia";
import { computed, ref } from "vue";
type IUserInfo = {
id: string;
account?: string;
email?: string;
name?: string;
token?: string;
avatar?: string;
};
const initState = { id: "", account: "", email: "", name: "", token: "" };
export const useUserStore = defineStore(
"user",
() => {
const userInfo = ref<IUserInfo>({ ...initState });
const setUserInfo = (val: IUserInfo) => {
userInfo.value = val;
};
const clearUserInfo = () => {
reset();
};
// 一般没有reset需求,不需要的可以删除
const reset = () => {
userInfo.value = {
id: "",
account: "",
email: "",
name: "",
token: "",
avatar: ""
};
};
// ✅ 如果需要同时验证多个字段
const isLogined = computed(() => {
return !!(
(
userInfo.value.token && // 用户令牌
userInfo.value.id && // 用户ID
// userInfo.value.email && // 邮箱
userInfo.value.account // 账户名
)
// userInfo.value.name // 名称
);
});
return {
userInfo,
setUserInfo,
clearUserInfo,
isLogined,
reset
};
},
{
persist: true
}
);