|
@@ -0,0 +1,129 @@
|
|
|
+package cn.sikey.tools.enums;
|
|
|
+
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
|
+import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
+import lombok.Data;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通用返回
|
|
|
+ *
|
|
|
+ * @param <T> 数据泛型
|
|
|
+ */
|
|
|
+@Data
|
|
|
+@JsonInclude(JsonInclude.Include.ALWAYS) // 总是包含所有字段
|
|
|
+public class CommonResult<T> implements Serializable {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 错误码
|
|
|
+ */
|
|
|
+
|
|
|
+ private Integer code;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回数据
|
|
|
+ */
|
|
|
+
|
|
|
+ private T data;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 错误提示,用户可阅读
|
|
|
+ */
|
|
|
+
|
|
|
+ private String message;
|
|
|
+
|
|
|
+ public CommonResult() {
|
|
|
+ this.code = GlobalErrorCodeConstants.SUCCESS.getCode();
|
|
|
+ this.message = "成功";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将传入的 result 对象,转换成另外一个泛型结果的对象
|
|
|
+ * <p>
|
|
|
+ * 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
|
|
|
+ *
|
|
|
+ * @param result 传入的 result 对象
|
|
|
+ * @param <T> 返回的泛型
|
|
|
+ * @return 新的 CommonResult 对象
|
|
|
+ */
|
|
|
+
|
|
|
+ public static <T> CommonResult<T> error(CommonResult<?> result) {
|
|
|
+ return error(result.getCode(), result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResult<T> error(Integer code, String message) {
|
|
|
+ CommonResult<T> result = new CommonResult<>();
|
|
|
+ result.code = code;
|
|
|
+ result.data = (T) new Object(); // 错误响应也设置空对象
|
|
|
+ result.message = message;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResult<T> error(ErrorCode errorCode) {
|
|
|
+ return error(errorCode.getCode(), errorCode.getMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResult<T> success(T data) {
|
|
|
+ CommonResult<T> result = new CommonResult<>();
|
|
|
+ result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
|
|
|
+ result.data = data;
|
|
|
+ result.message = "成功";
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResult<T> success() {
|
|
|
+ CommonResult<T> result = new CommonResult<>();
|
|
|
+ result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
|
|
|
+ result.data = (T) new Object(); // 设置为空对象而不是null
|
|
|
+ result.message = "成功";
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isSuccess(Integer code) {
|
|
|
+ return Objects.equals(code, GlobalErrorCodeConstants.SUCCESS.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResult<T> error(BusinessException serviceException) {
|
|
|
+ return error(serviceException.getCode(), serviceException.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @JsonIgnore // 避免 jackson 序列化
|
|
|
+ public boolean isSuccess() {
|
|
|
+ return isSuccess(code);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ========= 和 Exception 异常体系集成 =========
|
|
|
+
|
|
|
+ @JsonIgnore // 避免 jackson 序列化
|
|
|
+ public boolean isError() {
|
|
|
+ return !isSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否有异常。如果有,则抛出异常
|
|
|
+ */
|
|
|
+
|
|
|
+ public void checkError() throws BusinessException {
|
|
|
+ if (isSuccess()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 业务异常
|
|
|
+ throw new BusinessException(code, message);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否有异常。如果有,则抛出异常
|
|
|
+ * 如果没有,则返回数据
|
|
|
+ */
|
|
|
+
|
|
|
+ @JsonIgnore // 避免 jackson 序列化
|
|
|
+ public T getCheckedData() {
|
|
|
+ checkError();
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|