feat(router): 增强路由守卫网络错误处理机制
- 路由守卫中添加网络错误智能识别逻辑 - 网络错误时自动清除过期认证信息防止无效状态 - 优化路由初始化失败时的错误处理流程 - 简化动态路由获取异常处理,移除冗余日志 - 统一网络错误检测标准,包含timeout和ERR_NETWORK等
This commit is contained in:
parent
65af9b3a10
commit
849ff71393
@ -59,6 +59,22 @@ export function setupRouterGuard(router: Router) {
|
|||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error('路由初始化失败:', 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' })
|
next({ path: '/login' })
|
||||||
return
|
return
|
||||||
|
|||||||
@ -61,21 +61,24 @@ export const useRouteStore = defineStore('route-store', {
|
|||||||
const { isSuccess, data } = await fetchUserRoutes()
|
const { isSuccess, data } = await fetchUserRoutes()
|
||||||
|
|
||||||
if (isSuccess && data) {
|
if (isSuccess && data) {
|
||||||
// 将动态路由添加到静态路由中
|
|
||||||
const dynamicRoutes = Array.isArray(data) ? data : []
|
const dynamicRoutes = Array.isArray(data) ? data : []
|
||||||
console.warn('成功获取动态路由:', dynamicRoutes.length, '个')
|
return [...allRoutes, ...dynamicRoutes]
|
||||||
|
|
||||||
// 保持动态路由的原始ID关系,不需要修改ID,因为静态路由和动态路由可以共存
|
|
||||||
const processedDynamicRoutes = dynamicRoutes
|
|
||||||
|
|
||||||
return [...allRoutes, ...processedDynamicRoutes]
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.warn('动态路由获取失败,只使用静态路由')
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
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.rowRoutes = [...staticRouteData, ...transformedDynamicRoutes]
|
||||||
this.cacheRoutes = generateCacheRoutes(this.rowRoutes)
|
this.cacheRoutes = generateCacheRoutes(this.rowRoutes)
|
||||||
|
|
||||||
console.warn('混合路由模式 - 静态路由:', staticRouteData.length, '动态路由:', transformedDynamicRoutes.length)
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// 纯静态路由模式
|
// 纯静态路由模式
|
||||||
this.rowRoutes = routeData as AppRoute.RowRoute[]
|
this.rowRoutes = routeData as AppRoute.RowRoute[]
|
||||||
this.cacheRoutes = generateCacheRoutes(this.rowRoutes)
|
this.cacheRoutes = generateCacheRoutes(this.rowRoutes)
|
||||||
console.warn('静态路由模式 - 路由数量:', this.rowRoutes.length)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate actual route and insert
|
// Generate actual route and insert
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user