feat(router): 启动时强制访问初始化页面
- 新增 needInitLanding 标志位,用于控制是否需要跳转到初始化页面 - 修改路由守卫逻辑,确保应用启动时至少访问一次初始化页面 - 优化了开发环境下的路由处理流程
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import type { RouteRecordRaw } from 'vue-router'
|
||||
|
||||
let needInitLanding = true
|
||||
import { isAppInitialized } from '@/utils/config'
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
@@ -93,27 +93,36 @@ const router = createRouter({
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
console.log('路由守卫:', { to: to.path, from: from.path })
|
||||
|
||||
// 如果访问的不是初始化页面,且没有初始化标记,则重定向到初始化页面
|
||||
if (to.path !== '/initialization') {
|
||||
// 在开发环境下跳过初始化检查
|
||||
const isDev = import.meta.env.VITE_APP_ENV === 'dev'
|
||||
if (isDev) {
|
||||
console.log('开发环境,跳过初始化检查')
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
const initialized = await isAppInitialized()
|
||||
console.log('检查初始化状态:', initialized)
|
||||
|
||||
if (!initialized) {
|
||||
console.log('应用未初始化,重定向到初始化页面')
|
||||
next('/initialization')
|
||||
return
|
||||
}
|
||||
// 如果目标就是初始化页,放行并清除一次性标记,避免反复跳转
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user