All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 26s
Docker Build & Deploy / Deploy to Production (push) Successful in 8s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 2s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
177 lines
5.1 KiB
JavaScript
177 lines
5.1 KiB
JavaScript
import { createRouter, createWebHistory } from 'vue-router'
|
||
import { useAuthStore } from '@/stores/auth'
|
||
import { useVersionStore } from '@/stores/version'
|
||
|
||
const router = createRouter({
|
||
history: createWebHistory(import.meta.env.BASE_URL),
|
||
routes: [
|
||
{
|
||
path: '/login',
|
||
name: 'login',
|
||
component: () => import('../views/LoginView.vue'),
|
||
meta: { requiresAuth: false }
|
||
},
|
||
{
|
||
path: '/balance',
|
||
name: 'balance',
|
||
component: () => import('../views/BalanceView.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/email',
|
||
name: 'email',
|
||
component: () => import('../views/EmailRecord.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/setting',
|
||
name: 'setting',
|
||
component: () => import('../views/SettingView.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/calendar',
|
||
name: 'calendar',
|
||
component: () => import('../views/CalendarView.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/calendar-v2',
|
||
name: 'calendar-v2',
|
||
component: () => import('../views/calendarV2/Index.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/smart-classification',
|
||
name: 'smart-classification',
|
||
component: () => import('../views/ClassificationSmart.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/classification-edit',
|
||
name: 'classification-edit',
|
||
component: () => import('../views/ClassificationEdit.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/classification-batch',
|
||
name: 'classification-batch',
|
||
component: () => import('../views/ClassificationBatch.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/classification-nlp',
|
||
name: 'classification-nlp',
|
||
component: () => import('../views/ClassificationNLP.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/',
|
||
name: 'statistics',
|
||
component: () => import('../views/statisticsV1/Index.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/statistics-v2',
|
||
name: 'statistics-v2',
|
||
component: () => import('../views/statisticsV2/Index.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/bill-analysis',
|
||
name: 'bill-analysis',
|
||
component: () => import('../views/BillAnalysisView.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/message',
|
||
name: 'message',
|
||
redirect: { path: '/balance', query: { tab: 'message' } },
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/periodic-record',
|
||
name: 'periodic-record',
|
||
component: () => import('../views/PeriodicRecord.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/log',
|
||
name: 'log',
|
||
component: () => import('../views/LogView.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/budget',
|
||
name: 'budget',
|
||
component: () => import('../views/BudgetView.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/budget-v2',
|
||
name: 'budget-v2',
|
||
component: () => import('../views/budgetV2/Index.vue'),
|
||
meta: { requiresAuth: true, keepAlive: true }
|
||
},
|
||
{
|
||
path: '/scheduled-tasks',
|
||
name: 'scheduled-tasks',
|
||
component: () => import('../views/ScheduledTasksView.vue'),
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
// 待确认的分类项
|
||
path: '/unconfirmed-classification',
|
||
name: 'unconfirmed-classification',
|
||
component: () => import('../views/UnconfirmedClassification.vue'),
|
||
meta: { requiresAuth: true }
|
||
}
|
||
]
|
||
})
|
||
|
||
// 路由守卫
|
||
router.beforeEach((to, from, next) => {
|
||
const authStore = useAuthStore()
|
||
const versionStore = useVersionStore()
|
||
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 {
|
||
// 版本路由处理
|
||
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
|
||
}
|
||
}
|
||
}
|
||
|
||
next()
|
||
}
|
||
})
|
||
|
||
export default router
|