文字游戏
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.7 KiB

2 months ago
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useCharacterStore } from '@/stores/character'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
redirect: '/login'
},
{
path: '/login',
name: 'login',
component: () => import('@/views/LoginView.vue'),
meta: { requiresGuest: true }
},
{
path: '/register',
name: 'register',
component: () => import('@/views/RegisterView.vue'),
meta: { requiresGuest: true }
},
{
path: '/character',
name: 'character',
component: () => import('@/views/CharacterView.vue'),
meta: { requiresAuth: true }
},
{
path: '/game',
name: 'game',
component: () => import('@/views/GameView.vue'),
meta: { requiresAuth: true }
},
{
path: '/tutorial',
name: 'tutorial',
component: () => import('@/views/TutorialView.vue'),
meta: { requiresAuth: true }
},
2 months ago
{
path: '/training',
name: 'training',
component: () => import('@/views/TrainingView.vue'),
meta: { requiresAuth: true }
},
{
path: '/daily-mission',
name: 'daily-mission',
component: () => import('@/views/DailyMissionView.vue'),
meta: { requiresAuth: true }
},
{
path: '/scrap',
name: 'scrap',
component: () => import('@/views/ScrapView.vue'),
meta: { requiresAuth: true }
},
{
path: '/bag',
name: 'bag',
component: () => import('@/views/BagView.vue'),
meta: { requiresAuth: true }
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: () => import('@/views/NotFoundView.vue')
2 months ago
}
]
})
router.beforeEach((to, _from, next) => {
const authStore = useAuthStore()
const characterStore = useCharacterStore()
authStore.initAuth()
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
next('/login')
} else if (to.meta.requiresGuest && authStore.isAuthenticated) {
// 已登录用户访问登录/注册页,跳转到角色选择或游戏页面
if (characterStore.characters.length > 0 && characterStore.currentCharacter) {
next('/game')
} else {
next('/character')
}
} else if (to.path === '/login' && authStore.isAuthenticated) {
// 访问登录页且已登录,跳转到角色选择
next('/character')
} else if (to.path === '/game' && authStore.isAuthenticated && !characterStore.currentCharacter) {
// 访问游戏页但没有选择角色,跳转到角色选择
next('/character')
} else {
next()
}
})
export default router