fix(auth): 优化登录重定向异常处理,防止重定向失败

This commit is contained in:
Ray.Hao
2025-11-04 10:05:03 +08:00
parent 6cbfa08bae
commit 1e90f234b8

View File

@@ -50,6 +50,10 @@ export const AuthStorage = {
export function hasPerm(value: string | string[], type: "button" | "role" = "button"): boolean { export function hasPerm(value: string | string[], type: "button" | "role" = "button"): boolean {
const { roles, perms } = useUserStoreHook().userInfo; const { roles, perms } = useUserStoreHook().userInfo;
if (!roles || !perms) {
return false;
}
// 超级管理员拥有所有权限 // 超级管理员拥有所有权限
if (type === "button" && roles.includes(ROLE_ROOT)) { if (type === "button" && roles.includes(ROLE_ROOT)) {
return true; return true;
@@ -65,7 +69,6 @@ export function hasPerm(value: string | string[], type: "button" | "role" = "but
* 重定向到登录页面 * 重定向到登录页面
*/ */
export async function redirectToLogin(message: string = "请重新登录"): Promise<void> { export async function redirectToLogin(message: string = "请重新登录"): Promise<void> {
try {
ElNotification({ ElNotification({
title: "提示", title: "提示",
message, message,
@@ -75,10 +78,13 @@ export async function redirectToLogin(message: string = "请重新登录"): Prom
await useUserStoreHook().resetAllState(); await useUserStoreHook().resetAllState();
try {
// 跳转到登录页,保留当前路由用于登录后跳转 // 跳转到登录页,保留当前路由用于登录后跳转
const currentPath = router.currentRoute.value.fullPath; const currentPath = router.currentRoute.value.fullPath;
await router.push(`/login?redirect=${encodeURIComponent(currentPath)}`); await router.push(`/login?redirect=${encodeURIComponent(currentPath)}`);
} catch (error) { } catch (error) {
console.error("Redirect to login error:", error); console.error("Redirect to login error:", error);
// 强制跳转,即使路由重定向失败
window.location.href = "/login";
} }
} }