import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'; import { useAuthStore } from '../stores/auth'; const routes: RouteRecordRaw[] = [ { path: '/', name: 'dashboard', component: () => import('../features/dashboard/pages/DashboardPage.vue'), meta: { title: '仪表盘', requiresAuth: true } }, { path: '/transactions', name: 'transactions', component: () => import('../features/transactions/pages/TransactionsPage.vue'), meta: { title: '交易记录', requiresAuth: true } }, { path: '/analysis', name: 'analysis', component: () => import('../features/analysis/pages/AnalysisPage.vue'), meta: { title: 'AI 智能分析', requiresAuth: true } }, { path: '/settings', name: 'settings', component: () => import('../features/settings/pages/SettingsPage.vue'), meta: { title: '设置', requiresAuth: true } }, { path: '/auth', component: () => import('../features/auth/pages/AuthLayout.vue'), children: [ { path: 'login', name: 'login', component: () => import('../features/auth/pages/LoginPage.vue'), meta: { title: '登录' } }, { path: 'register', name: 'register', component: () => import('../features/auth/pages/RegisterPage.vue'), meta: { title: '注册' } }, { path: 'forgot-password', name: 'forgot-password', component: () => import('../features/auth/pages/ForgotPasswordPage.vue'), meta: { title: '找回密码' } } ] } ]; const router = createRouter({ history: createWebHistory(), routes }); router.beforeEach((to, _from, next) => { const authStore = useAuthStore(); if (to.meta.requiresAuth && !authStore.isAuthenticated) { return next({ name: 'login', query: to.fullPath === '/' ? undefined : { redirect: to.fullPath } }); } if (authStore.isAuthenticated && to.name && ['login', 'register', 'forgot-password'].includes(to.name.toString())) { return next({ name: 'dashboard' }); } return next(); }); router.afterEach((to) => { if (to.meta.title) { document.title = `AI 记账 · ${to.meta.title}`; } else { document.title = 'AI 记账'; } }); export default router;