// src/router/index.ts import { createRouter, createWebHistory } from 'vue-router' import type { RouteRecordRaw } from 'vue-router' import { isAuthenticated } from '@/api/pocketbase' // 路由配置 const routes: RouteRecordRaw[] = [ { path: '/login', name: 'Login', component: () => import('@/views/Login.vue'), meta: { requiresGuest: true } }, { path: '/register', name: 'Register', component: () => import('@/views/Register.vue'), meta: { requiresGuest: true } }, { path: '/', component: () => import('@/views/Layout.vue'), meta: { requiresAuth: true }, children: [ { path: '', name: 'Home', component: () => import('@/views/Home.vue') }, { path: 'group/:id', name: 'GroupView', component: () => import('@/views/GroupView.vue'), props: true }, { path: 'games', name: 'GamesLibrary', component: () => import('@/views/GamesLibrary.vue') }, { path: 'profile', name: 'Profile', component: () => import('@/views/Profile.vue') }, { path: 'settings', name: 'Settings', component: () => import('@/views/Settings.vue') } ] }, { path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('@/views/NotFound.vue') } ] // 创建路由实例 const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes }) // 路由守卫 router.beforeEach((to, _from, next) => { const authenticated = isAuthenticated() // 需要登录的页面 if (to.meta.requiresAuth && !authenticated) { next({ name: 'Login', query: { redirect: to.fullPath } }) return } // 已登录用户访问登录/注册页 if (to.meta.requiresGuest && authenticated) { next({ name: 'Home' }) return } next() }) export default router