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.

81 lines
2.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Apewer.Run
  5. {
  6. public class Web
  7. {
  8. public Web()
  9. {
  10. // GetParameter(Json.Parse("{\"status\":2,\"after\":0,\"before\":0}"), "status");
  11. }
  12. string GetParameter(Json data, params string[] names)
  13. {
  14. if (names == null || names.Length < 1) return null;
  15. var sameNames = new List<string>();
  16. var lowerNames = new List<string>();
  17. foreach (var name in names)
  18. {
  19. if (string.IsNullOrEmpty(name)) continue;
  20. sameNames.Add(name);
  21. var lower = TextUtility.ToLower(name);
  22. if (string.IsNullOrEmpty(lower)) continue;
  23. lowerNames.Add(lower);
  24. }
  25. // POST 优先。
  26. if (data != null && data.IsObject)
  27. {
  28. var properties = data.GetProperties();
  29. if (properties != null)
  30. {
  31. // Json 区分大小写,先全字匹配。
  32. foreach (var property in properties)
  33. {
  34. if (!property.IsProperty) continue;
  35. if (sameNames.Contains(property.Name) && property.Value != null)
  36. {
  37. var value = property.Value;
  38. if (value == null) continue;
  39. var text = value.ToString();
  40. if (!string.IsNullOrEmpty(text)) return text;
  41. }
  42. }
  43. // 以小写模糊匹配。
  44. foreach (var property in properties)
  45. {
  46. if (!property.IsProperty) continue;
  47. var lowerName = TextUtility.ToLower(property.Name);
  48. if (lowerNames.Contains(lowerName) && property.Value != null)
  49. {
  50. var value = property.Value;
  51. if (value == null) continue;
  52. var text = value.ToString();
  53. if (!string.IsNullOrEmpty(text)) return text;
  54. }
  55. }
  56. }
  57. }
  58. #if NETFX
  59. // NETFX 最后搜索 GET 参数。
  60. if (request.Url != null)
  61. {
  62. var value = GetParameter(request.Url, names);
  63. if (!string.IsNullOrEmpty(value)) return value;
  64. }
  65. #endif
  66. return null;
  67. }
  68. }
  69. }