ApiJsonModel.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Text;
  5. namespace XYY.Core.Standard.Mvc
  6. {
  7. /// <summary>
  8. /// 泛型对象
  9. /// </summary>
  10. /// <typeparam name="T"></typeparam>
  11. public class ApiJsonModel<T>
  12. {
  13. /// <summary>
  14. /// 错误码(0表示无错误)
  15. /// </summary>
  16. public HttpStatusCode code { get; set; }
  17. /// <summary>
  18. /// 是否成功
  19. /// </summary>
  20. public bool success { get; set; }
  21. /// <summary>
  22. /// 错误信息
  23. /// </summary>
  24. public string error { get; set; }
  25. /// <summary>
  26. /// 消息
  27. /// </summary>
  28. public string message { get; set; }
  29. /// <summary>
  30. /// Json数据
  31. /// </summary>
  32. public T data { get; set; }
  33. public string Guid
  34. {
  35. get; set;
  36. }
  37. public static ApiJsonModel<T> OK(T data)
  38. {
  39. return new ApiJsonModel<T>
  40. {
  41. code = HttpStatusCode.OK,
  42. data = data,
  43. success = true,
  44. message = "成功"
  45. };
  46. }
  47. public static ApiJsonModel<T> Error(string message, T data)
  48. {
  49. return new ApiJsonModel<T>
  50. {
  51. code = HttpStatusCode.InternalServerError,
  52. data = data,
  53. success = false,
  54. message = message
  55. };
  56. }
  57. }
  58. [Serializable]
  59. public class ApiJsonModel : ApiJsonModel<object>
  60. {
  61. /// <summary>
  62. /// 错误码(0表示无错误)
  63. /// </summary>
  64. public HttpStatusCode code { get; set; }
  65. /// <summary>
  66. /// 是否成功
  67. /// </summary>
  68. public bool success { get; set; }
  69. /// <summary>
  70. /// 错误信息
  71. /// </summary>
  72. public string error { get; set; }
  73. /// <summary>
  74. /// 消息
  75. /// </summary>
  76. public string message { get; set; }
  77. /// <summary>
  78. /// Json数据
  79. /// </summary>
  80. public object data { get; set; }
  81. public static ApiJsonModel OK(object data = null)
  82. {
  83. return new ApiJsonModel
  84. {
  85. code = HttpStatusCode.OK,
  86. data = data,
  87. success = true,
  88. message = "成功"
  89. };
  90. }
  91. public static ApiJsonModel Error(string message, object data = null)
  92. {
  93. return new ApiJsonModel
  94. {
  95. code = HttpStatusCode.InternalServerError,
  96. data = data,
  97. success = false,
  98. message = message
  99. };
  100. }
  101. }
  102. }