修复登录成功后不跳转(无主体账号重定向死循环)

- 路由守卫先判「无主体」再判「未匹配」:无主体时仪表盘路由未注册,
  旧逻辑把 /dashboard 原样返回,触发自身重定向直至 Vue Router 中止,页面停在登录页
- 未匹配时按新路由表解析一次,仍无匹配回退到仪表盘/「关于」,避免死循环

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-23 22:27:25 +08:00
parent 78f07651f1
commit ed67dd905c

View File

@ -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