inint
This commit is contained in:
@@ -1,23 +1,69 @@
|
||||
import {createRouter, createWebHistory} from 'vue-router'
|
||||
import Index from "../views/index.vue";
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
|
||||
// 需要登录才能访问的路由名称
|
||||
const AUTH_ROUTES = ['Index', 'Withdraw']
|
||||
|
||||
// 不需要登录即可访问的路由名称(白名单)
|
||||
const PUBLIC_ROUTES = ['Login', 'LinuxdoBind', 'Agree']
|
||||
|
||||
// 示例路由配置
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'Home',
|
||||
component: Index,
|
||||
redirect: '/login'
|
||||
},
|
||||
{
|
||||
path: '/login', name: '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
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user