diff --git a/Infrastructure/Resolver/JsonPropertyContractResolver.cs b/Infrastructure/Resolver/JsonPropertyContractResolver.cs new file mode 100644 index 0000000..f2d1232 --- /dev/null +++ b/Infrastructure/Resolver/JsonPropertyContractResolver.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Newtonsoft.Json.Serialization; + +namespace ZR.Infrastructure.Resolver; + +public class JsonPropertyContractResolver : CamelCasePropertyNamesContractResolver +{ + private HashSet _propertySet; + + public JsonPropertyContractResolver(IEnumerable propertyNames) + { + if (propertyNames != null) + { + _propertySet = new HashSet(propertyNames, StringComparer.Ordinal); + } + } + + // protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) + // { + // var list = base.CreateProperties(type, memberSerialization).ToList(); + // var resList = list + // .Where(a => !this._lstInclude.Contains(a.PropertyName)) + // .ToList(); + // return resList; + // // .FindAll(p => _lstInclude.Contains(p.PropertyName)); + // } + + protected override List GetSerializableMembers(Type objectType) + { + List serializableMembers = null; + var allMembers = base.GetSerializableMembers(objectType); + + if (_propertySet != null && _propertySet.Count > 0) + { + serializableMembers = allMembers.Where(m => !_propertySet.Contains(m.Name)).ToList(); + } + return serializableMembers != null && serializableMembers.Count > 0 ? serializableMembers : allMembers; + } +} \ No newline at end of file diff --git a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs index aba7e36..789b3b1 100644 --- a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs @@ -9,6 +9,7 @@ using ZR.Model; using ZR.Service.System.IService; using ZR.ServiceCore.Model.Dto; using ZR.ServiceCore.Model.Generate; +using ZR.ServiceCore.Services.IService; namespace ZR.Admin.WebApi.Controllers { diff --git a/ZR.Admin.WebApi/Controllers/System/SysFieldController.cs b/ZR.Admin.WebApi/Controllers/System/SysFieldController.cs index 0779a64..8e24ceb 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysFieldController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysFieldController.cs @@ -1,4 +1,6 @@ using ZR.Admin.WebApi.Filters; +using ZR.ServiceCore.Model; +using ZR.ServiceCore.Services.IService; namespace ZR.Admin.WebApi.Controllers.System; @@ -6,5 +8,80 @@ namespace ZR.Admin.WebApi.Controllers.System; [Route("system/field")] public class SysFieldController : BaseController { - + private readonly ISysFieldService _sysFieldService; + + public SysFieldController(ISysFieldService sysFieldService) + { + _sysFieldService = sysFieldService; + } + + [HttpGet("getModelList")] + public async Task GetModelList() + { + var serviceCoreModels = AppDomain.CurrentDomain + .GetAssemblies() + .First(it => it.FullName.Contains("ZR.ServiceCore")) + .ExportedTypes + .Where(p => p.FullName.StartsWith("ZR.ServiceCore.Model")) + .Select(it => new + { + it.FullName, + // Properties = it.GetProperties() + }) + .ToList(); + return SUCCESS(serviceCoreModels); + } + + [HttpGet("getFields")] + public async Task GetFields([FromQuery] string fullName) + { + var list = await _sysFieldService.Queryable() + .Where(it => it.FullName == fullName) + .ToListAsync(); + return SUCCESS(list); + } + + [HttpPost("initFields")] + public async Task InitFields() + { + var serviceCoreModels = AppDomain.CurrentDomain + .GetAssemblies() + .First(it => it.FullName.Contains("ZR.ServiceCore")) + .ExportedTypes + .Where(p => p.FullName.StartsWith("ZR.ServiceCore.Model")) + .Select(it => new + { + FullName = it.FullName, + Properties = it.GetProperties().Select(pt => new + { + FieldName = pt.Name, + FieldType = pt.PropertyType.FullName, + IsClass = pt.PropertyType.IsClass, + IsArray = pt.PropertyType.IsArray, + // IsList = pt.PropertyType.IsClass ? pt.DeclaringType.FullName : string.Empty + }).ToList() + // Properties = it.GetProperties() + }) + .ToList(); + + foreach (var serviceCoreModel in serviceCoreModels) + { + var sysFields = new List(); + foreach (var property in serviceCoreModel.Properties) + { + var sysField = new SysField + { + FieldName = property.FieldName, + FullName = serviceCoreModel.FullName, + FieldType = property.FieldType, + IsClass = property.IsClass ? "1" : "0" + }; + sysFields.Add(sysField); + } + await _sysFieldService.Insertable(sysFields).ExecuteReturnSnowflakeIdListAsync(); + } + + + return SUCCESS(serviceCoreModels); + } } \ No newline at end of file diff --git a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs index ee0ccc4..25ddbe2 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs @@ -8,6 +8,7 @@ using ZR.Service.System; using ZR.Service.System.IService; using ZR.ServiceCore.Model; using ZR.ServiceCore.Model.Dto; +using ZR.ServiceCore.Services.IService; namespace ZR.Admin.WebApi.Controllers.System { diff --git a/ZR.Admin.WebApi/Controllers/System/SysMenuController.cs b/ZR.Admin.WebApi/Controllers/System/SysMenuController.cs index 075325c..e209512 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysMenuController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysMenuController.cs @@ -4,6 +4,7 @@ using ZR.Model.System; using ZR.Service.System.IService; using ZR.ServiceCore.Model; using ZR.ServiceCore.Model.Dto; +using ZR.ServiceCore.Services.IService; namespace ZR.Admin.WebApi.Controllers.System { diff --git a/ZR.Admin.WebApi/Controllers/System/SysRoleController.cs b/ZR.Admin.WebApi/Controllers/System/SysRoleController.cs index 927694c..ba04a71 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysRoleController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysRoleController.cs @@ -5,6 +5,7 @@ using ZR.Model.System; using ZR.Service.System.IService; using ZR.ServiceCore.Model; using ZR.ServiceCore.Model.Dto; +using ZR.ServiceCore.Services.IService; namespace ZR.Admin.WebApi.Controllers.System { diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index b62dc6c..d6903d0 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -7,12 +7,17 @@ using System.Text.Json; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Redis; using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using Quartz; using ZR.Admin.WebApi.Extensions; using ZR.Common.Cache; +using ZR.Infrastructure.Resolver; using ZR.Infrastructure.WebExtensions; +using ZR.ServiceCore.Model; using ZR.ServiceCore.Services.IService; using ZR.ServiceCore.Signalr; using ZR.ServiceCore.SqlSugar; +using IpRateLimitPolicy = AspNetCoreRateLimit.IpRateLimitPolicy; var builder = WebApplication.CreateBuilder(args); @@ -185,4 +190,33 @@ foreach (var assembly in serviceCoreModel) } +var user = new SysUser +{ + UserName = "admin", + Password = "123456" +}; +var props = new HashSet +{ + "Name", + "Children" +}; +var article = new Article +{ + Title = "aadad", + ArticleCategoryNav = new ArticleCategory + { + Name = "adsad", + Children = new List + { + new() + { + Name = "1213" + } + } + } +}; +var json = JsonConvert.SerializeObject(article, new JsonSerializerSettings +{ + ContractResolver = new JsonPropertyContractResolver(props) +}); app.Run(); \ No newline at end of file diff --git a/ZR.ServiceCore/Filters/DataFieldFilter.cs b/ZR.ServiceCore/Filters/DataFieldFilter.cs new file mode 100644 index 0000000..9651c48 --- /dev/null +++ b/ZR.ServiceCore/Filters/DataFieldFilter.cs @@ -0,0 +1,120 @@ +using System.Collections; +using Infrastructure; +using Infrastructure.Model; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Newtonsoft.Json.Serialization; +using NLog; + +namespace ZR.ServiceCore.Filters; +public class DataFieldFilter : ActionFilterAttribute +{ + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + public DataFieldFilter(Type resultType) + { + ResultType = resultType; + } + + public Type ResultType { get; set; } + + public string RulePath { get; set; } + + public override void OnResultExecuting(ResultExecutingContext context) + { + // var list = DbScoped.SugarScope.Queryable().ToList(); + // var list = DbScoped.SugarScope.Queryable() + var httpContext = context.HttpContext; + var url = httpContext.Request.Path; + var info = JwtUtil.GetLoginUser(context.HttpContext); + // 获取原始的结果 + var originalResult = context.Result as ContentResult; + var apiResult = (JsonConvert.DeserializeObject(originalResult?.Content!)) as ApiResult; + // 创建泛型类型 + // var resObj = typeof(JObject).GetMethod("ToObject", Type.EmptyTypes)? + // .MakeGenericMethod(ResultType).Invoke(apiResult?.Data, null)!; + // SetPropertiesToNull(resObj, new List + // { + // "Reqno" + // }); + // var json = JsonConvert.SerializeObject(resObj, new JsonSerializerSettings + // { + // ContractResolver = new CamelCasePropertyNamesContractResolver() + // }); + // var jObject = JObject.Parse(json); + // jObject.Add(new JProperty("props", new List + // { + // "Reqno" + // }.Select(ToLowerFirstLetter).ToList())); + // apiResult.Data = jObject; + context.Result = new ContentResult{ Content = JsonConvert.SerializeObject(apiResult, new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }), + ContentType = "application/json"} ; + base.OnResultExecuting(context); + } + + private static void SetPropertiesToNull(object obj, IEnumerable props) + { + var type = obj.GetType(); + var properties = type.GetProperties(); + foreach (var property in properties) + { + if (obj.IsEmpty()) + { + return; + } + if (props.Contains(property.Name)) + { + property.SetValue(obj, null); + } + else if (property.PropertyType.IsArray) + { + var arrayValue = (Array)property.GetValue(obj); + if (arrayValue != null) + { + foreach (var element in arrayValue) + { + SetPropertiesToNull(element, props); + } + } + } + else if (property.PropertyType.IsGenericType && + property.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) + { + // 属性的类型是 List + var listValue = (IEnumerable)property.GetValue(obj); + if (listValue != null) + { + foreach (var element in listValue) + { + SetPropertiesToNull(element, props); + } + } + } + else if (property.PropertyType.GetInterface("IDictionary") != null || + property.PropertyType.GetInterface("IDictionary`2") != null) + { + + } + else if (property.PropertyType.IsClass && property.PropertyType != typeof(string)) + { + var name = property.Name; + var prop = property.GetValue(obj); + SetPropertiesToNull(prop, props); + } + } + } + + private static string ToLowerFirstLetter(string str) + { + if (!str.IsNotEmpty()) + { + return str; + } + var charArray = str.ToCharArray(); + charArray[0] = char.ToLower(charArray[0]); + return new string(charArray); + } +} \ No newline at end of file diff --git a/ZR.ServiceCore/Model/SysField.cs b/ZR.ServiceCore/Model/SysField.cs index 2be2b30..d0b577e 100644 --- a/ZR.ServiceCore/Model/SysField.cs +++ b/ZR.ServiceCore/Model/SysField.cs @@ -11,4 +11,8 @@ public class SysField public string FieldName { get; set; } public string FullName { get; set; } + + public string FieldType { get; set; } + + public string IsClass { get; set; } } \ No newline at end of file diff --git a/ZR.ServiceCore/Services/IService/ISysFieldService.cs b/ZR.ServiceCore/Services/IService/ISysFieldService.cs new file mode 100644 index 0000000..da6e010 --- /dev/null +++ b/ZR.ServiceCore/Services/IService/ISysFieldService.cs @@ -0,0 +1,14 @@ +using ZR.Service; +using ZR.ServiceCore.Model; + +namespace ZR.ServiceCore.Services.IService; + +public interface ISysFieldService : IBaseService +{ + +} + +public interface ISysRoleFieldService : IBaseService +{ + +} \ No newline at end of file diff --git a/ZR.ServiceCore/Services/IService/ISysMenuService.cs b/ZR.ServiceCore/Services/IService/ISysMenuService.cs index a30e023..2cce447 100644 --- a/ZR.ServiceCore/Services/IService/ISysMenuService.cs +++ b/ZR.ServiceCore/Services/IService/ISysMenuService.cs @@ -1,11 +1,10 @@ -using System.Collections.Generic; -using ZR.Model.System; +using ZR.Service; using ZR.ServiceCore.Model; using ZR.ServiceCore.Model.Dto; using ZR.ServiceCore.Model.Generate; using ZR.ServiceCore.Model.Vo; -namespace ZR.Service.System.IService +namespace ZR.ServiceCore.Services.IService { public interface ISysMenuService : IBaseService { diff --git a/ZR.ServiceCore/Services/SysFieldService.cs b/ZR.ServiceCore/Services/SysFieldService.cs new file mode 100644 index 0000000..3815387 --- /dev/null +++ b/ZR.ServiceCore/Services/SysFieldService.cs @@ -0,0 +1,12 @@ +using Infrastructure.Attribute; +using ZR.Service; +using ZR.ServiceCore.Model; +using ZR.ServiceCore.Services.IService; + +namespace ZR.ServiceCore.Services; +[AppService(ServiceType = typeof(ISysFieldService), ServiceLifetime = LifeTime.Transient)] +public class SysFieldService : BaseService, ISysFieldService +{ + +} + diff --git a/ZR.ServiceCore/Services/SysMenuService.cs b/ZR.ServiceCore/Services/SysMenuService.cs index 0f1e327..d459261 100644 --- a/ZR.ServiceCore/Services/SysMenuService.cs +++ b/ZR.ServiceCore/Services/SysMenuService.cs @@ -13,6 +13,7 @@ using ZR.ServiceCore.Model.Dto; using ZR.ServiceCore.Model.Enums; using ZR.ServiceCore.Model.Generate; using ZR.ServiceCore.Model.Vo; +using ZR.ServiceCore.Services.IService; namespace ZR.Service { diff --git a/ZR.ServiceCore/Services/SysPermissionService.cs b/ZR.ServiceCore/Services/SysPermissionService.cs index b9aef33..721112a 100644 --- a/ZR.ServiceCore/Services/SysPermissionService.cs +++ b/ZR.ServiceCore/Services/SysPermissionService.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using ZR.Model.System; using ZR.Service.System.IService; using ZR.ServiceCore.Model; +using ZR.ServiceCore.Services.IService; namespace ZR.Service.System { diff --git a/ZR.ServiceCore/Services/SysRoleMenuService.cs b/ZR.ServiceCore/Services/SysRoleMenuService.cs index a8b145d..091d58c 100644 --- a/ZR.ServiceCore/Services/SysRoleMenuService.cs +++ b/ZR.ServiceCore/Services/SysRoleMenuService.cs @@ -2,6 +2,7 @@ using ZR.Model.System; using ZR.Service.System.IService; using ZR.ServiceCore.Model; +using ZR.ServiceCore.Services.IService; namespace ZR.Service.System { diff --git a/ZR.ServiceCore/Services/SysRoleService.cs b/ZR.ServiceCore/Services/SysRoleService.cs index 45da1cd..9762dc5 100644 --- a/ZR.ServiceCore/Services/SysRoleService.cs +++ b/ZR.ServiceCore/Services/SysRoleService.cs @@ -11,6 +11,7 @@ using ZR.Repository; using ZR.Service.System.IService; using ZR.ServiceCore.Model; using ZR.ServiceCore.Model.Dto; +using ZR.ServiceCore.Services.IService; namespace ZR.Service {