This commit is contained in:
2026-01-08 16:40:02 +08:00
parent 16e714730d
commit 54599f28d1

View File

@@ -1,17 +1,6 @@
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { h } from 'vue'
import { loadRoutesFromStorage } from './dynamicRoute'
// 动态导入 TokenManager避免顶层 import 阻塞)
let _TokenManager: any = null
async function getTokenManager() {
if (!_TokenManager) {
const module = await import('shared/api')
_TokenManager = module.TokenManager
}
return _TokenManager
}
// 同步检查 token用于非异步场景
function hasTokenSync(): boolean {
try {
@@ -23,6 +12,7 @@ function hasTokenSync(): boolean {
}
// platform应用的动态路由会根据layout字段自动添加不需要预定义Root布局
// 参考 workcase 的方式:不预定义 404 路由,避免过早匹配
const routes: RouteRecordRaw[] = [
{
path: '/login',
@@ -32,24 +22,6 @@ const routes: RouteRecordRaw[] = [
title: '登录',
requiresAuth: false // 不需要登录
}
},
{
path: '/',
name: 'Root',
component: () => Promise.resolve({ default: { render: () => h('div', '加载中...') } }),
meta: {
title: '首页',
requiresAuth: true // 需要登录
}
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: () => Promise.resolve({ default: { render: () => h('div', { style: { padding: '20px', textAlign: 'center' } }, '404 - 页面未找到') } }),
meta: {
title: '404',
requiresAuth: false // 不需要登录
}
}
]
@@ -61,8 +33,14 @@ const router = createRouter({
// 标记动态路由是否已加载
let dynamicRoutesLoaded = false
// 路由守卫(使用 async
// 路由守卫
router.beforeEach(async (to, from, next) => {
console.log('[Platform Router] 路由守卫触发:', {
to: to.path,
from: from.path,
dynamicRoutesLoaded
})
// 设置页面标题
if (to.meta.title) {
document.title = `${to.meta.title} - 泰豪电源 AI 数智化平台`
@@ -89,37 +67,17 @@ router.beforeEach(async (to, from, next) => {
// 如果已登录且动态路由未加载,先加载动态路由
if (hasToken && !dynamicRoutesLoaded) {
console.log('[Platform Router] 开始加载动态路由...')
dynamicRoutesLoaded = true // 先设置标志,避免重复加载
try {
console.log('[Platform Router] 开始加载动态路由...')
const loaded = await loadRoutesFromStorage()
if (loaded) {
dynamicRoutesLoaded = true // ✅ 加载成功后才设置标志
console.log('[Platform Router] 动态路由加载成功')
console.log('[Platform Router] 动态路由加载结果:', loaded)
// 添加 404 路由(确保它是最后匹配的)
if (!notFoundRouteAdded) {
router.addRoute({
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: () => Promise.resolve({
default: {
render: () => h('div', {
style: {
padding: '20px',
textAlign: 'center'
}
}, '404 - 页面未找到')
}
}),
meta: {
title: '404',
requiresAuth: false
}
})
notFoundRouteAdded = true
console.log('[Platform Router] 404 路由已添加')
}
if (loaded) {
console.log('[Platform Router] 动态路由加载成功')
console.log('[Platform Router] 所有路由:', router.getRoutes().map(r => r.path))
if (to.path === '/') {
// 访问根路径,重定向到第一个可用路由
@@ -128,26 +86,20 @@ router.beforeEach(async (to, from, next) => {
console.log('[Platform Router] 根路径重定向到:', firstRoute)
next({ path: firstRoute, replace: true })
return
} else {
console.log('[Platform Router] 第一个路由就是根路径,直接放行')
}
} else {
// 动态路由加载,重新导航到目标路由
console.log('[Platform Router] 重新导航到目标路由:', to.path)
// 动态路由加载成功,重新导航以匹配新添加的路由
console.log('[Platform Router] 动态路由加载成功,重新导航到:', to.path)
next({ ...to, replace: true })
return
}
} else {
console.warn('[Platform Router] 动态路由加载失败,未返回有效数据')
// ✅ 加载失败也要调用next()继续导航
next()
return
console.warn('[Platform Router] 动态路由加载失败')
dynamicRoutesLoaded = false // 重置标志,允许下次重试
}
} catch (error) {
console.error('[Platform Router] 加载动态路由失败:', error)
// ✅ 加载失败不设置标志下次还会尝试加载但要调用next()继续导航
next()
return
dynamicRoutesLoaded = false // 重置标志,允许下次重试
}
}
@@ -161,6 +113,7 @@ router.beforeEach(async (to, from, next) => {
}
}
console.log('[Platform Router] 继续正常导航')
next()
})
@@ -218,5 +171,4 @@ export function resetDynamicRoutes() {
dynamicRoutesLoaded = false
}
// 导出路由和辅助函数
export default router