import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' import HomeView from '../views/HomeView.vue' import LoginView from '../views/LoginView.vue' import ProfileView from '../views/ProfileView.vue' import StudentView from '../views/StudentView.vue' import SubjectView from '../views/SubjectView.vue' import TermView from '../views/TermView.vue' import TaskView from '../views/TaskView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView, meta: { requiresAuth: true, layout: 'default', title: 'Home', breadcrumb: { key: 'navigation.breadcrumb.home', icon: 'mdi-home' } }, }, { path: '/login', name: 'login', component: LoginView, meta: { requiresAuth: false, layout: 'none' }, }, { path: '/profile', name: 'profile', component: ProfileView, meta: { requiresAuth: true, layout: 'default', title: 'Profile', breadcrumb: { key: 'navigation.breadcrumb.profile' }, breadcrumbPath: [ { key: 'navigation.breadcrumb.home', to: '/', disabled: false }, { key: 'navigation.breadcrumb.profile', to: '/profile', disabled: true } ] }, }, { path: '/master-data/students', name: 'students', component: StudentView, meta: { requiresAuth: true, layout: 'default', title: 'Students', breadcrumb: { key: 'navigation.breadcrumb.students' }, breadcrumbPath: [ { key: 'navigation.breadcrumb.home', to: '/', disabled: false }, { key: 'navigation.breadcrumb.masterData', to: null, disabled: true }, { key: 'navigation.breadcrumb.students', to: '/master-data/students', disabled: true } ] }, }, { path: '/master-data/subjects', name: 'subjects', component: SubjectView, meta: { requiresAuth: true, layout: 'default', title: 'Subjects', breadcrumb: { key: 'navigation.breadcrumb.subjects' }, breadcrumbPath: [ { key: 'navigation.breadcrumb.home', to: '/', disabled: false }, { key: 'navigation.breadcrumb.masterData', to: null, disabled: true }, { key: 'navigation.breadcrumb.subjects', to: '/master-data/subjects', disabled: true } ] }, }, { path: '/master-data/terms', name: 'terms', component: TermView, meta: { requiresAuth: true, layout: 'default', title: 'Terms', breadcrumb: { key: 'navigation.breadcrumb.terms' }, breadcrumbPath: [ { key: 'navigation.breadcrumb.home', to: '/', disabled: false }, { key: 'navigation.breadcrumb.masterData', to: null, disabled: true }, { key: 'navigation.breadcrumb.terms', to: '/master-data/terms', disabled: true } ] }, }, { path: '/tasks', name: 'tasks', component: TaskView, meta: { requiresAuth: true, layout: 'default', title: 'Tasks', breadcrumb: { key: 'navigation.breadcrumb.tasks' }, breadcrumbPath: [ { key: 'navigation.breadcrumb.home', to: '/', disabled: false }, { key: 'navigation.breadcrumb.tasks', to: '/tasks', disabled: true } ] }, }, { path: '/:pathMatch(.*)*', redirect: '/', }, ], }) // 路由守卫 router.beforeEach(async (to, from, next) => { const authStore = useAuthStore() const isAuthed = authStore.isAuthenticated if (to.meta.requiresAuth && !isAuthed) { next('/login') return } // 只有已认证用户才尝试刷新token,且不在/login页面 if ( to.meta.requiresAuth && to.path !== '/login' && isAuthed && authStore.isAccessTokenExpiringSoon && authStore.isAccessTokenExpiringSoon() ) { try { await authStore.refreshAccessToken() } catch { // 刷新失败,清理token并跳转到登录 if (authStore.logout) authStore.logout() next('/login') return } } if (to.path === '/login' && isAuthed) { next('/') return } next() }) export default router