合并菜单项

This commit is contained in:
xielue 2023-05-09 16:38:12 +08:00
parent 3537289f7d
commit 73f70b5b4b
4 changed files with 75 additions and 24 deletions

View File

@ -1,5 +1,5 @@
VUE_APP_API_BASE_URL=http://zxx4.f3322.net:46000 VUE_APP_API_BASE_URL=http://zxx4.f3322.net:46000
VUE_APP_USER_MODEL=AdminUser VUE_APP_USER_MODEL2=AdminUser
VUE_APP_USER_MODEL2=BaseAdmin VUE_APP_USER_MODEL=BaseAdmin
VUE_APP_BEID=1 VUE_APP_BEID=1

View File

@ -71,7 +71,12 @@ export default {
getRoutesConfig().then(result => { // getRoutesConfig().then(result => { //
if (result.data.data != null) { if (result.data.data != null) {
const routesConfig = result.data.data
const routesConfig = result.data.data;
this.setRoutesConfig(routesConfig) this.setRoutesConfig(routesConfig)
loadRoutes([routesConfig]) loadRoutes([routesConfig])
@ -104,7 +109,5 @@ export default {
} }
</script> </script>
<style lang="less" scoped> <style lang="less" scoped></style>
</style>

View File

@ -21,7 +21,7 @@ routerMap['basic']= {
permission: [], permission: [],
} }
}; };
/*
routerMap['basic_materials']= { routerMap['basic_materials']= {
name: '基础档案', name: '基础档案',
icon: 'idcard', icon: 'idcard',
@ -47,5 +47,6 @@ routerMap['basic_unit']= {
permission: [], permission: [],
} }
}; };
*/
export default {} export default routerMap

View File

@ -116,6 +116,50 @@ function hasPermission(permissions, route) {
} }
} }
//合并菜单项
function mergeMenus(routesConfig) {
if (!routesConfig) return routesConfig;
let root = routesConfig[0];
let routes = [];
let groups = ['basic']; //要合并的菜单项
let appendedGroups = []; //已经处理的菜单项
let children = JSON.parse(JSON.stringify(root.children));
console.log(children)
root.children.forEach(item => {
if (appendedGroups.filter(a => a == item.router).length) {
return;
}
if (groups.filter(a => a == item.router).length) {
let newItem = {
authority: item.authority,
icon: item.icon,
name: item.name,
router: item.router,
children: []
};
children.forEach(subitem => {
if (subitem.router == item.router) {
subitem.children = subitem.children || [];
newItem.children = [...newItem.children, ...subitem.children]; //合并子菜单项
}
});
routes.push(newItem);
appendedGroups.push(item.router);
} else {
routes.push(item);
}
});
root.children = routes;
return [root];
}
/** /**
* 加载路由 * 加载路由
@ -137,6 +181,12 @@ function loadRoutes(routesConfig) { // 加载路由
// } // }
/*************** 兼容 version < v0.6.1 *****************/ /*************** 兼容 version < v0.6.1 *****************/
console.log(routesConfig)
routesConfig = mergeMenus(routesConfig);
if (!routesConfig) {
return;
}
// 应用配置 // 应用配置
const { router, store, i18n } = appOptions const { router, store, i18n } = appOptions
@ -157,7 +207,6 @@ function loadRoutes(routesConfig) { // 加载路由
// 如果开启了异步路由,则加载异步路由配置 // 如果开启了异步路由,则加载异步路由配置
const asyncRoutes = store.state.setting.asyncRoutes // 获取store的setting模块的asyncRoutes const asyncRoutes = store.state.setting.asyncRoutes // 获取store的setting模块的asyncRoutes
if (asyncRoutes) { // 如果动态路由存在 if (asyncRoutes) { // 如果动态路由存在
console.log("routesConfig && routesConfig.length > 0", routesConfig.length > 0)
if (routesConfig && routesConfig.length > 0) { // 如果本地路由配置存在 并且 数量大于0 if (routesConfig && routesConfig.length > 0) { // 如果本地路由配置存在 并且 数量大于0
const routes = parseRoutes(routesConfig, routerMap) // 解析路由 const routes = parseRoutes(routesConfig, routerMap) // 解析路由
@ -166,9 +215,7 @@ function loadRoutes(routesConfig) { // 加载路由
const finalRoutes = mergeRoutes(basicOptions.routes, routes) const finalRoutes = mergeRoutes(basicOptions.routes, routes)
// 格式化路由 // 格式化路由
formatRoutes(finalRoutes) formatRoutes(finalRoutes)
console.log('最终路由表finalRoutes:', finalRoutes) // 最终路由表
router.options = { ...router.options, routes: finalRoutes } // 路由配置 router.options = { ...router.options, routes: finalRoutes } // 路由配置
console.log(router.options)
router.matcher = new Router({ ...router.options, routes: [] }).matcher // 重置路由 router.matcher = new Router({ ...router.options, routes: [] }).matcher // 重置路由
router.addRoutes(finalRoutes) // 添加路由 router.addRoutes(finalRoutes) // 添加路由