fix(router): 修复存储失效时登出逻辑的循环依赖

将存储失效登出逻辑从 storage 模块移至路由守卫,通过标志位解耦,
消除动态 import 及其引发的循环依赖问题。
This commit is contained in:
zhangtao
2026-05-11 17:05:52 +08:00
parent 80d4f18e8b
commit 7e7a5c97cb
2 changed files with 34 additions and 19 deletions
+9
View File
@@ -19,6 +19,7 @@ import { UserAPI } from "@/api/module_system/user";
import { ApiStatus, isHttpError } from "@utils/http";
import { RouteRegistry } from "./dynamicRoutes";
import { MenuProcessor } from "./MenuProcessor";
import { checkStorageInvalidated } from "@utils/storage";
// --- 模块级单例与守卫状态 ---
@@ -119,6 +120,14 @@ async function handleRouteGuard(
NProgress.start();
}
// 检查存储是否已失效(storage/index.ts 检测到异常时标记)
if (checkStorageInvalidated()) {
console.info("[RouteGuard] 检测到存储已失效,执行登出");
await userStore.logout();
next({ name: "Login", replace: true });
return;
}
if (!handleLoginStatus(to, userStore, next)) {
return;
}
+25 -19
View File
@@ -1,11 +1,22 @@
/** LocalStorage compatibility checks + recovery helpers.
*
* `import "@/router"` / user store
* router guards navigation locales storage
* locales storage StorageKeyManager TDZ
* `performSystemLogout` import
*
*
*/
/** 存储是否已失效(由路由守卫检查并处理登出) */
let invalidated = false;
/** 标记存储已失效(供路由守卫检查) */
export function markStorageInvalidated(): void {
invalidated = true;
}
/** 检查存储是否已失效 */
export function checkStorageInvalidated(): boolean {
return invalidated;
}
/** Storage config + versioned key helpers. */
export class StorageConfig {
/** 当前应用版本 */
@@ -173,24 +184,19 @@ class StorageCompatibilityManager {
}
/**
*
*
*/
private performSystemLogout(): void {
setTimeout(() => {
void (async () => {
try {
localStorage.clear();
const [{ router }, { useUserStore }] = await Promise.all([
import("@/router"),
import("@stores/modules/user.store"),
]);
useUserStore().logout();
await router.push({ name: "Login" });
console.info("[Storage] 已执行系统登出");
} catch (error) {
console.error("[Storage] 系统登出失败:", error);
}
})();
try {
localStorage.clear();
markStorageInvalidated();
console.info("[Storage] 已标记存储失效,跳转到登录页");
// 强制刷新触发路由守卫中的存储检查
window.location.href = window.location.origin + "/login";
} catch (error) {
console.error("[Storage] 标记存储失效失败:", error);
}
}, StorageConfig.LOGOUT_DELAY);
}