import { createRouter, createWebHistory } from 'vue-router' import { pinia } from '@/stores' import { useAuthStore } from '@/stores/auth' import DashboardLayout from '@/layouts/DashboardLayout.vue' import LoginView from '@/views/LoginView.vue' import RegisterView from '@/views/RegisterView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ // 认证页面(不使用仪表盘布局) { path: '/login', name: 'login', component: LoginView, meta: { guestOnly: true }, }, { path: '/register', name: 'register', component: RegisterView, meta: { guestOnly: true }, }, // 仪表盘内部页面(使用统一侧边栏布局) { path: '/', component: DashboardLayout, meta: { requiresAuth: true }, children: [ { path: '', name: 'dashboard', component: () => import('@/views/DashboardView.vue'), }, { path: 'revisions', name: 'revisions', component: () => import('@/views/RevisionsView.vue'), }, { path: 'topics', name: 'topics', component: () => import('@/views/TopicsView.vue'), }, { path: 'delivery', name: 'delivery', component: () => import('@/views/DeliveryView.vue'), }, { path: 'search', name: 'search', component: () => import('@/views/SearchView.vue'), }, ], }, ], }) router.beforeEach((to) => { const authStore = useAuthStore(pinia) authStore.restore() if (to.meta.requiresAuth && !authStore.isAuthenticated) { return { name: 'login', query: { redirect: to.fullPath } } } if (to.meta.guestOnly && authStore.isAuthenticated) { return { name: 'dashboard' } } return true }) export default router