package com.persagy.common.utils; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.persagy.common.enums.ResponseCode; /** * @version * @description * @company persagy * @author zhangqiankun * @since 2020年9月1日: 下午2:21:25 */ public class ResponseResultUtil { public static final String RESP_CODE_KEY = "respCode"; public static final String RESP_MSG_KEY = "respMsg"; public static final String RESP_CONTENT_KEY = "content"; public static final String RESP_COUNT_KEY = "count"; public static final String SUCCESS_CODE = ResponseCode.A00000.getCode(); public static final String FAILURE_CODE = ResponseCode.A10000.getCode(); /** * 判断是否响应成功, respCode=00000 */ public static boolean isSuccess(String resultJson) { if (StringUtil.isBlank(resultJson)) { return false; } try { return isSuccess(JSONObject.parseObject(resultJson)); } catch (Exception e) { } return false; } /** * 判断是否响应成功, respCode=00000 */ public static boolean isSuccess(JSONObject jsonObject) { if (jsonObject == null) { return false; } if (SUCCESS_CODE.equals(jsonObject.getString(RESP_CODE_KEY))) { return true; } return false; } /** * 判断是否响应成功, respCode=00000 */ public static boolean isSuccess(ResponseResult result) { if (result == null) { return false; } if (SUCCESS_CODE.equals(result.getRespCode())) { return true; } return false; } /** * 判断响应是否失败 */ public static boolean isError(String resultJson) { return !isSuccess(resultJson); } /** * 判断响应是否失败 */ public static boolean isError(JSONObject jsonObject) { return !isSuccess(jsonObject); } /** * 判断响应是否失败 */ public static boolean isError(ResponseResult result) { return !isSuccess(result); } /** * 获取响应体对象 * * @param resultJson * @return */ public static ResponseResult convertResponse(String resultJson) { if (StringUtil.isBlank(resultJson)) { return null; } try { return JSONObject.parseObject(resultJson, ResponseResult.class); } catch (Exception e) { } return null; } /** * 获取响应体中content内容所表示的class对象 * * @param resultJson * @return */ public static Class convertResponse(String resultJson, Class clazz) { JSONObject jsonObject = toJSONObject(resultJson); return (Class)jsonObject.toJavaObject(clazz); } /** * 获取响应体中content内容所表示的集合对象 * * @param resultJson * @return */ public static Class[] convertResponse(String resultJson, Class[] clazz) { JSONArray jsonArray = toJSONArray(resultJson); return (Class[]) jsonArray.toArray(clazz); } /** * 成功响应才会返回content * * @param resultJson * @return 此方法不会返回null */ public static JSONObject toJSONObject(String resultJson) { if (StringUtil.isBlank(resultJson)) { return new JSONObject(); } JSONObject jsonObject = JSONObject.parseObject(resultJson); if (isSuccess(jsonObject)) { return jsonObject.getJSONObject(RESP_CONTENT_KEY); } return new JSONObject(); } /** * 成功响应才会返回content * * @param resultJson * @return 此方法不会返回null */ public static JSONArray toJSONArray(String resultJson) { if (StringUtil.isBlank(resultJson)) { return new JSONArray(); } JSONObject jsonObject = JSONObject.parseObject(resultJson); if (isSuccess(jsonObject)) { return jsonObject.getJSONArray(RESP_CONTENT_KEY); } return new JSONArray(); } public static ResponseResult successResult() { ResponseResult result = new ResponseResult(); result.setRespCode(ResponseCode.A00000.getCode()); result.setRespMsg(ResponseCode.A00000.getDesc()); return result; } public static ResponseResult successResult(String respMsg) { ResponseResult result = new ResponseResult(); result.setRespCode(ResponseCode.A00000.getCode()); result.setRespMsg(respMsg); return result; } public static ResponseResult successResult(Object content) { ResponseResult result = new ResponseResult(); result.setRespCode(ResponseCode.A00000.getCode()); result.setRespMsg(ResponseCode.A00000.getDesc()); result.setContent(content); return result; } public static ResponseResult successResult(String respMsg, Object content) { ResponseResult result = new ResponseResult(); result.setRespCode(ResponseCode.A00000.getCode()); result.setRespMsg(respMsg); result.setContent(content); return result; } public static ResponseResult successResult(Object content, Long count) { ResponseResult result = new ResponseResult(); result.setRespCode(ResponseCode.A00000.getCode()); result.setRespMsg(ResponseCode.A00000.getDesc()); result.setContent(content); result.setCount(count); return result; } public static ResponseResult errorResult() { ResponseResult result = new ResponseResult(); result.setRespCode(ResponseCode.A10000.getCode()); result.setRespMsg(ResponseCode.A10000.getDesc()); return result; } public static ResponseResult errorResult(String respMsg) { ResponseResult result = new ResponseResult(); result.setRespCode(ResponseCode.A10000.getCode()); result.setRespMsg(respMsg); return result; } public static ResponseResult errorResult(String respCode, String respMsg) { ResponseResult result = new ResponseResult(); result.setRespCode(respCode); result.setRespMsg(respMsg); return result; } public static ResponseResult errorResult(String respCode, String respMsg, Object content) { ResponseResult result = new ResponseResult(); result.setRespCode(respCode); result.setRespMsg(respMsg); result.setContent(content); return result; } }