AjaxResult.java 4.4 KB

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