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
|
|
|
/**
|
2026-07-23 15:00:19 +08:00
|
|
|
* 静态路由名称集合(这些路由不会被 resetRouter 移除)
|
|
|
|
|
*/
|
|
|
|
|
const STATIC_ROUTE_NAMES = new Set(['Login', 'NotFound']);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置路由:清除动态添加的路由(包括 BasicLayout 和其子路由),
|
|
|
|
|
* 恢复到只包含静态路由的状态。用于退出登录时清理。
|
2026-07-23 11:27:33 +08:00
|
|
|
*/
|
|
|
|
|
export function resetRouter() {
|
|
|
|
|
const currentRoutes = router.getRoutes();
|
2026-07-14 10:31:17 +08:00
|
|
|
|
2026-07-23 11:27:33 +08:00
|
|
|
currentRoutes.forEach((route) => {
|
2026-07-23 15:00:19 +08:00
|
|
|
const routeName = route.name as string;
|
|
|
|
|
// 保留静态路由(Login, NotFound),移除所有动态路由(含 BasicLayout 及其子路由)
|
|
|
|
|
if (routeName && !STATIC_ROUTE_NAMES.has(routeName)) {
|
|
|
|
|
router.removeRoute(routeName);
|
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 };
|