fix(router): 优化路由守卫避免退出登录时的无效路由请求

- 在路由守卫中添加登录状态检查,避免未登录状态下初始化路由
- 防止退出登录过程中触发路由初始化导致的token无效错误
- 只有在已登录状态下才调用initAuthRoute,提高性能
- 修复退出登录时路由守卫与清理时序的竞态条件问题
This commit is contained in:
Leo 2025-07-06 22:07:53 +08:00
parent d6f3cfe25c
commit 8166c04919

View File

@ -43,6 +43,17 @@ export function setupRouterGuard(router: Router) {
// 判断路由有无进行初始化
if (!routeStore.isInitAuthRoute) {
// 只有在已登录状态下才初始化路由,避免退出登录时的无效请求
if (!isLogin) {
// 未登录状态下,如果访问的不是登录页,重定向到登录页
if (to.name !== 'login') {
const redirect = to.name === '404' ? undefined : to.fullPath
next({ path: '/login', query: { redirect } })
return
}
}
else {
// 已登录状态下才初始化路由
try {
await routeStore.initAuthRoute()
}
@ -52,6 +63,7 @@ export function setupRouterGuard(router: Router) {
next({ path: '/login' })
return
}
}
// 动态路由加载完回到根路由
if (to.name === 'notFound' || to.name === '404') {