blob: 7f60f9ef6bba18450809e1413469839535b43e8f [file] [log] [blame]
Xing Jinwenff16b1e2025-06-05 00:29:26 +08001import { createRouter, createWebHistory } from 'vue-router'
2import { ElMessage } from 'element-plus'
3import store from '@/store'
4
5// 路由组件
6import LoginView from '@/views/auth/LoginView.vue'
7import RegisterView from '@/views/auth/RegisterView.vue'
8import HomeView from '@/views/HomeView.vue'
208159515458d95702025-06-09 14:46:58 +08009import ForumView from '@/views/forum/ForumView.vue'
10import TopicView from '@/views/forum/TopicView.vue'
11import ChatRoom from '@/views/ChatRoom.vue'
Xing Jinwenff16b1e2025-06-05 00:29:26 +080012
vulgar5201c4345b12025-06-09 18:48:06 +080013
Xing Jinwenff16b1e2025-06-05 00:29:26 +080014const 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 {
vulgar5201c4345b12025-06-09 18:48:06 +080075 path: '/torrent/:infoHash',
Xing Jinwenff16b1e2025-06-05 00:29:26 +080076 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',
208159515458d95702025-06-09 14:46:58 +080086 name: 'forum',
87 component: ForumView
Xing Jinwenff16b1e2025-06-05 00:29:26 +080088 },
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',
208159515458d95702025-06-09 14:46:58 +0800100 name: 'topic',
101 component: TopicView
102 },
103 {
104 path: '/chat',
105 name: 'ChatRoom',
106 component: ChatRoom,
Xing Jinwenff16b1e2025-06-05 00:29:26 +0800107 meta: {
208159515458d95702025-06-09 14:46:58 +0800108 title: 'PT Tracker - 聊天室',
Xing Jinwenff16b1e2025-06-05 00:29:26 +0800109 requiresAuth: true
110 }
111 },
112 // 404页面
113 {
114 path: '/:pathMatch(.*)*',
115 redirect: '/login'
116 }
117]
118
119const router = createRouter({
120 history: createWebHistory(),
121 routes
122})
123
124// 更新路由守卫
125router.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
170export default router