AjaxResult.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.simuwang.base.common.result;
  2. import com.simuwang.base.common.util.StringUtil;
  3. import com.simuwang.base.pojo.vo.SuccessVO;
  4. import com.smppw.common.pojo.enums.status.ResultCode;
  5. import java.util.HashMap;
  6. import java.util.Objects;
  7. /**
  8. * 操作消息提醒
  9. *
  10. * @author ruoyi
  11. */
  12. public class AjaxResult extends HashMap<String, Object>
  13. {
  14. private static final long serialVersionUID = 1L;
  15. /** 状态码 */
  16. public static final String CODE_TAG = "code";
  17. /** 返回内容 */
  18. public static final String MSG_TAG = "msg";
  19. /** 数据对象 */
  20. public static final String DATA_TAG = "data";
  21. /**
  22. * 状态类型
  23. */
  24. public enum Type
  25. {
  26. /** 成功 */
  27. SUCCESS(0),
  28. /** 警告 */
  29. WARN(301),
  30. /** 错误 */
  31. ERROR(500);
  32. private final int value;
  33. Type(int value)
  34. {
  35. this.value = value;
  36. }
  37. public int value()
  38. {
  39. return this.value;
  40. }
  41. }
  42. /**
  43. * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
  44. */
  45. public AjaxResult()
  46. {
  47. }
  48. /**
  49. * 初始化一个新创建的 AjaxResult 对象
  50. *
  51. * @param type 状态类型
  52. * @param msg 返回内容
  53. */
  54. public AjaxResult(Type type, String msg)
  55. {
  56. super.put(CODE_TAG, type.value);
  57. super.put(MSG_TAG, msg);
  58. }
  59. /**
  60. * 初始化一个新创建的 AjaxResult 对象
  61. *
  62. * @param type 状态类型
  63. * @param msg 返回内容
  64. * @param data 数据对象
  65. */
  66. public AjaxResult(Type type, String msg, Object data)
  67. {
  68. super.put(CODE_TAG, type.value);
  69. super.put(MSG_TAG, msg);
  70. if (StringUtil.isNotNull(data))
  71. {
  72. super.put(DATA_TAG, data);
  73. }
  74. }
  75. /**
  76. * 返回成功消息
  77. *
  78. * @return 成功消息
  79. */
  80. public static AjaxResult success()
  81. {
  82. SuccessVO successVO = new SuccessVO();
  83. successVO.setStatus(1);
  84. successVO.setMsg(ResultCode.SUCCESS.getMsg());
  85. return AjaxResult.success(successVO);
  86. }
  87. /**
  88. * 返回成功数据
  89. *
  90. * @return 成功消息
  91. */
  92. public static AjaxResult success(Object data)
  93. {
  94. return AjaxResult.success("", data);
  95. }
  96. /**
  97. * 返回成功消息
  98. *
  99. * @param msg 返回内容
  100. * @return 成功消息
  101. */
  102. public static AjaxResult success(String msg)
  103. {
  104. return AjaxResult.success(msg, null);
  105. }
  106. /**
  107. * 返回成功消息
  108. *
  109. * @param msg 返回内容
  110. * @param data 数据对象
  111. * @return 成功消息
  112. */
  113. public static AjaxResult success(String msg, Object data)
  114. {
  115. return new AjaxResult(Type.SUCCESS, msg, data);
  116. }
  117. /**
  118. * 返回警告消息
  119. *
  120. * @param msg 返回内容
  121. * @return 警告消息
  122. */
  123. public static AjaxResult warn(String msg)
  124. {
  125. return AjaxResult.warn(msg, null);
  126. }
  127. /**
  128. * 返回警告消息
  129. *
  130. * @param msg 返回内容
  131. * @param data 数据对象
  132. * @return 警告消息
  133. */
  134. public static AjaxResult warn(String msg, Object data)
  135. {
  136. return new AjaxResult(Type.WARN, msg, data);
  137. }
  138. /**
  139. * 返回错误消息
  140. *
  141. * @return
  142. */
  143. public static AjaxResult error()
  144. {
  145. return AjaxResult.error("操作失败");
  146. }
  147. /**
  148. * 返回错误消息
  149. *
  150. * @param msg 返回内容
  151. * @return 警告消息
  152. */
  153. public static AjaxResult error(String msg)
  154. {
  155. return AjaxResult.error(msg, null);
  156. }
  157. /**
  158. * 返回错误消息
  159. *
  160. * @param msg 返回内容
  161. * @param data 数据对象
  162. * @return 警告消息
  163. */
  164. public static AjaxResult error(String msg, Object data)
  165. {
  166. return new AjaxResult(Type.ERROR, msg, data);
  167. }
  168. /**
  169. * 是否为成功消息
  170. *
  171. * @return 结果
  172. */
  173. public boolean isSuccess()
  174. {
  175. return Objects.equals(Type.SUCCESS.value, this.get(CODE_TAG));
  176. }
  177. /**
  178. * 是否为警告消息
  179. *
  180. * @return 结果
  181. */
  182. public boolean isWarn()
  183. {
  184. return Objects.equals(Type.WARN.value, this.get(CODE_TAG));
  185. }
  186. /**
  187. * 是否为错误消息
  188. *
  189. * @return 结果
  190. */
  191. public boolean isError()
  192. {
  193. return Objects.equals(Type.ERROR.value, this.get(CODE_TAG));
  194. }
  195. /**
  196. * 方便链式调用
  197. *
  198. * @param key 键
  199. * @param value 值
  200. * @return 数据对象
  201. */
  202. @Override
  203. public AjaxResult put(String key, Object value)
  204. {
  205. super.put(key, value);
  206. return this;
  207. }
  208. }