This commit is contained in:
oneao committed 2025-08-14 17:28:32 +08:00
1 parent d1b1aafc57
commit 0f1604c709
58 files changed
+15476 -25

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
import type { App } from "vue";
import { createPinia } from "pinia";
import { createPersistedState } from "pinia-plugin-persistedstate"; // 数据持久化
export function setupPinia(app: App) {
const store = createPinia();
store.use(createPersistedState());
app.use(store);
}
+18
View File
@@ -0,0 +1,18 @@
import { defineStore } from "pinia";
import { useRoute } from "vue-router";
import { useRouter } from "vue-router";
export const useRouterStore = defineStore("router", () => {
const route = useRoute();
const router = useRouter();
const getCurrentPath = () => {
return route.path;
};
const toPath = (path: string) => {
router.push(path);
};
return {
getCurrentPath,
toPath,
};
});
+60
View File
@@ -0,0 +1,60 @@
import { ref, computed } from "vue";
import { defineStore } from "pinia";
const initState = {
id: "",
account: "",
email: "",
name: "",
token: "",
avatar: "",
};
export const useUserStore = defineStore(
"user",
() => {
const userInfo = ref<IUserInfo>({ ...initState });
const setUserInfo = (val: IUserInfo) => {
userInfo.value = { ...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.account // 账户名
)
// userInfo.value.name // 名称
);
});
return {
userInfo,
setUserInfo,
clearUserInfo,
isLogined,
reset,
};
},
{
persist: true,
}
);