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.
 
 

151 lines
5.4 KiB

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