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

100 lines
2.7 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'
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'),
meta: { requiresAuth: false },
},
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'),
2025-12-25 13:27:23 +08:00
meta: { requiresAuth: true },
2025-12-25 11:20:56 +08:00
},
{
path: '/email',
name: 'email',
component: () => import('../views/EmailRecord.vue'),
2025-12-25 13:27:23 +08:00
meta: { requiresAuth: true },
2025-12-25 11:20:56 +08:00
},
{
path: '/setting',
name: 'setting',
component: () => import('../views/SettingView.vue'),
2025-12-25 13:27:23 +08:00
meta: { requiresAuth: true },
2025-12-25 11:20:56 +08:00
},
{
path: '/calendar',
name: 'calendar',
component: () => import('../views/CalendarView.vue'),
2025-12-25 13:27:23 +08:00
meta: { requiresAuth: true },
2025-12-25 11:20:56 +08:00
},
{
path: '/smart-classification',
name: 'smart-classification',
2025-12-26 15:21:31 +08:00
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 },
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',
component: () => import('../views/StatisticsView.vue'),
meta: { requiresAuth: true },
},
{
path: '/bill-analysis',
name: 'bill-analysis',
component: () => import('../views/BillAnalysisView.vue'),
meta: { requiresAuth: true },
2025-12-28 10:23:57 +08:00
},
{
path: '/message',
name: 'message',
component: () => import('../views/MessageView.vue'),
meta: { requiresAuth: true },
}
2025-12-25 11:20:56 +08:00
],
})
2025-12-25 13:27:23 +08:00
// 路由守卫
router.beforeEach((to, from, next) => {
const authStore = useAuthStore()
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 {
next()
}
})
2025-12-25 11:20:56 +08:00
export default router
2025-12-25 13:27:23 +08:00