|
|
|
|
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: '/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: '/:pathMatch(.*)*',
|
|
|
|
|
name: 'NotFound',
|
|
|
|
|
component: () => import('@/views/NotFoundView.vue')
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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
|