Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

127 linhas
3.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Policy;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Learun.Util;
  8. namespace Learun.Application.WeChat
  9. {
  10. public abstract class OperationRequestBase<T, THttp> : ISend<T>
  11. where T : OperationResultsBase, new()
  12. where THttp : IHttpSend, new()
  13. {
  14. protected abstract string Url();
  15. /// <summary>
  16. /// 视同attribute进行简单校验
  17. /// </summary>
  18. /// <param name="message"></param>
  19. /// <returns></returns>
  20. private bool Verify(out string message)
  21. {
  22. message = "";
  23. foreach (var pro in this.GetType().GetProperties())
  24. {
  25. var v = pro.GetCustomAttributes(typeof(IVerifyAttribute), true);
  26. foreach (IVerifyAttribute verify in pro.GetCustomAttributes(typeof(IVerifyAttribute), true))
  27. {
  28. if (!verify.Verify(pro.PropertyType, pro.GetValue(this), out message))
  29. {
  30. return false;
  31. }
  32. }
  33. }
  34. return true;
  35. }
  36. /// <summary>
  37. /// 格式化URL,替换Token
  38. /// </summary>
  39. /// <returns></returns>
  40. protected string GetUrl()
  41. {
  42. if (Token.IsTimeOut())
  43. {
  44. Token.GetNewToken();
  45. }
  46. string url = Url();
  47. if (url.Contains("=ACCESS_TOKEN"))
  48. {
  49. url = url.Replace("=ACCESS_TOKEN", "=" + Token.GetToken());
  50. }
  51. return url;
  52. }
  53. /// <summary>
  54. /// 发送
  55. /// </summary>
  56. /// <returns></returns>
  57. public T Send()
  58. {
  59. string message = "";
  60. if (!Verify(out message))
  61. {
  62. throw new Exception(message);
  63. }
  64. //string result = new HttpHelper().Post(url, JsonConvert.SerializeObject(this), Encoding.UTF8, Encoding.UTF8);
  65. IHttpSend httpSend = new THttp();
  66. string result = HttpSend(httpSend, GetUrl());
  67. return GetDeserializeObject(result);
  68. }
  69. /// <summary>
  70. /// 开放平台发送
  71. /// </summary>
  72. /// <returns></returns>
  73. public T OpenSend()
  74. {
  75. string message = "";
  76. if (!Verify(out message))
  77. {
  78. throw new Exception(message);
  79. }
  80. //string result = new HttpHelper().Post(url, JsonConvert.SerializeObject(this), Encoding.UTF8, Encoding.UTF8);
  81. IHttpSend httpSend = new THttp();
  82. string result = HttpSend(httpSend, Url());
  83. return GetDeserializeObject(result);
  84. }
  85. /// <summary>
  86. /// 处理返回结果
  87. /// </summary>
  88. /// <param name="result"></param>
  89. /// <returns></returns>
  90. protected virtual T GetDeserializeObject(string result)
  91. {
  92. return result.ToObject<T>();
  93. }
  94. /// <summary>
  95. /// 处理发送请求
  96. /// </summary>
  97. /// <param name="httpSend"></param>
  98. /// <param name="url"></param>
  99. /// <returns></returns>
  100. protected virtual string HttpSend(IHttpSend httpSend, string url)
  101. {
  102. return httpSend.Send(url,this.ToJson());
  103. }
  104. }
  105. }