From 849ff713930351a94647bd88c9a1c9c7d1810d71 Mon Sep 17 00:00:00 2001 From: Leo <98382335+gaoziman@users.noreply.github.com> Date: Wed, 9 Jul 2025 14:50:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(router):=20=E5=A2=9E=E5=BC=BA=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=AE=88=E5=8D=AB=E7=BD=91=E7=BB=9C=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 路由守卫中添加网络错误智能识别逻辑 - 网络错误时自动清除过期认证信息防止无效状态 - 优化路由初始化失败时的错误处理流程 - 简化动态路由获取异常处理,移除冗余日志 - 统一网络错误检测标准,包含timeout和ERR_NETWORK等 --- src/router/guard.ts | 16 ++++++++++++++++ src/store/router/index.ts | 28 ++++++++++++++-------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/router/guard.ts b/src/router/guard.ts index 120ab81..18db121 100644 --- a/src/router/guard.ts +++ b/src/router/guard.ts @@ -59,6 +59,22 @@ export function setupRouterGuard(router: Router) { } catch (error) { console.error('路由初始化失败:', error) + + // 检查是否是网络错误 + const isNetworkError = !navigator.onLine + || (error instanceof Error && ( + error.message.includes('网络') + || error.message.includes('Network') + || error.message.includes('fetch') + || error.message.includes('timeout') + )) + + if (isNetworkError) { + // 网络错误,清除认证信息并重定向到登录页 + local.remove('accessToken') + local.remove('refreshToken') + } + // 路由初始化失败,重定向到登录页 next({ path: '/login' }) return diff --git a/src/store/router/index.ts b/src/store/router/index.ts index faf1068..51ea39d 100644 --- a/src/store/router/index.ts +++ b/src/store/router/index.ts @@ -61,21 +61,24 @@ export const useRouteStore = defineStore('route-store', { const { isSuccess, data } = await fetchUserRoutes() if (isSuccess && data) { - // 将动态路由添加到静态路由中 const dynamicRoutes = Array.isArray(data) ? data : [] - console.warn('成功获取动态路由:', dynamicRoutes.length, '个') - - // 保持动态路由的原始ID关系,不需要修改ID,因为静态路由和动态路由可以共存 - const processedDynamicRoutes = dynamicRoutes - - return [...allRoutes, ...processedDynamicRoutes] - } - else { - console.warn('动态路由获取失败,只使用静态路由') + return [...allRoutes, ...dynamicRoutes] } } catch (error) { - console.error('动态路由获取异常,只使用静态路由:', error) + // 检查是否是网络错误 + const isNetworkError = !navigator.onLine + || (error instanceof Error && ( + error.message.includes('网络') + || error.message.includes('Network') + || error.message.includes('fetch') + || error.message.includes('timeout') + || error.message.includes('ERR_NETWORK') + )) + + if (isNetworkError) { + throw new Error('网络连接失败,请检查网络状态') + } } } @@ -124,14 +127,11 @@ export const useRouteStore = defineStore('route-store', { // 合并静态路由和转换后的动态路由 this.rowRoutes = [...staticRouteData, ...transformedDynamicRoutes] this.cacheRoutes = generateCacheRoutes(this.rowRoutes) - - console.warn('混合路由模式 - 静态路由:', staticRouteData.length, '动态路由:', transformedDynamicRoutes.length) } else { // 纯静态路由模式 this.rowRoutes = routeData as AppRoute.RowRoute[] this.cacheRoutes = generateCacheRoutes(this.rowRoutes) - console.warn('静态路由模式 - 路由数量:', this.rowRoutes.length) } // Generate actual route and insert