import { createRouter, createWebHistory } from 'vue-router' // 需要登录才能访问的路由名称 const AUTH_ROUTES = ['Index', 'Withdraw'] // 不需要登录即可访问的路由名称(白名单) const PUBLIC_ROUTES = ['Login', 'LinuxdoBind', 'Agree'] const routes = [ { path: '/', redirect: '/login' }, { path: '/login', name: 'Login', component: () => import('@/views/login.vue') }, { path: '/linuxdo-bind', name: 'LinuxdoBind', component: () => import('@/views/linuxdo-bind.vue') }, { path: '/index', name: 'Index', component: () => import('@/views/index.vue'), meta: { requiresAuth: true } }, { path: '/play', name: 'Play', redirect: '/index', }, { path: '/agree', name: 'Agree', component: () => import('@/views/agree.vue') }, { path: '/withdraw', name: 'Withdraw', component: () => import('@/views/withdraw.vue'), meta: { requiresAuth: true } }, ] const router = createRouter({ history: createWebHistory(), routes, }) // ─── 全局路由守卫 ───────────────────────────────────────────────────────────── router.beforeEach((to, from, next) => { const token = sessionStorage.getItem('CQ-TOKEN') const isLoggedIn = !!token if (to.meta?.requiresAuth && !isLoggedIn) { // 需要登录但未登录 → 跳转登录页 next({ path: '/login', query: { redirect: to.fullPath } }) } else if (to.name === 'Login' && isLoggedIn) { // 已登录访问登录页 → 跳转游戏主页 next({ path: '/index' }) } else { next() } }) export default router