diff --git a/AGSS/Services/MenuService.cs b/AGSS/Services/MenuService.cs index 90766fa..346d426 100644 --- a/AGSS/Services/MenuService.cs +++ b/AGSS/Services/MenuService.cs @@ -266,9 +266,10 @@ public class MenuService /// private List BuildMenuTree(List allMenus) { - var menuDict = allMenus.ToDictionary(m => m.Uuid); + var menuResponseDict = new Dictionary(); var rootMenus = new List(); + // 第一步:创建所有菜单的响应对象 foreach (var menu in allMenus) { var menuResponse = new MenuResponse @@ -289,6 +290,14 @@ public class MenuService UpdateTime = menu.UpdateTime }; + menuResponseDict[menu.Uuid] = menuResponse; + } + + // 第二步:构建树形结构 + foreach (var menu in allMenus) + { + var menuResponse = menuResponseDict[menu.Uuid]; + if (string.IsNullOrEmpty(menu.ParentId)) { // 根菜单 @@ -297,13 +306,9 @@ public class MenuService else { // 子菜单 - if (menuDict.TryGetValue(menu.ParentId, out var parentMenu)) + if (menuResponseDict.TryGetValue(menu.ParentId, out var parentResponse)) { - var parentResponse = FindMenuResponse(rootMenus, menu.ParentId); - if (parentResponse != null) - { - parentResponse.Children.Add(menuResponse); - } + parentResponse.Children.Add(menuResponse); } } } @@ -311,25 +316,5 @@ public class MenuService return rootMenus; } - /// - /// 在菜单树中查找指定UUID的菜单响应对象 - /// - private MenuResponse? FindMenuResponse(List menus, string uuid) - { - foreach (var menu in menus) - { - if (menu.Uuid == uuid) - { - return menu; - } - var found = FindMenuResponse(menu.Children, uuid); - if (found != null) - { - return found; - } - } - - return null; - } } \ No newline at end of file