diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 252f2c9..da7b86a 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -67,18 +67,19 @@ router.beforeEach(async (to) => { if (!menuStore.loaded) { await menuStore.fetchMenus() } + // 已登录但无主体:强制停留在「创建主体」面板(该路由始终注册),隐藏其余菜单。 + // 必须放在下面的「未匹配」判断之前:仪表盘路由是按菜单动态注册的,无主体时并不存在, + // 先判未匹配会把 /dashboard 原样返回,形成「重定向到自身」的死循环(登录后停在登录页) + if (!auth.hasPrincipal && to.path !== '/principal/create') { + return { path: '/principal/create', replace: true } + } // 动态路由是运行期注册的,深链接刷新时早于匹配阶段: // 当前 to 尚未匹配到任何路由 → 用新的路由表重新解析当前目标; // 重新解析后仍无匹配(地址不存在或当前权限不可见)则回仪表盘,避免反复重定向 if (to.matched.length === 0) { - if (to.redirectedFrom && to.redirectedFrom.matched.length === 0) { - return { path: '/dashboard' } - } - return to.fullPath - } - // 已登录但无主体:强制停留在创建主体面板,隐藏其余菜单 - if (!auth.hasPrincipal && to.path !== '/principal/create') { - return { path: '/principal/create' } + if (router.resolve(to.fullPath).matched.length > 0) return { path: to.fullPath, replace: true } + const fallback = router.resolve('/dashboard').matched.length > 0 ? '/dashboard' : '/about' + return to.fullPath === fallback ? true : { path: fallback, replace: true } } } return true