| | |
| | | const topbarRouters = ref<RouteRecordRaw[]>([]); |
| | | const sidebarRouters = ref<RouteRecordRaw[]>([]); |
| | | |
| | | const getRoutes = (): RouteRecordRaw[] => { |
| | | return routes.value; |
| | | }; |
| | | const getSidebarRoutes = (): RouteRecordRaw[] => { |
| | | return sidebarRouters.value; |
| | | }; |
| | | const getTopbarRoutes = (): RouteRecordRaw[] => { |
| | | return topbarRouters.value; |
| | | }; |
| | | |
| | | const setRoutes = (newRoutes: RouteRecordRaw[]): void => { |
| | | addRoutes.value = newRoutes; |
| | | routes.value = constantRoutes.concat(newRoutes); |
| | |
| | | setSidebarRouters(constantRoutes.concat(sidebarRoutes)); |
| | | setDefaultRoutes(sidebarRoutes); |
| | | setTopbarRoutes(defaultRoutes); |
| | | // 路由name重复检查 |
| | | duplicateRouteChecker(asyncRoutes, sidebarRoutes); |
| | | return new Promise<RouteRecordRaw[]>((resolve) => resolve(rewriteRoutes)); |
| | | }; |
| | | |
| | |
| | | }); |
| | | return children; |
| | | }; |
| | | return { routes, setRoutes, generateRoutes, setSidebarRouters, topbarRouters, sidebarRouters, defaultRoutes }; |
| | | return { |
| | | routes, |
| | | topbarRouters, |
| | | sidebarRouters, |
| | | defaultRoutes, |
| | | |
| | | getRoutes, |
| | | getSidebarRoutes, |
| | | getTopbarRoutes, |
| | | |
| | | setRoutes, |
| | | generateRoutes, |
| | | setSidebarRouters |
| | | }; |
| | | }); |
| | | |
| | | // 动态路由遍历,验证是否具备权限 |
| | |
| | | return usePermissionStore(store); |
| | | }; |
| | | |
| | | interface Route { |
| | | name?: string | symbol; |
| | | path: string; |
| | | children?: Route[]; |
| | | } |
| | | |
| | | /** |
| | | * 检查路由name是否重复 |
| | | * @param localRoutes 本地路由 |
| | | * @param routes 动态路由 |
| | | */ |
| | | function duplicateRouteChecker(localRoutes: Route[], routes: Route[]) { |
| | | // 展平 |
| | | function flatRoutes(routes: Route[]) { |
| | | const res: Route[] = []; |
| | | routes.forEach((route) => { |
| | | if (route.children) { |
| | | res.push(...flatRoutes(route.children)); |
| | | } else { |
| | | res.push(route); |
| | | } |
| | | }); |
| | | return res; |
| | | } |
| | | |
| | | const allRoutes = flatRoutes([...localRoutes, ...routes]); |
| | | |
| | | const nameList: string[] = []; |
| | | allRoutes.forEach((route) => { |
| | | const name = route.name.toString(); |
| | | if (name && nameList.includes(name)) { |
| | | const message = `路由名称: [${name}] 重复, 会造成 404`; |
| | | console.error(message); |
| | | ElNotification({ |
| | | title: '路由名称重复', |
| | | message, |
| | | type: 'error' |
| | | }); |
| | | return; |
| | | } |
| | | nameList.push(route.name.toString()); |
| | | }); |
| | | } |
| | | |
| | | export default usePermissionStore; |