import { createRouter, createWebHistory } from 'vue-router'; import { useUserStore } from '@/stores/user'; const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/login', component: () => import('@/pages/login-page.vue'), }, { path: '/', component: () => import('@/layouts/app-layout.vue'), redirect: '/dashboard', children: [ { path: 'dashboard', component: () => import('@/pages/dashboard-page.vue'), }, { path: 'station', component: () => import('@/pages/station-page.vue'), }, { path: 'device', component: () => import('@/pages/device-page.vue'), }, { path: 'alarm', component: () => import('@/pages/alarm-page.vue'), }, { path: 'statistics', component: () => import('@/pages/statistics-page.vue'), }, { path: 'log', children: [ { path: 'vimp-log', component: () => import('@/pages/vimp-log-page.vue'), }, { path: 'call-log', component: () => import('@/pages/call-log-page.vue'), }, ], }, { path: 'debug', component: () => import('@/pages/debug-page.vue'), beforeEnter: (to, from, next) => { if (import.meta.env.DEV) { next(); } else { next(false); } }, }, { path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('@/pages/not-found-page.vue'), }, ], }, ], }); const whiteList = ['/debug']; router.beforeEach((to) => { const userStore = useUserStore(); const isAuthed = !!userStore.userLoginResult?.token; // 放行白名单 const inWhiteList = whiteList.some((path) => to.path.startsWith(path)); if (inWhiteList) { return true; } // 已登录用户不允许进入登录页(手动访问 /login 会重定向到首页) if (to.path === '/login') { if (isAuthed) { return { path: '/' }; } else { return true; } } else { // 其它路由按登录态控制 if (!isAuthed) { return { path: '/login' }; } else { return true; } } }); export default router;