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.

131 lines
4.3 KiB

  1. using Apewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer.Models
  6. {
  7. /// <summary>WebAPI 请求。T 必须带有 System.Serializable 特性。</summary>
  8. [Serializable]
  9. internal sealed class ApiRequest<T>
  10. {
  11. private string _application = null;
  12. private string _function = null;
  13. private string _random = Guid.NewGuid().ToString("n");
  14. private string _ticket = null;
  15. private T _data = default(T);
  16. /// <summary>创建 WebAPI 请求。</summary>
  17. public ApiRequest(string application = null, string function = null, T data = default(T))
  18. {
  19. _application = application;
  20. _function = function;
  21. _data = data;
  22. }
  23. /// <summary>请求的 Application。</summary>
  24. public string Application { get { return _application; } set { _application = value; } }
  25. /// <summary>请求的 Function。</summary>
  26. public string Function { get { return _function; } set { _function = value; } }
  27. /// <summary>请求的 Random,服务端将原样返回。</summary>
  28. public string Ramdom { get { return _random; } set { _random = value; } }
  29. /// <summary>用户 Ticket。</summary>
  30. public string Ticket { get { return _ticket; } set { _ticket = value; } }
  31. /// <summary></summary>
  32. public T Data { get { return _data; } set { _data = value; } }
  33. /// <summary>生成 Json 对象。</summary>
  34. public Json ToJson()
  35. {
  36. var json = Json.NewObject();
  37. json.SetProperty("application", Application);
  38. json.SetProperty("function", Function);
  39. json.SetProperty("random", Ramdom);
  40. json.SetProperty("ticket", Ticket);
  41. json.SetProperty("data", Json.Parse(Data, true));
  42. return json;
  43. }
  44. /// <summary>生成 Json 字符串,可指定 Json 缩进。</summary>
  45. public string ToString(bool indented)
  46. {
  47. return ToJson().ToString(indented);
  48. }
  49. /// <summary>生成 Json 字符串,默认不缩进。</summary>
  50. public override string ToString()
  51. {
  52. return ToJson().ToString(false);
  53. }
  54. }
  55. /// <summary>WebAPI 请求。</summary>
  56. [Serializable]
  57. public sealed class ApiRequest
  58. {
  59. private string _application = null;
  60. private string _function = null;
  61. private string _random = Guid.NewGuid().ToString("n");
  62. private string _ticket = null;
  63. private Json _data = Json.NewObject();
  64. /// <summary>创建 WebAPI 请求。</summary>
  65. public ApiRequest(string application = null, string function = null, object data = null)
  66. {
  67. _application = application;
  68. _function = function;
  69. _data = data == null ? Json.NewObject() : Json.Parse(data);
  70. }
  71. /// <summary>请求的 Application。</summary>
  72. public string Application { get { return _application; } set { _application = value; } }
  73. /// <summary>请求的 Function。</summary>
  74. public string Function { get { return _function; } set { _function = value; } }
  75. /// <summary>请求的 Random,服务端将原样返回。</summary>
  76. public string Ramdom { get { return _random; } set { _random = value; } }
  77. /// <summary>用户 Ticket。</summary>
  78. public string Ticket { get { return _ticket; } set { _ticket = value; } }
  79. /// <summary></summary>
  80. public Json Data { get { return _data; } set { _data = value; } }
  81. /// <summary>生成 Json 对象。</summary>
  82. public Json ToJson()
  83. {
  84. var json = Json.NewObject();
  85. json.SetProperty("application", Application);
  86. json.SetProperty("function", Function);
  87. json.SetProperty("random", Ramdom);
  88. json.SetProperty("ticket", Ticket);
  89. json.SetProperty("data", Data);
  90. return json;
  91. }
  92. /// <summary>生成 Json 字符串,可指定 Json 缩进。</summary>
  93. public string ToString(bool indented)
  94. {
  95. return ToJson().ToString(indented);
  96. }
  97. /// <summary>生成 Json 字符串,默认不缩进。</summary>
  98. public override string ToString()
  99. {
  100. return ToJson().ToString(false);
  101. }
  102. }
  103. }