2026-07-14 10:31:17 +08:00
|
|
|
import { createRouter, createWebHistory } from 'vue-router';
|
2026-07-23 11:27:33 +08:00
|
|
|
import { constantRoutes } from './routes';
|
2026-07-14 10:31:17 +08:00
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
|
history: createWebHistory(import.meta.env.VITE_BASE_URL || '/'),
|
2026-07-23 11:27:33 +08:00
|
|
|
routes: constantRoutes,
|
2026-07-14 10:31:17 +08:00
|
|
|
scrollBehavior: () => ({ left: 0, top: 0 }),
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-23 11:27:33 +08:00
|
|
|
/**
|
|
|
|
|
* 路由白名单:不需要登录即可访问的路径
|
|
|
|
|
*/
|
|
|
|
|
const WHITE_LIST = ['/login', '/404'];
|
2026-07-14 10:31:17 +08:00
|
|
|
|
2026-07-23 11:27:33 +08:00
|
|
|
/**
|
|
|
|
|
* 重置路由:清除动态添加的子路由,恢复到只包含静态路由的状态
|
|
|
|
|
* 用于退出登录时清理
|
|
|
|
|
*/
|
|
|
|
|
export function resetRouter() {
|
|
|
|
|
const currentRoutes = router.getRoutes();
|
|
|
|
|
const protectedNames = new Set(['Login', 'BasicLayout', 'NotFound']);
|
2026-07-14 10:31:17 +08:00
|
|
|
|
2026-07-23 11:27:33 +08:00
|
|
|
currentRoutes.forEach((route) => {
|
|
|
|
|
// 保留静态路由(Login, BasicLayout, NotFound),移除动态添加的子路由
|
|
|
|
|
if (!protectedNames.has(route.name as string)) {
|
|
|
|
|
router.removeRoute(route.name!);
|
2026-07-14 10:31:17 +08:00
|
|
|
}
|
2026-07-23 11:27:33 +08:00
|
|
|
});
|
|
|
|
|
}
|
2026-07-14 10:31:17 +08:00
|
|
|
|
|
|
|
|
export default router;
|
2026-07-23 11:27:33 +08:00
|
|
|
export { WHITE_LIST };
|