QueryModelBinder.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Microsoft.AspNetCore.Mvc.ModelBinding;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using XYY.Common.Standard;
  7. using System.Linq;
  8. using System.Web;
  9. using System.Text.RegularExpressions;
  10. using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
  11. namespace XYY.Core.Standard.Mvc
  12. {
  13. public class QueryModelBinderProvider : IModelBinderProvider
  14. {
  15. public IModelBinder GetBinder(ModelBinderProviderContext context)
  16. {
  17. if (context == null)
  18. {
  19. throw new ArgumentNullException(nameof(context));
  20. }
  21. if (context.Metadata.ModelType == typeof(QueryModel))
  22. {
  23. return new BinderTypeModelBinder(typeof(QueryModelBinder));
  24. }
  25. return null;
  26. }
  27. }
  28. public class QueryModelBinder : IModelBinder
  29. {
  30. public Task BindModelAsync(ModelBindingContext bindingContext)
  31. {
  32. var body = bindingContext.HttpContext.Request.Body;
  33. byte[] b = new byte[bindingContext.HttpContext.Request.ContentLength ?? 0];
  34. body.Read(b, 0, b.Length);
  35. string str = System.Text.Encoding.Default.GetString(b);
  36. var valueProvider = bindingContext.ValueProvider;
  37. var modelName = bindingContext.ModelName;
  38. var valueProviderResult = valueProvider.GetValue(modelName);
  39. if (valueProviderResult == ValueProviderResult.None)
  40. {
  41. return Task.CompletedTask;
  42. }
  43. string sortField = bindingContext.ActionContext.HttpContext.Request.Query["sort"];
  44. string sortDirection = bindingContext.ActionContext.HttpContext.Request.Query["order"];
  45. var dict = bindingContext.HttpContext.Request.Query;
  46. var keys = dict.Where(c => c.Key.StartsWith("_"));//只有"["开头的为需要处理的
  47. List<QueryParamer> queryParamer = new List<QueryParamer>();
  48. if (keys.Count() != 0)
  49. {
  50. foreach (var key in keys)
  51. {
  52. if (!key.Key.StartsWith("_")) continue;
  53. var val = key.Value;
  54. //处理无值的情况
  55. if (string.IsNullOrEmpty(val)) continue;
  56. //if (val.ToLower().Contains("wp-"))
  57. //{
  58. // val = val.ToLower().Replace("wp-", "").TryConvertInt32().ToString();
  59. //}
  60. var paramer = AddParamer(key.Key, val);
  61. if (paramer != null)
  62. {
  63. queryParamer.Add(paramer);
  64. }
  65. }
  66. }
  67. if (!int.TryParse(bindingContext.ActionContext.HttpContext.Request.Query["limit"], out int pageSize))
  68. pageSize = 20;
  69. if (!int.TryParse(bindingContext.ActionContext.HttpContext.Request.Query["offset"], out int pageIndex))
  70. pageIndex = 1;
  71. List<QuerySort> sort = new List<QuerySort>();
  72. if (!string.IsNullOrWhiteSpace(sortField))
  73. sort.Add(new QuerySort() { Field = sortField, IsDesc = (sortDirection == "desc") });
  74. var m = new QueryModel()
  75. {
  76. PageSize = pageSize,
  77. PageIndex = pageIndex / pageSize + 1,
  78. QuerySort = sort,
  79. QueryParamer = queryParamer
  80. };
  81. bindingContext.Result = ModelBindingResult.Success(m);
  82. return Task.CompletedTask;
  83. }
  84. private QueryParamer AddParamer(string item, string value)
  85. {
  86. string[] strArr = item.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
  87. if (strArr.Length != 2)
  88. return null;
  89. string method = "", field = "";
  90. method = strArr[0];
  91. field = strArr[1];
  92. string decVal = HttpUtility.UrlDecode(value, Encoding.UTF8);
  93. //html datetime-local的时间格式
  94. if (Regex.Match(decVal, @"\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}").Success && DateTime.TryParse(decVal, out var dateTime))
  95. {
  96. decVal = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
  97. }
  98. return new QueryParamer()
  99. {
  100. Filed = field.Replace("[", "").Replace("]", ""),
  101. Value = decVal,
  102. Method = method
  103. };
  104. }
  105. }
  106. }