新增数据字段拦截器,json序列化解析器
This commit is contained in:
@@ -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<PoRequirement>().ToList();
|
||||
// var list = DbScoped.SugarScope.Queryable<S>()
|
||||
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<ApiResult>(originalResult?.Content!)) as ApiResult;
|
||||
// 创建泛型类型
|
||||
// var resObj = typeof(JObject).GetMethod("ToObject", Type.EmptyTypes)?
|
||||
// .MakeGenericMethod(ResultType).Invoke(apiResult?.Data, null)!;
|
||||
// SetPropertiesToNull(resObj, new List<string>
|
||||
// {
|
||||
// "Reqno"
|
||||
// });
|
||||
// var json = JsonConvert.SerializeObject(resObj, new JsonSerializerSettings
|
||||
// {
|
||||
// ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
// });
|
||||
// var jObject = JObject.Parse(json);
|
||||
// jObject.Add(new JProperty("props", new List<string>
|
||||
// {
|
||||
// "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<string> 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<T>
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using ZR.Service;
|
||||
using ZR.ServiceCore.Model;
|
||||
|
||||
namespace ZR.ServiceCore.Services.IService;
|
||||
|
||||
public interface ISysFieldService : IBaseService<SysField>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface ISysRoleFieldService : IBaseService<SysRoleField>
|
||||
{
|
||||
|
||||
}
|
||||
@@ -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<SysMenu>
|
||||
{
|
||||
|
||||
@@ -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<SysField>, ISysFieldService
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user