ApiResult.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Infrastructure.Constant;
  2. using System.Collections.Generic;
  3. namespace Infrastructure.Model
  4. {
  5. public class ApiResult : Dictionary<string, object>
  6. {
  7. /// <summary>
  8. /// 状态码
  9. /// </summary>
  10. public static readonly string CODE_TAG = "status";
  11. /// <summary>
  12. /// 返回内容
  13. /// </summary>
  14. public static readonly string MSG_TAG = "msg";
  15. /// <summary>
  16. /// 数据对象
  17. /// </summary>
  18. public static readonly string DATA_TAG = "data";
  19. /// <summary>
  20. /// 初始化一个新创建的APIResult对象,使其表示一个空消息
  21. /// </summary>
  22. public ApiResult()
  23. {
  24. }
  25. /// <summary>
  26. /// 初始化一个新创建的 ApiResult 对象
  27. /// </summary>
  28. /// <param name="code"></param>
  29. /// <param name="msg"></param>
  30. public ApiResult(int code, string msg)
  31. {
  32. Add(CODE_TAG, code);
  33. Add(MSG_TAG, msg);
  34. }
  35. /// <summary>
  36. /// 初始化一个新创建的 ApiResult 对象
  37. /// </summary>
  38. /// <param name="code"></param>
  39. /// <param name="msg"></param>
  40. /// <param name="data"></param>
  41. public ApiResult(int code, string msg, object data)
  42. {
  43. Add(CODE_TAG, code);
  44. Add(MSG_TAG, msg);
  45. if (data != null)
  46. {
  47. Add(DATA_TAG, data);
  48. }
  49. }
  50. /// <summary>
  51. /// 返回成功消息
  52. /// </summary>
  53. /// < returns > 成功消息 </ returns >
  54. public static ApiResult Success() { return new ApiResult(HttpStatus.SUCCESS, "success"); }
  55. /// <summary>
  56. /// 返回成功消息
  57. /// </summary>
  58. /// <param name="data"></param>
  59. /// <returns> 成功消息 </returns >
  60. public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); }
  61. /// <summary>
  62. /// 返回成功消息
  63. /// </summary>
  64. /// <param name="msg">返回内容</param>
  65. /// <returns>成功消息</returns>
  66. public static ApiResult Success(string msg) { return new ApiResult(HttpStatus.SUCCESS, msg, null); }
  67. /// <summary>
  68. /// 返回成功消息
  69. /// </summary>
  70. /// <param name="msg">返回内容</param>
  71. /// <param name="data">数据对象</param>
  72. /// <returns>成功消息</returns>
  73. public static ApiResult Success(string msg, object data) { return new ApiResult(HttpStatus.SUCCESS, msg, data); }
  74. public static ApiResult Error(ResultCode code, string msg = "")
  75. {
  76. return Error((int)code, msg);
  77. }
  78. /// <summary>
  79. /// 返回失败消息
  80. /// </summary>
  81. /// <param name="code"></param>
  82. /// <param name="msg"></param>
  83. /// <returns></returns>
  84. public static ApiResult Error(int code, string msg) { return new ApiResult(code, msg); }
  85. /// <summary>
  86. /// 返回失败消息
  87. /// </summary>
  88. /// <param name="msg"></param>
  89. /// <returns></returns>
  90. public static ApiResult Error(string msg) { return new ApiResult((int)ResultCode.CUSTOM_ERROR, msg); }
  91. /// <summary>
  92. /// 是否为成功消息
  93. /// </summary>
  94. /// <returns></returns>
  95. public bool IsSuccess()
  96. {
  97. return HttpStatus.SUCCESS == (int)this[CODE_TAG];
  98. }
  99. /// <summary>
  100. /// 方便链式调用
  101. /// </summary>
  102. /// <param name="key"></param>
  103. /// <param name="value"></param>
  104. /// <returns></returns>
  105. public ApiResult Put(string key, object value)
  106. {
  107. Add(key, value);
  108. return this;
  109. }
  110. }
  111. }