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