Browse Source

PermissionNode for el-tree

HOME 2 years ago
parent
commit
792a3f75fd

+ 17 - 0
VCommonCoreExample/AppServices/System/Dto/ElTreeNode.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+
+namespace VCommonCoreExample.AppServices.System.Dto
+{
+    [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
+    public class ElTreeNode
+    {
+        public string Label { get; set; }
+        public string Key { get; set; }
+        public IReadOnlyCollection<ElTreeNode> Children { get; set; }
+    }
+}

+ 3 - 2
VCommonCoreExample/AppServices/System/Roles/Dto/RoleFormPrepOutput.cs

@@ -1,15 +1,16 @@
 using System.Collections.Generic;
 using VCommon.VApplication.Authorization;
+using VCommonCoreExample.AppServices.System.Dto;
 
 namespace VCommonCoreExample.AppServices.System.Roles.Dto
 {
     public class RoleFormPrepOutput
     {
-        public IReadOnlyCollection<PermissionNodeOutput> AvailablePermissions { get; }
+        public IReadOnlyCollection<ElTreeNode> AvailablePermissions { get; }
 
         public RoleDto Output { get; set; }
 
-        public RoleFormPrepOutput(IReadOnlyCollection<PermissionNodeOutput> availablePermission)
+        public RoleFormPrepOutput(IReadOnlyCollection<ElTreeNode> availablePermission)
         {
             AvailablePermissions = availablePermission;
         }

+ 17 - 5
VCommonCoreExample/AppServices/System/Roles/RoleService.cs

@@ -1,10 +1,14 @@
 using System;
+using System.Collections.ObjectModel;
+using System.Linq;
+using VCommon.DataModel;
 using VCommon.VApplication.Authorization;
 using VCommon.VApplication.Dto;
 using VCommon.VApplication.Linq.Expressions;
 using VCommon.VAutoMapper;
 using VCommon.VOpenApi.VAspNetCore;
 using VCommonCoreExample.AppServices.Basic;
+using VCommonCoreExample.AppServices.System.Dto;
 using VCommonCoreExample.AppServices.System.Roles.Dto;
 using VCommonCoreExample.Authorization;
 using VCommonCoreExample.Caching;
@@ -38,7 +42,7 @@ namespace VCommonCoreExample.AppServices.System.Roles
         private readonly IPermissionManager _permissionManager;
         private readonly AuthCache _authCache;
 
-        public RoleService(IPermissionManager permissionManager,AuthCache authCache)
+        public RoleService(IPermissionManager permissionManager, AuthCache authCache)
         {
             _permissionManager = permissionManager;
             _authCache = authCache;
@@ -61,7 +65,15 @@ namespace VCommonCoreExample.AppServices.System.Roles
 
         public RoleFormPrepOutput FormPrep(Guid? input)
         {
-            var result = new RoleFormPrepOutput(_permissionManager.GetPermissionTreeOutput(VSession.Side));
+            var tree = _permissionManager.GetPermissionTreeOutput(VSession.Side);
+
+            var treeOutput = TreeCopy.CreateFrom<PermissionNodeOutput, ElTreeNode, string, string>(tree,
+                (k, v, c) => new ElTreeNode { Key = k, Label = v, Children = c.ToArray() },
+                p => p.Code,
+                p => p.Name, 
+                p => p.Children);
+
+            var result = new RoleFormPrepOutput(treeOutput.ToArray());
             if (input.HasValue)
             {
                 using var repo = GetRepository();
@@ -91,17 +103,17 @@ namespace VCommonCoreExample.AppServices.System.Roles
         {
             foreach (var s in input.PermissionSet)
             {
-                if (false == _permissionManager.ValidPermission(s))  throw new VFriendlyException($"无效的权限代码:{s}");
+                if (false == _permissionManager.ValidPermission(s)) throw new VFriendlyException($"无效的权限代码:{s}");
             }
 
             var repo = GetRepository();
-            if(repo.CheckExist(p => p.Id != input.Id && p.Name == input.Name)) throw new VFriendlyException("角色名称已经存在");
+            if (repo.CheckExist(p => p.Id != input.Id && p.Name == input.Name)) throw new VFriendlyException("角色名称已经存在");
 
             var entity = repo.GetEntityOrDefault(input.Id) ?? throw new VFriendlyException("找不到角色");
             input.MapTo(entity);
             repo.SaveChanges();
 
-            _authCache.ClearRole(VSession.TenantId,entity.Id);
+            _authCache.ClearRole(VSession.TenantId, entity.Id);
         }
 
         public void Delete(DeleteInput input)

+ 2 - 4
VCommonCoreExample/Startup.cs

@@ -1,13 +1,11 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Http;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
+using System;
+using System.Collections.Generic;
 using VCommon.Ioc;
 using VCommon.Logging;
 using VCommon.VApplication;