import { createRouter, createWebHistory } from 'vue-router' const routes = [ { path: '/', name: 'Home', component: () => import('@/views/Home.vue'), meta: { title: '情绪博物馆 - 首页' } }, { path: '/test', name: 'Test', component: () => import('@/views/HomeTest.vue'), meta: { title: '情绪博物馆 - 测试页面' } }, { path: '/chat', name: 'Chat', component: () => import('@/views/ChatComplete.vue'), meta: { title: 'AI对话 - 情绪博物馆' } }, { path: '/history', name: 'History', component: () => import('@/views/HistorySimple.vue'), meta: { title: '对话历史 - 情绪博物馆' } }, { path: '/analysis', name: 'Analysis', component: () => import('@/views/AnalysisSimple.vue'), meta: { title: '情绪分析 - 情绪博物馆' } }, { path: '/login', name: 'Login', component: () => import('@/views/Login.vue'), meta: { title: '登录注册 - 情绪博物馆' } } ] const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes }) // 路由守卫 router.beforeEach((to, from, next) => { // 设置页面标题 if (to.meta.title) { document.title = to.meta.title } // 检查是否需要认证 const requiresAuth = to.meta.requiresAuth const token = localStorage.getItem('token') if (requiresAuth && !token) { // 需要认证但没有token,跳转到登录页 next({ path: '/login', query: { redirect: to.fullPath } }) } else if (to.path === '/login' && token) { // 已登录用户访问登录页,跳转到首页 next('/') } else { next() } }) export default router