fix: 🐛 混合布局左侧菜单丢失问题修复
closed #ICEVSD
This commit is contained in:
@@ -14,16 +14,12 @@ export function setupPermission() {
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
NProgress.start();
|
||||
console.log("🚀 Route guard triggered:", { to: to.path, from: from.path });
|
||||
|
||||
const isLoggedIn = Auth.isLoggedIn();
|
||||
|
||||
if (isLoggedIn) {
|
||||
console.log("✅ User is logged in");
|
||||
|
||||
// 如果已登录但访问登录页,重定向到首页
|
||||
if (to.path === "/login") {
|
||||
console.log("🔄 Redirecting from login to home");
|
||||
next({ path: "/" });
|
||||
return;
|
||||
}
|
||||
@@ -44,8 +40,7 @@ export function setupPermission() {
|
||||
});
|
||||
|
||||
// 后置守卫,确保进度条关闭
|
||||
router.afterEach((to, from) => {
|
||||
console.log("✅ Route navigation completed:", { to: to.path, from: from.path });
|
||||
router.afterEach(() => {
|
||||
NProgress.done();
|
||||
});
|
||||
}
|
||||
@@ -64,14 +59,11 @@ async function handleAuthenticatedUser(
|
||||
try {
|
||||
// 检查用户信息是否存在
|
||||
if (!userStore.userInfo.username) {
|
||||
console.log("🔄 User info not found, fetching...");
|
||||
await userStore.getUserInfo();
|
||||
}
|
||||
|
||||
// 检查路由是否已生成
|
||||
if (!permissionStore.routesLoaded) {
|
||||
console.log("🔄 Routes not loaded, generating...");
|
||||
|
||||
// 防止重复生成路由
|
||||
if (isGeneratingRoutes) {
|
||||
console.log("⏳ Routes already generating, waiting...");
|
||||
@@ -82,14 +74,12 @@ async function handleAuthenticatedUser(
|
||||
}
|
||||
|
||||
// 路由生成完成后,重新导航到目标路由
|
||||
console.log("🔄 Routes generated, redirecting to:", to.path);
|
||||
next({ ...to, replace: true });
|
||||
return;
|
||||
}
|
||||
|
||||
// 路由已加载,检查路由是否存在
|
||||
if (to.matched.length === 0) {
|
||||
console.log("❌ Route not found, redirecting to 404");
|
||||
next("/404");
|
||||
return;
|
||||
}
|
||||
@@ -100,7 +90,6 @@ async function handleAuthenticatedUser(
|
||||
to.meta.title = title;
|
||||
}
|
||||
|
||||
console.log("✅ Route access granted:", to.path);
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error("❌ Route guard error:", error);
|
||||
@@ -117,15 +106,12 @@ async function generateAndAddRoutes(permissionStore: any) {
|
||||
isGeneratingRoutes = true;
|
||||
|
||||
try {
|
||||
console.log("🔧 Generating dynamic routes...");
|
||||
const dynamicRoutes = await permissionStore.generateRoutes();
|
||||
|
||||
// 添加路由到路由器
|
||||
dynamicRoutes.forEach((route: RouteRecordRaw) => {
|
||||
router.addRoute(route);
|
||||
});
|
||||
|
||||
console.log("✅ All dynamic routes generated and added");
|
||||
} finally {
|
||||
isGeneratingRoutes = false;
|
||||
}
|
||||
@@ -141,12 +127,11 @@ async function waitForRoutesGeneration(permissionStore: any): Promise<void> {
|
||||
clearInterval(checkInterval);
|
||||
resolve();
|
||||
}
|
||||
}, 50); // 每50ms检查一次
|
||||
}, 50);
|
||||
|
||||
// 超时保护,最多等待5秒
|
||||
setTimeout(() => {
|
||||
clearInterval(checkInterval);
|
||||
console.warn("⚠️ Routes generation timeout");
|
||||
resolve();
|
||||
}, 5000);
|
||||
});
|
||||
@@ -176,7 +161,6 @@ function redirectToLogin(to: RouteLocationNormalized, next: NavigationGuardNext)
|
||||
const queryString = params.toString();
|
||||
const redirect = queryString ? `${to.path}?${queryString}` : to.path;
|
||||
|
||||
console.log("🔄 Redirecting to login with redirect:", redirect);
|
||||
next(`/login?redirect=${encodeURIComponent(redirect)}`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user