2081595154 | f846f91 | 2025-06-04 17:35:21 +0800 | [diff] [blame] | 1 | import { createRouter, createWebHistory } from 'vue-router' |
| 2 | import { ElMessage } from 'element-plus' |
| 3 | |
| 4 | // 路由组件 |
| 5 | import LoginView from '@/views/auth/LoginView.vue' |
| 6 | import RegisterView from '@/views/auth/RegisterView.vue' |
| 7 | import HomeView from '@/views/HomeView.vue' |
| 8 | |
| 9 | const routes = [ |
| 10 | { |
| 11 | path: '/', |
| 12 | redirect: '/login' |
| 13 | }, |
| 14 | { |
| 15 | path: '/login', |
| 16 | name: 'Login', |
| 17 | component: LoginView, |
| 18 | meta: { |
| 19 | title: 'PT Tracker - 登录', |
| 20 | requiresGuest: true |
| 21 | } |
| 22 | }, |
| 23 | { |
| 24 | path: '/register', |
| 25 | name: 'Register', |
| 26 | component: RegisterView, |
| 27 | meta: { |
| 28 | title: 'PT Tracker - 注册', |
| 29 | requiresGuest: true |
| 30 | } |
| 31 | }, |
| 32 | { |
| 33 | path: '/home', |
| 34 | name: 'Home', |
| 35 | component: HomeView, |
| 36 | meta: { |
| 37 | title: 'PT Tracker - 首页', |
| 38 | requiresAuth: true |
| 39 | } |
| 40 | }, |
| 41 | { |
| 42 | path: '/:pathMatch(.*)*', |
| 43 | redirect: '/login' |
xingjinwen | d652cc6 | 2025-06-04 19:52:19 +0800 | [diff] [blame^] | 44 | }, |
| 45 | { |
| 46 | path: '/torrents', |
| 47 | name: 'Torrents', |
| 48 | component: () => import('@/views/torrent/TorrentsView.vue'), |
| 49 | meta: { |
| 50 | title: 'PT Tracker - 种子浏览', |
| 51 | requiresAuth: true |
2081595154 | f846f91 | 2025-06-04 17:35:21 +0800 | [diff] [blame] | 52 | } |
xingjinwen | d652cc6 | 2025-06-04 19:52:19 +0800 | [diff] [blame^] | 53 | }, |
| 54 | { |
| 55 | path: '/upload', |
| 56 | name: 'Upload', |
| 57 | component: () => import('@/views/torrent/UploadView.vue'), |
| 58 | meta: { |
| 59 | title: 'PT Tracker - 上传种子', |
| 60 | requiresAuth: true |
| 61 | } |
| 62 | }, |
| 63 | { |
| 64 | path: '/torrent/:id', |
| 65 | name: 'TorrentDetail', |
| 66 | component: () => import('@/views/torrent/TorrentDetailView.vue'), |
| 67 | meta: { |
| 68 | title: 'PT Tracker - 种子详情', |
| 69 | requiresAuth: true |
| 70 | } |
| 71 | }, |
| 72 | { |
| 73 | path: '/forum', |
| 74 | name: 'Forum', |
| 75 | component: () => import('@/views/forum/ForumView.vue'), |
| 76 | meta: { |
| 77 | title: 'PT Tracker - 论坛', |
| 78 | requiresAuth: true |
| 79 | } |
| 80 | }, |
| 81 | { |
| 82 | path: '/forum/section/:id', |
| 83 | name: 'ForumSection', |
| 84 | component: () => import('@/views/forum/ForumSectionView.vue'), |
| 85 | meta: { |
| 86 | title: 'PT Tracker - 版块', |
| 87 | requiresAuth: true |
| 88 | } |
| 89 | }, |
| 90 | { |
| 91 | path: '/forum/topic/:id', |
| 92 | name: 'ForumTopic', |
| 93 | component: () => import('@/views/forum/ForumTopicView.vue'), |
| 94 | meta: { |
| 95 | title: 'PT Tracker - 主题详情', |
| 96 | requiresAuth: true |
| 97 | } |
| 98 | }, |
| 99 | // { |
| 100 | // path: '/forum/topics', |
| 101 | // name: 'ForumTopics', |
| 102 | // component: () => impor@/views/forum/ForumTopicView.vue'), |
| 103 | // meta: { |
| 104 | // title: 'PT Tracker - 所有主题', |
| 105 | // requiresAuth: true |
| 106 | // } |
| 107 | // }, |
| 108 | { |
| 109 | path: '/profile', |
| 110 | name: 'Profile', |
| 111 | component: () => import('@/views/auth/ProfileView.vue'), |
| 112 | meta: { |
| 113 | title: 'PT Tracker - 个人资料', |
| 114 | requiresAuth: true |
| 115 | } |
| 116 | }] |
2081595154 | f846f91 | 2025-06-04 17:35:21 +0800 | [diff] [blame] | 117 | |
| 118 | const router = createRouter({ |
| 119 | history: createWebHistory(), |
| 120 | routes |
| 121 | }) |
| 122 | |
| 123 | // 路由守卫 |
| 124 | router.beforeEach((to, from, next) => { |
| 125 | // 设置页面标题 |
| 126 | if (to.meta.title) { |
| 127 | document.title = to.meta.title |
| 128 | } |
| 129 | |
| 130 | // 检查登录状态 |
| 131 | const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true' |
| 132 | |
| 133 | console.log(`路由跳转: ${from.path} -> ${to.path}, 登录状态: ${isLoggedIn}`) |
| 134 | |
| 135 | // 需要登录但未登录 |
| 136 | if (to.meta.requiresAuth && !isLoggedIn) { |
| 137 | ElMessage.warning('请先登录') |
| 138 | next('/login') |
| 139 | return |
| 140 | } |
| 141 | |
| 142 | // 已登录但访问登录/注册页 |
| 143 | if (to.meta.requiresGuest && isLoggedIn) { |
| 144 | next('/home') |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | next() |
| 149 | }) |
| 150 | |
| 151 | export default router |