This commit is contained in:
2025-07-21 22:30:43 +08:00
parent 0fe89e1fbb
commit 3e4f9ad660
2 changed files with 27 additions and 27 deletions

View File

@@ -1,9 +1,8 @@
import type { RouteRecordRaw, NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { adminAuth } from '@/utils/auth'
import { createRouter, createWebHistory } from 'vue-router';
import { useAuthStore } from '@/stores/auth';
import { adminAuth } from '@/utils/auth';
const routes: RouteRecordRaw[] = [
const routes = [
{
path: '/',
name: 'Home',
@@ -58,41 +57,42 @@ const routes: RouteRecordRaw[] = [
component: () => import('@/views/NotFound.vue'),
meta: { title: '页面未找到' }
}
]
];
const router = createRouter({
history: createWebHistory(),
routes
})
routes,
});
// 路由守卫
router.beforeEach(async (
to: RouteLocationNormalized,
_from: RouteLocationNormalized,
next: NavigationGuardNext
) => {
router.beforeEach((to, from, next) => {
// 设置页面标题
const title = to.meta.title as string
document.title = `${title} - AI`
const title = to.meta.title as string;
document.title = `${title} - AI`;
const authStore = useAuthStore();
// 检查是否需要用户认证
if (to.meta.requiresAuth) {
const authStore = useAuthStore()
if (!authStore.isLoggedIn) {
next('/')
return
next('/');
return;
}
}
// 检查是否需要管理员认证
if (to.meta.requiresAdminAuth) {
if (!adminAuth.isLoggedIn()) {
next('/admin/login')
return
next('/admin/login');
return;
}
}
next('/dashboard')
})
export default router
if (to.path === '/' && authStore.isLoggedIn) {
return next('/dashboard');
}
next();
});
export default router;