Files
Aether/frontend/src/router/index.ts
T

62 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-12-10 20:52:44 +08:00
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useModuleStore } from '@/stores/modules'
2025-12-10 20:52:44 +08:00
import { log } from '@/utils/logger'
import {
ensureUserLoaded,
resolveHomeRedirect,
checkAdminAccess,
checkModuleAccess
} from './guards'
import { routes } from './routes'
2025-12-10 20:52:44 +08:00
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes
})
router.beforeEach(async (to, from, next) => {
const requiresAuth = to.matched.some(record => record.meta.requiresAuth !== false)
2025-12-10 20:52:44 +08:00
try {
const authStore = useAuthStore()
const moduleStore = useModuleStore()
const isAuthenticated = await ensureUserLoaded(authStore)
2025-12-10 20:52:44 +08:00
// 首页重定向
const homeRedirect = resolveHomeRedirect(to, from, authStore)
if (homeRedirect !== null) return next(homeRedirect === '' ? undefined : homeRedirect)
// 需要认证但未认证
if (requiresAuth && !isAuthenticated) {
2025-12-10 20:52:44 +08:00
sessionStorage.setItem('redirectPath', to.fullPath)
log.debug('No valid token found, redirecting to home')
return next('/')
2025-12-10 20:52:44 +08:00
}
// 管理端检查
const requiresAdmin = to.matched.some(record => record.meta.requiresAdmin)
if (requiresAdmin) {
const adminRedirect = await checkAdminAccess(to, authStore, moduleStore)
if (adminRedirect) return next(adminRedirect)
}
// 非管理端的模块检查
if (!requiresAdmin) {
const moduleRedirect = await checkModuleAccess(to, moduleStore)
if (moduleRedirect) return next(moduleRedirect)
}
next()
2025-12-10 20:52:44 +08:00
} catch (error) {
log.error('Router guard error', error)
if (requiresAuth) {
sessionStorage.setItem('redirectPath', to.fullPath)
return next('/')
}
2025-12-10 20:52:44 +08:00
next()
}
})
export default router