diff --git a/admin/src/utils/parsing.js b/admin/src/utils/parsing.js index a0a6caf..f9adf54 100644 --- a/admin/src/utils/parsing.js +++ b/admin/src/utils/parsing.js @@ -169,52 +169,36 @@ export function handleTree(data, id, parentId, children) { * 路由数据遍历 * */ - export function formatRoutes(routerArr){ - let arr = [],obj = {}; - routerArr.forEach(tmp => { - obj = { - id:tmp.id, - pid:tmp.pid, - name:tmp.name, - url:tmp.component, - path:'/' + tmp.pid + '/', - perms:tmp.perms, - child:tmp.childList.length ? tmp.childList.map(item=>{ - return { - id:item.id, - pid:item.pid, - name:item.name, - url:item.component, - path:'/' + tmp.pid + '/' + item.pid + '/', - perms:item.perms, - extra:item.icon, - child:item.childList.length ? item.childList.map(item1=>{ - return { - id:item1.id, - pid:item1.pid, - name:item1.name, - url:item1.component, - path:'/' + tmp.pid + '/' + item.pid + '/' + item1.pid + '/', - perms:item1.perms, - extra:item1.icon, - child:item1.childList.length ? item1.childList.map(item2=>{ - return { - id:item2.id, - pid:item2.pid, - name:item2.name, - url:item2.component, - path:'/' + tmp.pid + '/' + item.pid + '/' + item1.pid + '/' + item2.pid + '/', - perms:item2.perms, - extra:item2.icon, - } - }) : [] - } - }) : [] - } - }) : [], - extra:tmp.icon, +export function formatRoutes(routerArr) { + /** + * 递归处理单个路由项 + * @param {Object} routeItem 路由项 + * @param {String} parentPath 父级路径,用于构建完整路径 + * @returns {Object} 格式化后的路由对象 + */ + function formatRouteItem(routeItem, parentPath = '') { + const currentPath = parentPath ? `${parentPath}${routeItem.pid}/` : `/${routeItem.pid}/`; + + const formattedRoute = { + id: routeItem.id, + pid: routeItem.pid, + name: routeItem.name, + url: routeItem.component, + path: currentPath, + perms: routeItem.perms, + child: [], + extra: routeItem.icon + }; + + // 递归处理子路由 + if (routeItem.childList && routeItem.childList.length > 0) { + formattedRoute.child = routeItem.childList.map(child => + formatRouteItem(child, currentPath) + ); } - arr.push(obj); - }) - return arr; - } \ No newline at end of file + + return formattedRoute; + } + + return routerArr.map(item => formatRouteItem(item)); +} \ No newline at end of file