using Furion.JsonSerialization;
using Newtonsoft.Json;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using NPOI.Util.ArrayExtensions;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Text;
namespace BPA.KitChen.GroupMealOrder.Application.BaseDto
{
///
/// Dto参数验证
///
public static class DtoValidator
{
/////
///// 获取签名
/////
/////
/////
///// 0-不排序 1-按名称ASCII排序
/////
//public static string GetSign(T t, int otype = 1)
//{
// string retstr = "";
// //定义PropertyInfo的List
// List proplist = new List();
// //遍历泛型类的每个属性加入到List里面
// Array.ForEach(typeof(T).GetProperties(),
// p => proplist.Add(p));
// //根据参数进行排序 0-不排序 1-按名称ASCII码排序
// if (otype == 1)
// proplist = proplist.OrderBy(k => k.Name).ToList();
// //遍历List泛型生成我们要签名的字符串
// proplist.ForEach(p =>
// {
// if (p.Name.ToLower() != "sign".ToLower())
// {
// if (p.GetValue(t, null) != null && p.GetValue(t, null).ToString() != "")
// {
// var type = p.GetValue(t, null).GetType().FullName;
// if (type == "System.String" || type == "System.Boolean" || type == "System.Int32" || type == "System.DateTime")
// {
// retstr = retstr + p.Name + "=" + p.GetValue(t, null) + "&";
// }
// else
// {
// retstr = retstr + p.Name + "=" + JsonConvert.SerializeObject(p.GetValue(t, null)) + "&";
// }
// }
// }
// });
// //把字符串最后一位截断
// retstr = retstr.Substring(0, retstr.Length - 1);
// //输出字符串
// return retstr;
//}
///
/// 获取签名
///
///
///
public static string GetSign(object t)
{
string retstr = "";
//定义PropertyInfo的List
List proplist = new List();
//遍历泛型类的每个属性加入到List里面
Array.ForEach(t.GetType().GetProperties(),
p => proplist.Add(p));
//根据参数进行排序 0-不排序 1-按名称ASCII码排序
proplist = proplist.OrderBy(k => k.Name).ToList();
//遍历List泛型生成我们要签名的字符串
proplist.ForEach(p =>
{
if (p.Name.ToLower() != "sign".ToLower())
{
if (p.GetValue(t, null) != null && p.GetValue(t, null).ToString() != "")
{
var type = p.GetValue(t, null).GetType().FullName;
if (type == "System.String" || type == "System.Boolean" || type == "System.Int32" || type == "System.DateTime")
{
retstr = retstr + p.Name + "=" + p.GetValue(t, null) + "&";
}
else
{
var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
retstr = retstr + p.Name + "=" + JsonConvert.SerializeObject(p.GetValue(t, null), Formatting.None, jsonSetting) + "&";
}
}
}
});
//把字符串最后一位截断
retstr = retstr.Substring(0, retstr.Length - 1);
//输出字符串
return retstr;
}
///
/// 获取属性值
///
///
///
///
public static string GetAttributePrice(object obj, string name)
{
string retstr = "";
//定义PropertyInfo的List
List proplist = new List();
//遍历泛型类的每个属性加入到List里面
Array.ForEach(obj.GetType().GetProperties(),
p => proplist.Add(p));
//根据参数进行排序 0-不排序 1-按名称ASCII码排序
proplist = proplist.OrderBy(k => k.Name).ToList();
//遍历List泛型生成我们要签名的字符串
proplist.ForEach(p =>
{
if (p.Name.ToLower() == name.ToLower())
{
if (p.GetValue(obj, null).GetType().FullName.Contains("System.Collections.Generic.List"))
{
retstr = JsonConvert.SerializeObject(p.GetValue(obj, null));
}
else
{
retstr = p.GetValue(obj, null)?.ToString();
}
}
});
//输出字符串
return retstr;
}
}
}