团餐订单
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.

DtoValidator.cs 5.6 KiB

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