/src/main/java/com/sichuang/schedule/common/MyResponse.java

https://bitbucket.org/sichuangzixun/serviceschedule · Java · 185 lines · 116 code · 32 blank · 37 comment · 8 complexity · adba401be84249a3ecf74b280ac56410 MD5 · raw file

  1. package com.sichuang.schedule.common;
  2. import com.alibaba.fastjson.serializer.SerializerFeature;
  3. import com.sichuang.schedule.model.entity.Page;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class MyResponse {
  7. static SerializerFeature[] features = {SerializerFeature.WriteMapNullValue};
  8. private static int errorCode = 40001;
  9. private static int successCode = 40000;
  10. private static int needLoginCode = -1;
  11. private static int defaultSuccessCode = 40002;
  12. /**
  13. * @param msg
  14. * @param code -1未登陆,40000请求成功,40001请求不成功
  15. * @param data
  16. * @return接口返回数据格式
  17. */
  18. public static Map<String, Object> getResponse(int code, String msg, Object data) {
  19. Map<String, Object> map = new HashMap<>();
  20. map.put("code", code);
  21. map.put("msg", msg);
  22. map.put("data", data);
  23. return map;
  24. }
  25. public static Map<String, Object> getResponse(boolean code, String msg, Object data) {
  26. if (code) {
  27. return getResponse(successCode, msg, data);
  28. } else {
  29. return getResponse(errorCode, msg, data);
  30. }
  31. }
  32. public static Map<String, Object> getResponse(boolean code, Object data) {
  33. if (code) {
  34. return getResponse(successCode, "", data);
  35. } else {
  36. return getResponse(errorCode, "", data);
  37. }
  38. }
  39. public static Map<String, Object> getResponse(boolean code, String msg) {
  40. if (code) {
  41. return getResponse(successCode, msg, null);
  42. } else {
  43. return getResponse(errorCode, msg, null);
  44. }
  45. }
  46. public static Map<String, Object> getResponse(boolean code) {
  47. if (code) {
  48. return getResponse(successCode, "", null);
  49. } else {
  50. return getResponse(errorCode, "", null);
  51. }
  52. }
  53. public static Map<String, Object> getErrorResponse(String msg) {
  54. return getResponse(errorCode, msg, null);
  55. }
  56. public static Map<String, Object> getNotLoginResponse(String msg) {
  57. return getResponse(needLoginCode, "验证失败,请重新登录!", null);
  58. }
  59. public static Map<String, Object> getSQLErrorResponse() {
  60. return getErrorResponse("未知原因,操作失败!");
  61. }
  62. public static Map<String, Object> getSQLSuccessResponse() {
  63. return getSuccessResponse("操作成功!");
  64. }
  65. public static Map<String, Object> getNotLoginResponse() {
  66. return getNotLoginResponse("验证失败,请重新登录!");
  67. }
  68. public static Map<String, Object> getErrorResponse() {
  69. return getResponse(false, "请求错误!");
  70. }
  71. public static Map<String, Object> getSuccessResponse() {
  72. return getResponse(successCode, "请求成功!", null);
  73. }
  74. public static Map<String, Object> getDefaultSuccessResponse() {
  75. return getResponse(defaultSuccessCode, "请求成功!", null);
  76. }
  77. public static Map<String, Object> getSuccessResponse(String msg, Object data) {
  78. return getResponse(successCode, msg, data);
  79. }
  80. public static Map<String, Object> getSuccessResponse(Object data) {
  81. return getResponse(successCode, "操作成功!", data);
  82. }
  83. public static Map<String, Object> getSuccessResponse(String msg) {
  84. return getResponse(successCode, msg, null);
  85. }
  86. public static Map<String, Object> getResponse(int count) {
  87. return MyResponse.getSuccessResponse("成功修改" + count + "条记录!");
  88. }
  89. public static boolean isSuccessByRootMap(Map<String, Object> map) {
  90. return map.get("code").equals(successCode);
  91. }
  92. public static boolean isErrorByRootMap(Map<String, Object> map) {
  93. return map.get("code").equals(errorCode);
  94. }
  95. public static Object getDataByRootMap(Map<String, Object> map) {
  96. return map.get("data");
  97. }
  98. public static String getMsgByRootMap(Map<String, Object> map) {
  99. return map.get("msg").toString();
  100. }
  101. /**
  102. * 设置不用加密的方法
  103. *
  104. * @param map
  105. * @return
  106. */
  107. public static Map<String, Object> getNotEncryptString(Map<String, Object> map) {
  108. map.put("NotEncrypt", true);
  109. return map;
  110. }
  111. /**
  112. * 返回请求错误码
  113. *
  114. * @return
  115. */
  116. public static int getErrorCode() {
  117. return errorCode;
  118. }
  119. /**
  120. * 返回请求成功码
  121. *
  122. * @return
  123. */
  124. public static int getSuccessCode() {
  125. return successCode;
  126. }
  127. /**
  128. * 返回未登陆码
  129. *
  130. * @return
  131. */
  132. public static int getNeedLoginCode() {
  133. return needLoginCode;
  134. }
  135. /**
  136. * 分页搜索返回结果
  137. *
  138. * @param code
  139. * @param msg
  140. * @param page
  141. * @param data
  142. * @return
  143. */
  144. public static Map<String, Object> getPageResponse(boolean code, String msg, Page page, Object data) {
  145. Map<String, Object> map = getResponse(code, msg, data);
  146. map.put("page", page);
  147. return map;
  148. }
  149. public static Map<String, Object> getResponse(boolean code, Page page, Object data) {
  150. // TODO Auto-generated method stub
  151. return getPageResponse(code, "", page, data);
  152. }
  153. }