Seamus há 4 meses atrás
pai
commit
f0b77b5a17

+ 1 - 1
ERP.Core/Controller/SysMenuController.cs

@@ -27,7 +27,7 @@ namespace ERP.Core.Controller
         {
             var userId = LoginHelper.GetUserId();
 
-            var menuList = _sysMenuService.SelectMenuListByUserId(userId);
+            var menuList = _sysMenuService.SelectMenuTreeByUserId(userId);
 
             var result = _sysMenuService.BuildMenuTree(menuList, string.Empty);
 

+ 1 - 1
ERP.Core/CoreDbContext.cs

@@ -114,7 +114,7 @@ namespace ERP.Core
         private static bool IsBaseEntityDerived(Type type)
         {
             // 检查类型是否是BaseEntity或其派生类
-            return type == typeof(BaseEntity) || IsDerivedFromGenericBase(type, typeof(TreeEntity<>));
+            return type.BaseType == typeof(BaseEntity) || IsDerivedFromGenericBase(type, typeof(TreeEntity<>));
         }
 
         private static bool IsDerivedFromGenericBase(Type type, Type genericBaseType)

+ 1 - 1
ERP.Core/Dto/Router.cs

@@ -34,6 +34,6 @@ namespace ERP.Core.Dto
         /// <summary>
         /// 孩子
         /// </summary>
-        public virtual List<Router>? Chindren { get; set; }
+        public virtual List<Router>? Children { get; set; }
     }
 }

+ 1 - 1
ERP.Core/Interface/ISysMenuService.cs

@@ -9,7 +9,7 @@ namespace ERP.Core.Interface
 {
     public interface ISysMenuService
     {
-        List<SysMenu> SelectMenuListByUserId(long userId);
+        List<SysMenu> SelectMenuTreeByUserId(long userId);
 
         List<Router> BuildMenuTree(List<SysMenu> list, string parentPath);
     }

+ 1 - 0
ERP.Core/Service/AuthService.cs

@@ -71,6 +71,7 @@ namespace ERP.Core.Service
             {
                 UserId = user.Id,
                 UserName = user.UserName,
+                NickName = user.NickName,
                 Avatar = user.Avatar,
                 Email = user.Email,
                 UserStatus = user.UserStatus,

+ 6 - 5
ERP.Core/Service/SysMenuService.cs

@@ -8,6 +8,7 @@ using ERP.Core.Entity;
 using ERP.Core.Interface;
 using ERP.Core.Repository;
 using ERP.Framework.Security;
+using ERP.Framework.Utils;
 
 namespace ERP.Core.Service
 {
@@ -28,7 +29,7 @@ namespace ERP.Core.Service
         /// 获取
         /// </summary>
         /// <returns></returns>
-        public List<SysMenu> SelectMenuListByUserId(long userId)
+        public List<SysMenu> SelectMenuTreeByUserId(long userId)
         {
             List<SysMenu> result;
 
@@ -41,7 +42,7 @@ namespace ERP.Core.Service
                 result = _sysMenuRepository.SelectMenuListByUserId(userId);
             }
 
-            return result;
+            return TreeUtil.Build(result);
         }
 
         public List<Router> BuildMenuTree(List<SysMenu> list, string parentPath)
@@ -53,7 +54,7 @@ namespace ERP.Core.Service
                 var router = new Router()
                 {
                     Hidden = menu.Visible,
-                    Name = menu.Path.,
+                    Name = menu.Path,
                     Path = GetRoutePath(menu, parentPath),
                     Component = GetComponent(menu),
                     Meta = new Meta()
@@ -67,7 +68,7 @@ namespace ERP.Core.Service
                 var cMenus = menu.Children;
                 if (cMenus != null && menu.MenuType == Enum.MenuTypeEnum.Catalog)
                 {
-                    router.Chindren = BuildMenuTree(cMenus, router.Path);
+                    router.Children = BuildMenuTree(cMenus, router.Path);
                 }
                 else if (IsMenuFrame(menu))
                 {
@@ -84,7 +85,7 @@ namespace ERP.Core.Service
                         }
                     };
                     childrenList.Add(children);
-                    router.Chindren = childrenList;
+                    router.Children = childrenList;
                 }
 
                 routers.Add(router);

+ 7 - 1
ERP.Framework/Security/Core/LoginUser.cs

@@ -10,10 +10,16 @@ namespace ERP.Framework.Security.Core
         public long UserId { get; set; }
 
         /// <summary>
-        ///
+        /// 用户名
         /// </summary>
         public string UserName { get; set; } = string.Empty;
 
+
+        /// <summary>
+        /// 名称
+        /// </summary>
+        public string NickName { get; set; } = string.Empty;
+
         /// <summary>
         /// 用户状态
         /// </summary>

+ 44 - 1
ERP.Framework/Utils/TreeUtil.cs

@@ -2,12 +2,55 @@
 // <date></date>
 // <description></description>
 
+using ERP.Framework.WebApi;
+
 namespace ERP.Framework.Utils
 {
     public class TreeUtil
     {
-        public static List<T> Build() where T : TreeEntity<T
+        public static List<T> Build<T>(List<T> list) where T : TreeEntity<T>
+        {
+            var result = new List<T>();
+            var tempIdList = new List<long>();
+
+            foreach (var item in list)
+            {
+                tempIdList.Add(item.Id);
+            }
+
+            foreach (var item in list)
+            {
+                if (!tempIdList.Contains(item.ParentId))
+                {
+                    RecursionFn(list, item);
+                    result.Add(item);
+                }
+            }
+
+            if (result.Count == 0)
+            {
+                result = list;
+            }
+
+            return result;
+        }
+
+        private static List<T> GetChildList<T>(List<T> list, T t) where T : TreeEntity<T>
+        {
+            return list.Where(l => l.ParentId == t.Id).ToList();
+        }
+
+        private static void RecursionFn<T>(List<T> list, T t) where T : TreeEntity<T>
         {
+            List<T> childList = GetChildList(list, t);
+            t.Children = childList;
+            foreach (var tChild in childList)
+            {
+                if (GetChildList(list, t).Count > 0)
+                {
+                    RecursionFn(list, tChild);
+                }
+            }
         }
     }
 }