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