Files
AUTO-MAS-test/frontend/src/router/index.ts
AoXuan b30073cb80 feat(router): 启动时强制访问初始化页面
- 新增 needInitLanding 标志位,用于控制是否需要跳转到初始化页面
- 修改路由守卫逻辑,确保应用启动时至少访问一次初始化页面
- 优化了开发环境下的路由处理流程
2025-08-15 20:32:11 +08:00

129 lines
3.2 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
let needInitLanding = true
import { isAppInitialized } from '@/utils/config'
const routes: RouteRecordRaw[] = [
{
path: '/',
redirect: () => {
return '/initialization'
},
},
{
path: '/initialization',
name: 'Initialization',
component: () => import('../views/Initialization.vue'),
meta: { title: '初始化' },
},
{
path: '/home',
name: 'Home',
component: () => import('../views/Home.vue'),
meta: { title: '主页' },
},
{
path: '/scripts',
name: 'Scripts',
component: () => import('../views/Scripts.vue'),
meta: { title: '脚本管理' },
},
{
path: '/scripts/:id/edit',
name: 'ScriptEdit',
component: () => import('../views/ScriptEdit.vue'),
meta: { title: '编辑脚本' },
},
{
path: '/scripts/:scriptId/users/add',
name: 'UserAdd',
component: () => import('../views/UserEdit.vue'),
meta: { title: '添加用户' },
},
{
path: '/scripts/:scriptId/users/:userId/edit',
name: 'UserEdit',
component: () => import('../views/UserEdit.vue'),
meta: { title: '编辑用户' },
},
{
path: '/plans',
name: 'Plans',
component: () => import('../views/Plans.vue'),
meta: { title: '计划管理' },
},
{
path: '/queue',
name: 'Queue',
component: () => import('../views/Queue.vue'),
meta: { title: '调度队列' },
},
{
path: '/scheduler',
name: 'Scheduler',
component: () => import('../views/Scheduler.vue'),
meta: { title: '调度中心' },
},
{
path: '/history',
name: 'History',
component: () => import('../views/History.vue'),
meta: { title: '历史记录' },
},
{
path: '/settings',
name: 'Settings',
component: () => import('../views/Settings.vue'),
meta: { title: '设置' },
},
{
path: '/logs',
name: 'Logs',
component: () => import('../views/Logs.vue'),
meta: { title: '系统日志' },
},
]
const router = createRouter({
history: createWebHistory(),
routes,
})
// 添加路由守卫,确保在生产环境中也能正确进入初始化页面
router.beforeEach(async (to, from, next) => {
console.log('路由守卫:', { to: to.path, from: from.path })
// 如果目标就是初始化页,放行并清除一次性标记,避免反复跳转
if (to.path === '/initialization') {
needInitLanding = false
next()
return
}
// (可选)开发环境跳过检查,可按需恢复
const isDev = import.meta.env.VITE_APP_ENV === 'dev'
if (isDev) return next()
// 先按原逻辑:未初始化 => 强制进入初始化
const initialized = await isAppInitialized()
console.log('检查初始化状态:', initialized)
if (!initialized) {
needInitLanding = false // 以免重复重定向
next('/initialization')
return
}
// 已初始化:如果是“本次启动的第一次进入”,也先去初始化页一次
if (needInitLanding) {
needInitLanding = false
next({ path: '/initialization', query: { redirect: to.fullPath } })
return
}
// 其他情况正常放行
next()
})
export default router