@ -7,20 +7,14 @@
@@ -7,20 +7,14 @@
* /
import type { AppRouteModule , AppRouteRecordRaw } from '/@/router/types' ;
import type { Router , RouteRecordNormalized } from 'vue-router' ;
import { getParentLayout , LAYOUT } from '/@/router/constant' ;
import { cloneDeep , omit } from 'lodash-es' ;
import { cloneDeep } from 'lodash-es' ;
import { warn } from '/@/utils/log' ;
import { createRouter , createWebHashHistory } from 'vue-router' ;
const IFRAME = ( ) = > import ( '/@/views/sys/iframe/FrameBlank.vue' ) ;
const LayoutMap = new Map < string , ( ) = > Promise < typeof import ( ' * .vue ' ) > > ( ) ;
LayoutMap . set ( 'LAYOUT' , LAYOUT ) ;
LayoutMap . set ( 'IFRAME' , IFRAME ) ;
let dynamicViewsModules : Record < string , ( ) = > Promise < Recordable > > ;
function asyncImportRoute ( routes : AppRouteRecordRaw [ ] | undefined ) {
@ -46,10 +40,7 @@ function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
@@ -46,10 +40,7 @@ function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
} ) ;
}
function dynamicImport (
dynamicViewsModules : Record < string , ( ) = > Promise < Recordable > > ,
component : string
) {
function dynamicImport ( dynamicViewsModules : Record < string , ( ) = > Promise < Recordable > > , component : string ) {
const keys = Object . keys ( dynamicViewsModules ) ;
const matchKeys = keys . filter ( ( key ) = > {
let k = key . replace ( '../../views' , '' ) ;
@ -62,14 +53,12 @@ function dynamicImport(
@@ -62,14 +53,12 @@ function dynamicImport(
return dynamicViewsModules [ matchKey ] ;
}
if ( matchKeys ? . length > 1 ) {
warn (
'Please do not create `.vue` and `.TSX` files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure'
) ;
warn ( 'Please do not create `.vue` and `.TSX` files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure' ) ;
return ;
}
}
// 将菜单对象变成路由对象
/** 将菜单对象变成路由对象 */
export function transformObjToRoute < T = AppRouteModule > ( routeList : AppRouteModule [ ] ) : T [ ] {
routeList . forEach ( ( route ) = > {
const component = route . component as string ;
@ -91,74 +80,3 @@ export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModul
@@ -91,74 +80,3 @@ export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModul
} ) ;
return routeList as unknown as T [ ] ;
}
/ * *
* 将 多 级 路 由 转 换 为 2 级 路 由
* /
export function flatMultiLevelRoutes ( routeModules : AppRouteModule [ ] ) {
const module s : AppRouteModule [ ] = cloneDeep ( routeModules ) ;
for ( let index = 0 ; index < module s.length ; index ++ ) {
const routeModule = module s [ index ] ;
if ( ! isMultipleRoute ( routeModule ) ) {
continue ;
}
promoteRouteLevel ( routeModule ) ;
}
return module s ;
}
// 路由等级升级
function promoteRouteLevel ( routeModule : AppRouteModule ) {
// 使用vue-router拼接菜单
let router : Router | null = createRouter ( {
routes : [ routeModule as unknown as RouteRecordNormalized ] ,
history : createWebHashHistory ( ) ,
} ) ;
const routes = router . getRoutes ( ) ;
addToChildren ( routes , routeModule . children || [ ] , routeModule ) ;
router = null ;
routeModule . children = routeModule . children ? . map ( ( item ) = > omit ( item , 'children' ) ) ;
}
// 将所有子路由添加到二级路由
function addToChildren (
routes : RouteRecordNormalized [ ] ,
children : AppRouteRecordRaw [ ] ,
routeModule : AppRouteModule
) {
for ( let index = 0 ; index < children . length ; index ++ ) {
const child = children [ index ] ;
const route = routes . find ( ( item ) = > item . name === child . name ) ;
if ( ! route ) {
continue ;
}
routeModule . children = routeModule . children || [ ] ;
if ( ! routeModule . children . find ( ( item ) = > item . name === route . name ) ) {
routeModule . children ? . push ( route as unknown as AppRouteModule ) ;
}
if ( child . children ? . length ) {
addToChildren ( routes , child . children , routeModule ) ;
}
}
}
// 判断级别是否超过2级
function isMultipleRoute ( routeModule : AppRouteModule ) {
if ( ! routeModule || ! Reflect . has ( routeModule , 'children' ) || ! routeModule . children ? . length ) {
return false ;
}
const children = routeModule . children ;
let flag = false ;
for ( let index = 0 ; index < children . length ; index ++ ) {
const child = children [ index ] ;
if ( child . children ? . length ) {
flag = true ;
break ;
}
}
return flag ;
}