Blame view

src/main/java/cn/iocoder/dashboard/common/pojo/CommonResult.java 2.83 KB
0a607861   YunaiV   common 包,基础组件
1
2
3
  package cn.iocoder.dashboard.common.pojo;
  
  import cn.iocoder.dashboard.common.exception.ErrorCode;
0a607861   YunaiV   common 包,基础组件
4
5
  import cn.iocoder.dashboard.common.exception.ServiceException;
  import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants;
9c1c2659   YunaiV   解决各种 json 库的兼容性问题
6
  import com.fasterxml.jackson.annotation.JsonIgnore;
0a607861   YunaiV   common 包,基础组件
7
8
9
10
  import lombok.Data;
  import org.springframework.util.Assert;
  
  import java.io.Serializable;
d843d6a5   YunaiV   短信提交 2021-04-03,重...
11
  import java.util.Objects;
0a607861   YunaiV   common 包,基础组件
12
13
14
15
16
17
18
  
  /**
   * 通用返回
   *
   * @param <T> 数据泛型
   */
  @Data
9528698a   YunaiV   短信提交 2021-03-31,重...
19
  public class CommonResult<T> implements Serializable {
0a607861   YunaiV   common 包,基础组件
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
      /**
       * 错误码
       *
       * @see ErrorCode#getCode()
       */
      private Integer code;
      /**
       * 返回数据
       */
      private T data;
      /**
       * 错误提示,用户可阅读
       *
5a1491d7   YunaiV   短信提交 2021-04-01,重...
34
       * @see ErrorCode#getMsg() ()
0a607861   YunaiV   common 包,基础组件
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
       */
      private String msg;
  
      /**
       * 将传入的 result 对象,转换成另外一个泛型结果的对象
       *
       * 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
       *
       * @param result 传入的 result 对象
       * @param <T> 返回的泛型
       * @return 新的 CommonResult 对象
       */
      public static <T> CommonResult<T> error(CommonResult<?> result) {
          return error(result.getCode(), result.getMsg());
      }
  
      public static <T> CommonResult<T> error(Integer code, String message) {
          Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
          CommonResult<T> result = new CommonResult<>();
          result.code = code;
          result.msg = message;
          return result;
      }
  
      public static <T> CommonResult<T> error(ErrorCode errorCode) {
5a1491d7   YunaiV   短信提交 2021-04-01,重...
60
          return error(errorCode.getCode(), errorCode.getMsg());
0a607861   YunaiV   common 包,基础组件
61
62
63
64
65
66
67
68
69
70
      }
  
      public static <T> CommonResult<T> success(T data) {
          CommonResult<T> result = new CommonResult<>();
          result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
          result.data = data;
          result.msg = "";
          return result;
      }
  
d843d6a5   YunaiV   短信提交 2021-04-03,重...
71
72
73
74
      public static boolean isSuccess(Integer code) {
          return Objects.equals(code, GlobalErrorCodeConstants.SUCCESS.getCode());
      }
  
9c1c2659   YunaiV   解决各种 json 库的兼容性问题
75
      @JsonIgnore // 避免 jackson 序列化
0a607861   YunaiV   common 包,基础组件
76
      public boolean isSuccess() {
73a2f4d8   YunaiV   1. 编写短信模板的前端
77
          return isSuccess(code);
0a607861   YunaiV   common 包,基础组件
78
79
      }
  
9c1c2659   YunaiV   解决各种 json 库的兼容性问题
80
      @JsonIgnore // 避免 jackson 序列化
0a607861   YunaiV   common 包,基础组件
81
82
83
84
85
86
87
      public boolean isError() {
          return !isSuccess();
      }
  
      // ========= 和 Exception 异常体系集成 =========
  
      /**
9528698a   YunaiV   短信提交 2021-03-31,重...
88
       * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常
0a607861   YunaiV   common 包,基础组件
89
       */
9528698a   YunaiV   短信提交 2021-03-31,重...
90
      public void checkError() throws ServiceException {
0a607861   YunaiV   common 包,基础组件
91
92
93
          if (isSuccess()) {
              return;
          }
0a607861   YunaiV   common 包,基础组件
94
95
96
97
98
99
100
101
          // 业务异常
          throw new ServiceException(code, msg);
      }
  
      public static <T> CommonResult<T> error(ServiceException serviceException) {
          return error(serviceException.getCode(), serviceException.getMessage());
      }
  
0a607861   YunaiV   common 包,基础组件
102
  }