Files
EmailBill/Web/src/router/index.js

171 lines
4.9 KiB
JavaScript
Raw Normal View History

2025-12-25 11:20:56 +08:00
import { createRouter, createWebHistory } from 'vue-router'
2025-12-25 13:27:23 +08:00
import { useAuthStore } from '@/stores/auth'
2026-02-02 16:59:24 +08:00
import { useVersionStore } from '@/stores/version'
2025-12-25 11:20:56 +08:00
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
2025-12-25 13:27:23 +08:00
{
path: '/login',
name: 'login',
component: () => import('../views/LoginView.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: false }
2025-12-25 13:27:23 +08:00
},
2025-12-25 11:20:56 +08:00
{
2025-12-28 10:23:57 +08:00
path: '/balance',
name: 'balance',
component: () => import('../views/BalanceView.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-25 11:20:56 +08:00
},
{
path: '/email',
name: 'email',
component: () => import('../views/EmailRecord.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-25 11:20:56 +08:00
},
{
path: '/setting',
name: 'setting',
component: () => import('../views/SettingView.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-25 11:20:56 +08:00
},
{
path: '/calendar',
name: 'calendar',
component: () => import('../views/CalendarView.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-25 11:20:56 +08:00
},
2026-02-02 16:59:24 +08:00
{
path: '/calendar-v2',
name: 'calendar-v2',
2026-02-03 17:56:32 +08:00
component: () => import('../views/calendarV2/Index.vue'),
2026-02-02 16:59:24 +08:00
meta: { requiresAuth: true }
},
{
path: '/smart-classification',
name: 'smart-classification',
2025-12-26 15:21:31 +08:00
component: () => import('../views/ClassificationSmart.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-26 15:21:31 +08:00
},
{
path: '/classification-edit',
name: 'classification-edit',
component: () => import('../views/ClassificationEdit.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-26 15:21:31 +08:00
},
{
path: '/classification-batch',
name: 'classification-batch',
component: () => import('../views/ClassificationBatch.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-26 15:21:31 +08:00
},
{
path: '/classification-nlp',
name: 'classification-nlp',
component: () => import('../views/ClassificationNLP.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-26 17:13:57 +08:00
},
{
2025-12-28 10:23:57 +08:00
path: '/',
2025-12-26 17:13:57 +08:00
name: 'statistics',
2026-02-09 19:25:51 +08:00
component: () => import('../views/statisticsV1/Index.vue'),
meta: { requiresAuth: true }
},
{
path: '/statistics-v2',
name: 'statistics-v2',
component: () => import('../views/statisticsV2/Index.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-26 17:13:57 +08:00
},
{
path: '/bill-analysis',
name: 'bill-analysis',
component: () => import('../views/BillAnalysisView.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-28 10:23:57 +08:00
},
{
path: '/message',
name: 'message',
redirect: { path: '/balance', query: { tab: 'message' } },
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-29 15:20:32 +08:00
},
{
path: '/periodic-record',
name: 'periodic-record',
component: () => import('../views/PeriodicRecord.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
2025-12-29 16:45:51 +08:00
},
{
path: '/log',
name: 'log',
component: () => import('../views/LogView.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
},
{
path: '/budget',
name: 'budget',
component: () => import('../views/BudgetView.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
},
{
path: '/scheduled-tasks',
name: 'scheduled-tasks',
component: () => import('../views/ScheduledTasksView.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
},
{
// 待确认的分类项
path: '/unconfirmed-classification',
name: 'unconfirmed-classification',
component: () => import('../views/UnconfirmedClassification.vue'),
2026-01-16 11:15:44 +08:00
meta: { requiresAuth: true }
}
2026-01-16 11:15:44 +08:00
]
2025-12-25 11:20:56 +08:00
})
2025-12-25 13:27:23 +08:00
// 路由守卫
router.beforeEach((to, from, next) => {
const authStore = useAuthStore()
2026-02-02 16:59:24 +08:00
const versionStore = useVersionStore()
2025-12-25 13:27:23 +08:00
const requiresAuth = to.meta.requiresAuth !== false // 默认需要认证
if (requiresAuth && !authStore.isAuthenticated) {
// 需要认证但未登录,跳转到登录页
next({ name: 'login', query: { redirect: to.fullPath } })
} else if (to.name === 'login' && authStore.isAuthenticated) {
// 已登录用户访问登录页,跳转到首页
next({ name: 'transactions' })
} else {
2026-02-02 16:59:24 +08:00
// 版本路由处理
if (versionStore.isV2()) {
// 如果当前选择 V2尝试跳转到 V2 路由
const routeName = to.name?.toString()
if (routeName && !routeName.endsWith('-v2')) {
const v2RouteName = `${routeName}-v2`
const v2Route = router.getRoutes().find(route => route.name === v2RouteName)
if (v2Route) {
next({ name: v2RouteName, query: to.query, params: to.params })
return
}
}
} else {
// 如果当前选择 V1且访问的是 V2 路由,跳转到 V1
const routeName = to.name?.toString()
if (routeName && routeName.endsWith('-v2')) {
const v1RouteName = routeName.replace(/-v2$/, '')
const v1Route = router.getRoutes().find(route => route.name === v1RouteName)
if (v1Route) {
next({ name: v1RouteName, query: to.query, params: to.params })
return
}
}
}
2025-12-25 13:27:23 +08:00
next()
}
})
2025-12-25 11:20:56 +08:00
export default router