diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 8aa4035b..3ce21e58 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -65,16 +65,26 @@ export function hasPerm(value: string | string[], type: "button" | "role" = "but : value.some((perm) => auths.includes(perm)); } +let redirectingToLogin = false; + /** * 重定向到登录页面 */ -export async function redirectToLogin(message: string = "请重新登录"): Promise { - ElNotification({ - title: "提示", - message, - type: "warning", - duration: 3000, - }); +export async function redirectToLogin( + message: string = "请重新登录", + notify: boolean = true +): Promise { + if (redirectingToLogin) return; + redirectingToLogin = true; + + if (notify) { + ElNotification({ + title: "提示", + message, + type: "warning", + duration: 3000, + }); + } await useUserStoreHook().resetAllState(); @@ -86,5 +96,7 @@ export async function redirectToLogin(message: string = "请重新登录"): Prom console.error("Redirect to login error:", error); // 强制跳转,即使路由重定向失败 window.location.href = "/login"; + } finally { + redirectingToLogin = false; } } diff --git a/src/utils/request.ts b/src/utils/request.ts index 574b18dc..33cea384 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -90,8 +90,8 @@ http.interceptors.response.use( // Refresh token 失效:无法续期,跳转登录 if (code === ApiCodeEnum.REFRESH_TOKEN_INVALID) { - await redirectToLogin("登录已过期,请重新登录"); - return Promise.reject(new Error(msg || "Token Invalid")); + await redirectToLogin("登录已过期,请重新登录", false); + return Promise.reject(new Error("Token Invalid")); } // 权限不足:刷新权限快照后提示