2025-12-11 14:21:36 +08:00
|
|
|
|
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
|
|
|
|
|
import { TokenManager } from 'shared/api'
|
2025-12-12 18:17:38 +08:00
|
|
|
|
import { loadRoutesFromStorage } from './dynamicRoute'
|
2025-12-11 14:21:36 +08:00
|
|
|
|
|
2025-12-13 14:13:31 +08:00
|
|
|
|
// platform应用的动态路由会根据layout字段自动添加,不需要预定义Root布局
|
2025-12-11 14:21:36 +08:00
|
|
|
|
const routes: RouteRecordRaw[] = [
|
|
|
|
|
|
{
|
|
|
|
|
|
path: '/login',
|
|
|
|
|
|
name: 'Login',
|
2025-12-12 18:17:38 +08:00
|
|
|
|
component: () => import('@/views/public/Login/Login.vue'),
|
2025-12-11 14:21:36 +08:00
|
|
|
|
meta: {
|
|
|
|
|
|
title: '登录',
|
|
|
|
|
|
requiresAuth: false // 不需要登录
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
|
|
history: createWebHistory(),
|
|
|
|
|
|
routes
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-12-12 18:17:38 +08:00
|
|
|
|
// 标记动态路由是否已加载
|
|
|
|
|
|
let dynamicRoutesLoaded = false
|
|
|
|
|
|
|
2025-12-11 14:21:36 +08:00
|
|
|
|
// 路由守卫
|
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
|
|
// 设置页面标题
|
|
|
|
|
|
if (to.meta.title) {
|
|
|
|
|
|
document.title = `${to.meta.title} - 泰豪电源 AI 数智化平台`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否需要登录
|
|
|
|
|
|
const requiresAuth = to.meta.requiresAuth !== false // 默认需要登录
|
|
|
|
|
|
const hasToken = TokenManager.hasToken()
|
|
|
|
|
|
|
|
|
|
|
|
if (requiresAuth && !hasToken) {
|
|
|
|
|
|
// 需要登录但未登录,跳转到登录页
|
|
|
|
|
|
next({
|
|
|
|
|
|
path: '/login',
|
|
|
|
|
|
query: { redirect: to.fullPath } // 保存原始路径
|
|
|
|
|
|
})
|
2025-12-12 18:17:38 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (to.path === '/login' && hasToken) {
|
2025-12-11 14:21:36 +08:00
|
|
|
|
// 已登录但访问登录页,跳转到首页
|
2025-12-12 18:17:38 +08:00
|
|
|
|
next('/')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已登录且动态路由未加载,先加载动态路由
|
|
|
|
|
|
if (hasToken && !dynamicRoutesLoaded) {
|
|
|
|
|
|
dynamicRoutesLoaded = true
|
|
|
|
|
|
const loaded = loadRoutesFromStorage()
|
|
|
|
|
|
|
2025-12-13 15:56:12 +08:00
|
|
|
|
if (loaded) {
|
|
|
|
|
|
if (to.path === '/') {
|
|
|
|
|
|
// 访问根路径,重定向到第一个可用路由
|
|
|
|
|
|
const firstRoute = getFirstAvailableRoute()
|
|
|
|
|
|
if (firstRoute && firstRoute !== '/') {
|
|
|
|
|
|
// 只有当第一个路由不是 / 时才重定向,避免无限循环
|
|
|
|
|
|
console.log('[Platform Router] 根路径重定向到:', firstRoute)
|
|
|
|
|
|
next({ path: firstRoute, replace: true })
|
|
|
|
|
|
return
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 第一个路由就是 /,直接放行
|
|
|
|
|
|
console.log('[Platform Router] 第一个路由就是根路径,直接放行')
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 动态路由已加载,重新导航到目标路由
|
|
|
|
|
|
next({ ...to, replace: true })
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已登录且访问根路径,但动态路由已加载,重定向到第一个可用路由
|
|
|
|
|
|
if (hasToken && to.path === '/' && dynamicRoutesLoaded) {
|
|
|
|
|
|
const firstRoute = getFirstAvailableRoute()
|
|
|
|
|
|
if (firstRoute && firstRoute !== '/') {
|
|
|
|
|
|
// 只有当第一个路由不是 / 时才重定向,避免无限循环
|
|
|
|
|
|
console.log('[Platform Router] 已登录访问根路径,重定向到:', firstRoute)
|
|
|
|
|
|
next({ path: firstRoute, replace: true })
|
2025-12-12 18:17:38 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-11 14:21:36 +08:00
|
|
|
|
}
|
2025-12-12 18:17:38 +08:00
|
|
|
|
|
|
|
|
|
|
next()
|
2025-12-11 14:21:36 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-12-13 15:56:12 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取第一个可用的路由路径
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getFirstAvailableRoute(): string | null {
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log('[Platform Router] 开始获取第一个可用路由...')
|
|
|
|
|
|
|
|
|
|
|
|
const loginDomainStr = localStorage.getItem('loginDomain')
|
|
|
|
|
|
if (!loginDomainStr) {
|
|
|
|
|
|
console.warn('[Platform Router] localStorage 中没有 loginDomain')
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const loginDomain = JSON.parse(loginDomainStr)
|
|
|
|
|
|
const userViews = loginDomain.userViews || []
|
|
|
|
|
|
|
|
|
|
|
|
console.log('[Platform Router] 所有用户视图:', userViews.length)
|
|
|
|
|
|
|
|
|
|
|
|
// 过滤出 platform 服务的非 admin 视图
|
|
|
|
|
|
// 注意:不限制 type,因为首页路由可能是 type=3(路由类型)而不是 type=1(菜单类型)
|
|
|
|
|
|
const platformViews = userViews.filter((view: any) =>
|
|
|
|
|
|
view.service === 'platform' &&
|
|
|
|
|
|
!view.url?.startsWith('/admin') &&
|
|
|
|
|
|
view.url // 必须有 url 字段
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
console.log('[Platform Router] Platform 服务视图:', platformViews)
|
|
|
|
|
|
|
|
|
|
|
|
if (platformViews.length === 0) {
|
|
|
|
|
|
console.warn('[Platform Router] 没有找到 platform 服务的视图')
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 按 orderNum 排序
|
|
|
|
|
|
platformViews.sort((a: any, b: any) => (a.orderNum || 0) - (b.orderNum || 0))
|
|
|
|
|
|
|
|
|
|
|
|
const firstRoute = platformViews[0].url
|
|
|
|
|
|
console.log('[Platform Router] 第一个路由:', firstRoute, '视图:', platformViews[0].name)
|
|
|
|
|
|
|
|
|
|
|
|
return firstRoute
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[Platform Router] 获取首页路由失败:', error)
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-12 18:17:38 +08:00
|
|
|
|
// 导出动态路由生成函数
|
|
|
|
|
|
export { addDynamicRoutes, loadRoutesFromStorage } from './dynamicRoute'
|
|
|
|
|
|
|
|
|
|
|
|
// 重置动态路由加载状态(用于登录后重新加载)
|
|
|
|
|
|
export function resetDynamicRoutes() {
|
|
|
|
|
|
dynamicRoutesLoaded = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 导出路由和辅助函数
|
2025-12-11 14:21:36 +08:00
|
|
|
|
export default router
|