|
|
|
@ -9,6 +9,7 @@ import ParentView from '@/components/ParentView/index.vue'; |
|
|
|
import InnerLink from '@/layout/components/InnerLink/index.vue'; |
|
|
|
import { ref } from 'vue'; |
|
|
|
import { createCustomNameComponent } from '@/utils/createCustomNameComponent'; |
|
|
|
import { isHttp } from '@/utils/validate'; |
|
|
|
|
|
|
|
// 匹配views里面所有的.vue文件
|
|
|
|
const modules = import.meta.glob('./../../views/**/*.vue'); |
|
|
|
@ -20,6 +21,9 @@ export interface SystemCodeOption { |
|
|
|
/** 当设置了 iframeUrl 时,切换到该选项会以全屏 iframe 展示,而非加载菜单路由 */ |
|
|
|
iframeUrl?: string; |
|
|
|
} |
|
|
|
/** 404 兜底路由名称,方便动态移除和重新添加 */ |
|
|
|
export const CATCH_ALL_ROUTE_NAME = 'CatchAll404'; |
|
|
|
|
|
|
|
export const systemCodeOptions: SystemCodeOption[] = [ |
|
|
|
{ label: '首页', value: 'planindex', icon: 'HomeFilled', iframeUrl: '/enforcement-code-v1/statistical/home.html' }, |
|
|
|
{ label: '执法监督码', value: 'supervisionCode', icon: 'Document' }, |
|
|
|
@ -32,6 +36,8 @@ export const usePermissionStore = defineStore('permission', () => { |
|
|
|
const defaultRoutes = ref<RouteRecordRaw[]>([]); |
|
|
|
const topbarRouters = ref<RouteRecordRaw[]>([]); |
|
|
|
const sidebarRouters = ref<RouteRecordRaw[]>([]); |
|
|
|
/** 缓存后端返回的全量路由原始数据(未经组件转换) */ |
|
|
|
const allRouteDataCache = ref<any[]>([]); |
|
|
|
/** sessionStorage key */ |
|
|
|
const SYSTEM_CODE_KEY = 'currentSystemCode'; |
|
|
|
/** 当前选中的系统编码(从 sessionStorage 恢复,默认 planindex) */ |
|
|
|
@ -71,12 +77,18 @@ export const usePermissionStore = defineStore('permission', () => { |
|
|
|
}; |
|
|
|
const generateRoutes = async (systemCode?: string): Promise<RouteRecordRaw[]> => { |
|
|
|
const code = systemCode || currentSystemCode.value; |
|
|
|
const res = await getRouters(code); |
|
|
|
const { data } = res; |
|
|
|
console.log('[路由调试] systemCode =', code, ', 后端返回路由数据 =', JSON.parse(JSON.stringify(data))); |
|
|
|
const sdata = JSON.parse(JSON.stringify(data)); |
|
|
|
const rdata = JSON.parse(JSON.stringify(data)); |
|
|
|
const defaultData = JSON.parse(JSON.stringify(data)); |
|
|
|
// 仅首次加载时请求后端,后续从缓存取
|
|
|
|
if (allRouteDataCache.value.length === 0) { |
|
|
|
const res = await getRouters(); |
|
|
|
allRouteDataCache.value = res.data as any[]; |
|
|
|
console.log('[路由调试] 后端返回全量路由数据 =', JSON.parse(JSON.stringify(allRouteDataCache.value))); |
|
|
|
} |
|
|
|
// 按当前 systemCode 过滤出匹配的顶层路由
|
|
|
|
const filteredData = allRouteDataCache.value.filter((route: any) => route.systemCode === code); |
|
|
|
console.log('[路由调试] systemCode =', code, ', 过滤后路由数据 =', JSON.parse(JSON.stringify(filteredData))); |
|
|
|
const sdata = JSON.parse(JSON.stringify(filteredData)); |
|
|
|
const rdata = JSON.parse(JSON.stringify(filteredData)); |
|
|
|
const defaultData = JSON.parse(JSON.stringify(filteredData)); |
|
|
|
const sidebarRoutes = filterAsyncRouter(sdata); |
|
|
|
const rewriteRoutes = filterAsyncRouter(rdata, undefined, true); |
|
|
|
const defaultRoutes = filterAsyncRouter(defaultData); |
|
|
|
@ -152,19 +164,34 @@ export const usePermissionStore = defineStore('permission', () => { |
|
|
|
} |
|
|
|
iframeMode.value = false; |
|
|
|
iframeUrl.value = ''; |
|
|
|
// 移除之前动态添加的路由
|
|
|
|
addRoutes.value.forEach((route) => { |
|
|
|
if (route.name) { |
|
|
|
|
|
|
|
// ====== 彻底清理所有动态路由,保证路由表干净 ======
|
|
|
|
// 1. 收集所有静态路由名称(包括子路由)
|
|
|
|
const constantNames = new Set<string | symbol>(); |
|
|
|
const collectNames = (routeList: RouteRecordRaw[]) => { |
|
|
|
routeList.forEach((r) => { |
|
|
|
if (r.name) constantNames.add(r.name); |
|
|
|
if (r.children) collectNames(r.children); |
|
|
|
}); |
|
|
|
}; |
|
|
|
collectNames(constantRoutes); |
|
|
|
// 2. 移除所有非静态路由(包括之前的动态业务路由和404兜底路由)
|
|
|
|
router.getRoutes().forEach((route) => { |
|
|
|
if (route.name && !constantNames.has(route.name)) { |
|
|
|
router.removeRoute(route.name); |
|
|
|
} |
|
|
|
}); |
|
|
|
// 重新生成路由
|
|
|
|
|
|
|
|
// ====== 重新生成并注册路由 ======
|
|
|
|
const accessRoutes = await generateRoutes(systemCode); |
|
|
|
accessRoutes.forEach((route) => { |
|
|
|
if (!router.hasRoute(route.name!)) { |
|
|
|
if (!isHttp(route.path)) { |
|
|
|
router.addRoute(route); |
|
|
|
} |
|
|
|
}); |
|
|
|
// 重新添加404兜底路由,确保放在所有动态路由之后
|
|
|
|
router.addRoute({ name: CATCH_ALL_ROUTE_NAME, path: '/:pathMatch(.*)*', component: () => import('@/views/error/404.vue') }); |
|
|
|
console.log('[路由调试] switchSystemCode 完成, 当前路由列表 =', router.getRoutes().map((r) => ({ name: r.name, path: r.path }))); |
|
|
|
}; |
|
|
|
|
|
|
|
return { |
|
|
|
|