mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-20 20:39:55 +00:00
fix(router): 修复存储失效时登出逻辑的循环依赖
将存储失效登出逻辑从 storage 模块移至路由守卫,通过标志位解耦, 消除动态 import 及其引发的循环依赖问题。
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user