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