Xing Jinwen | ff16b1e | 2025-06-05 00:29:26 +0800 | [diff] [blame] | 1 | import { createRouter, createWebHistory } from 'vue-router'
|
| 2 | import { ElMessage } from 'element-plus'
|
| 3 | import store from '@/store'
|
| 4 |
|
| 5 | // 路由组件
|
| 6 | import LoginView from '@/views/auth/LoginView.vue'
|
| 7 | import RegisterView from '@/views/auth/RegisterView.vue'
|
| 8 | import HomeView from '@/views/HomeView.vue'
|
| 9 |
|
| 10 | const routes = [
|
| 11 | {
|
| 12 | path: '/',
|
| 13 | redirect: '/login'
|
| 14 | },
|
| 15 | {
|
| 16 | path: '/login',
|
| 17 | name: 'Login',
|
| 18 | component: LoginView,
|
| 19 | meta: {
|
| 20 | title: 'PT Tracker - 登录',
|
| 21 | requiresGuest: true
|
| 22 | }
|
| 23 | },
|
| 24 | {
|
| 25 | path: '/register',
|
| 26 | name: 'Register',
|
| 27 | component: RegisterView,
|
| 28 | meta: {
|
| 29 | title: 'PT Tracker - 注册',
|
| 30 | requiresGuest: true
|
| 31 | }
|
| 32 | },
|
| 33 | {
|
| 34 | path: '/home',
|
| 35 | name: 'Home',
|
| 36 | component: HomeView,
|
| 37 | meta: {
|
| 38 | title: 'PT Tracker - 首页',
|
| 39 | requiresAuth: true
|
| 40 | }
|
| 41 | },
|
| 42 | {
|
| 43 | path: '/profile',
|
| 44 | name: 'Profile',
|
| 45 | component: () => import('@/views/auth/ProfileView.vue'),
|
| 46 | meta: {
|
| 47 | title: 'PT Tracker - 个人资料',
|
| 48 | requiresAuth: true
|
| 49 | }
|
| 50 | },
|
| 51 | // 种子相关路由
|
| 52 | {
|
| 53 | path: '/torrents',
|
| 54 | name: 'Torrents',
|
| 55 | component: () => import('@/views/torrent/TorrentsView.vue'),
|
| 56 | meta: {
|
| 57 | title: 'PT Tracker - 种子浏览',
|
| 58 | requiresAuth: true
|
| 59 | }
|
| 60 | },
|
| 61 | {
|
| 62 | path: '/upload',
|
| 63 | name: 'Upload',
|
| 64 | component: () => import('@/views/torrent/UploadView.vue'),
|
| 65 | meta: {
|
| 66 | title: 'PT Tracker - 上传种子',
|
| 67 | requiresAuth: true
|
| 68 | }
|
| 69 | },
|
| 70 | {
|
| 71 | path: '/torrent/:id',
|
| 72 | name: 'TorrentDetail',
|
| 73 | component: () => import('@/views/torrent/TorrentDetailView.vue'),
|
| 74 | meta: {
|
| 75 | title: 'PT Tracker - 种子详情',
|
| 76 | requiresAuth: true
|
| 77 | }
|
| 78 | },
|
| 79 | // 论坛相关路由
|
| 80 | {
|
| 81 | path: '/forum',
|
| 82 | name: 'Forum',
|
| 83 | component: () => import('@/views/forum/ForumView.vue'),
|
| 84 | meta: {
|
| 85 | title: 'PT Tracker - 论坛',
|
| 86 | requiresAuth: true
|
| 87 | }
|
| 88 | },
|
| 89 | {
|
| 90 | path: '/forum/section/:id',
|
| 91 | name: 'ForumSection',
|
| 92 | component: () => import('@/views/forum/ForumSectionView.vue'),
|
| 93 | meta: {
|
| 94 | title: 'PT Tracker - 版块',
|
| 95 | requiresAuth: true
|
| 96 | }
|
| 97 | },
|
| 98 | {
|
| 99 | path: '/forum/topic/:id',
|
| 100 | name: 'ForumTopic',
|
| 101 | component: () => import('@/views/forum/ForumTopicView.vue'),
|
| 102 | meta: {
|
| 103 | title: 'PT Tracker - 主题详情',
|
| 104 | requiresAuth: true
|
| 105 | }
|
| 106 | },
|
| 107 | // 404页面
|
| 108 | {
|
| 109 | path: '/:pathMatch(.*)*',
|
| 110 | redirect: '/login'
|
| 111 | }
|
| 112 | ]
|
| 113 |
|
| 114 | const router = createRouter({
|
| 115 | history: createWebHistory(),
|
| 116 | routes
|
| 117 | })
|
| 118 |
|
| 119 | // 更新路由守卫
|
| 120 | router.beforeEach(async (to, from, next) => {
|
| 121 | // 设置页面标题
|
| 122 | if (to.meta.title) {
|
| 123 | document.title = to.meta.title
|
| 124 | }
|
| 125 |
|
| 126 | console.log(`路由跳转: ${from.path} -> ${to.path}`)
|
| 127 |
|
| 128 | // 恢复登录状态(仅在应用启动时执行一次)
|
| 129 | if (!store.state.auth.isLoggedIn && localStorage.getItem('isLoggedIn') === 'true') {
|
| 130 | store.dispatch('auth/restoreLoginState')
|
| 131 | }
|
| 132 |
|
| 133 | const isLoggedIn = store.getters['auth/isAuthenticated']
|
| 134 |
|
| 135 | // 需要登录但未登录
|
| 136 | if (to.meta.requiresAuth && !isLoggedIn) {
|
| 137 | // 尝试检查服务器端的登录状态
|
| 138 | try {
|
| 139 | const isValid = await store.dispatch('auth/checkLoginStatus')
|
| 140 | if (isValid) {
|
| 141 | // 服务器确认已登录,继续跳转
|
| 142 | next()
|
| 143 | } else {
|
| 144 | // 服务器确认未登录,跳转到登录页
|
| 145 | ElMessage.warning('请先登录')
|
| 146 | next('/login')
|
| 147 | }
|
| 148 | } catch (error) {
|
| 149 | // 检查失败,跳转到登录页
|
| 150 | ElMessage.warning('请先登录')
|
| 151 | next('/login')
|
| 152 | }
|
| 153 | return
|
| 154 | }
|
| 155 |
|
| 156 | // 已登录但访问登录/注册页
|
| 157 | if (to.meta.requiresGuest && isLoggedIn) {
|
| 158 | next('/home')
|
| 159 | return
|
| 160 | }
|
| 161 |
|
| 162 | next()
|
| 163 | })
|
| 164 |
|
| 165 | export default router |