团餐订单
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

160 lines
5.6 KiB

  1. using Furion.JsonSerialization;
  2. using Newtonsoft.Json;
  3. using NPOI.POIFS.Crypt.Dsig;
  4. using NPOI.SS.Formula.Functions;
  5. using NPOI.Util.ArrayExtensions;
  6. using System.Collections.Generic;
  7. using System.ComponentModel.DataAnnotations;
  8. using System.Reflection;
  9. using System.Text;
  10. namespace BPA.KitChen.GroupMealOrder.Application.BaseDto
  11. {
  12. /// <summary>
  13. /// Dto参数验证
  14. /// </summary>
  15. public static class DtoValidator
  16. {
  17. ///// <summary>
  18. ///// 获取签名
  19. ///// </summary>
  20. ///// <typeparam name="T"></typeparam>
  21. ///// <param name="t"></param>
  22. ///// <param name="otype">0-不排序 1-按名称ASCII排序</param>
  23. ///// <returns></returns>
  24. //public static string GetSign<T>(T t, int otype = 1)
  25. //{
  26. // string retstr = "";
  27. // //定义PropertyInfo的List
  28. // List<PropertyInfo> proplist = new List<PropertyInfo>();
  29. // //遍历泛型类的每个属性加入到List里面
  30. // Array.ForEach<PropertyInfo>(typeof(T).GetProperties(),
  31. // p => proplist.Add(p));
  32. // //根据参数进行排序 0-不排序 1-按名称ASCII码排序
  33. // if (otype == 1)
  34. // proplist = proplist.OrderBy(k => k.Name).ToList();
  35. // //遍历List泛型生成我们要签名的字符串
  36. // proplist.ForEach(p =>
  37. // {
  38. // if (p.Name.ToLower() != "sign".ToLower())
  39. // {
  40. // if (p.GetValue(t, null) != null && p.GetValue(t, null).ToString() != "")
  41. // {
  42. // var type = p.GetValue(t, null).GetType().FullName;
  43. // if (type == "System.String" || type == "System.Boolean" || type == "System.Int32" || type == "System.DateTime")
  44. // {
  45. // retstr = retstr + p.Name + "=" + p.GetValue(t, null) + "&";
  46. // }
  47. // else
  48. // {
  49. // retstr = retstr + p.Name + "=" + JsonConvert.SerializeObject(p.GetValue(t, null)) + "&";
  50. // }
  51. // }
  52. // }
  53. // });
  54. // //把字符串最后一位截断
  55. // retstr = retstr.Substring(0, retstr.Length - 1);
  56. // //输出字符串
  57. // return retstr;
  58. //}
  59. /// <summary>
  60. /// 获取签名
  61. /// </summary>
  62. /// <param name="t"></param>
  63. /// <returns></returns>
  64. public static string GetSign(object t)
  65. {
  66. string retstr = "";
  67. //定义PropertyInfo的List
  68. List<PropertyInfo> proplist = new List<PropertyInfo>();
  69. //遍历泛型类的每个属性加入到List里面
  70. Array.ForEach<PropertyInfo>(t.GetType().GetProperties(),
  71. p => proplist.Add(p));
  72. //根据参数进行排序 0-不排序 1-按名称ASCII码排序
  73. proplist = proplist.OrderBy(k => k.Name).ToList();
  74. //遍历List泛型生成我们要签名的字符串
  75. proplist.ForEach(p =>
  76. {
  77. if (p.Name.ToLower() != "sign".ToLower())
  78. {
  79. if (p.GetValue(t, null) != null && p.GetValue(t, null).ToString() != "")
  80. {
  81. var type = p.GetValue(t, null).GetType().FullName;
  82. if (type == "System.String" || type == "System.Boolean" || type == "System.Int32" || type == "System.DateTime")
  83. {
  84. retstr = retstr + p.Name + "=" + p.GetValue(t, null) + "&";
  85. }
  86. else
  87. {
  88. var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
  89. retstr = retstr + p.Name + "=" + JsonConvert.SerializeObject(p.GetValue(t, null), Formatting.None, jsonSetting) + "&";
  90. }
  91. }
  92. }
  93. });
  94. //把字符串最后一位截断
  95. retstr = retstr.Substring(0, retstr.Length - 1);
  96. //输出字符串
  97. return retstr;
  98. }
  99. /// <summary>
  100. /// 获取属性值
  101. /// </summary>
  102. /// <param name="obj"></param>
  103. /// <param name="name"></param>
  104. /// <returns></returns>
  105. public static string GetAttributePrice(object obj, string name)
  106. {
  107. string retstr = "";
  108. //定义PropertyInfo的List
  109. List<PropertyInfo> proplist = new List<PropertyInfo>();
  110. //遍历泛型类的每个属性加入到List里面
  111. Array.ForEach<PropertyInfo>(obj.GetType().GetProperties(),
  112. p => proplist.Add(p));
  113. //根据参数进行排序 0-不排序 1-按名称ASCII码排序
  114. proplist = proplist.OrderBy(k => k.Name).ToList();
  115. //遍历List泛型生成我们要签名的字符串
  116. proplist.ForEach(p =>
  117. {
  118. if (p.Name.ToLower() == name.ToLower())
  119. {
  120. if (p.GetValue(obj, null).GetType().FullName.Contains("System.Collections.Generic.List"))
  121. {
  122. retstr = JsonConvert.SerializeObject(p.GetValue(obj, null));
  123. }
  124. else
  125. {
  126. retstr = p.GetValue(obj, null)?.ToString();
  127. }
  128. }
  129. });
  130. //输出字符串
  131. return retstr;
  132. }
  133. }
  134. }