Files
chuanqi-qycq-web/module/web/src/router/index.js
艾贤凌 6d4a72161f inint
2026-03-16 12:05:55 +08:00

70 lines
1.7 KiB
JavaScript

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