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