From ed67dd905c60164796b950e735abf5eff2447f7f Mon Sep 17 00:00:00 2001 From: liufumingren Date: Mon, 23 Nov 2026 22:27:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=99=BB=E5=BD=95=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E5=90=8E=E4=B8=8D=E8=B7=B3=E8=BD=AC=EF=BC=88=E6=97=A0?= =?UTF-8?q?=E4=B8=BB=E4=BD=93=E8=B4=A6=E5=8F=B7=E9=87=8D=E5=AE=9A=E5=90=91?= =?UTF-8?q?=E6=AD=BB=E5=BE=AA=E7=8E=AF=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 路由守卫先判「无主体」再判「未匹配」:无主体时仪表盘路由未注册, 旧逻辑把 /dashboard 原样返回,触发自身重定向直至 Vue Router 中止,页面停在登录页 - 未匹配时按新路由表解析一次,仍无匹配回退到仪表盘/「关于」,避免死循环 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- frontend/src/router/index.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) 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