瀏覽代碼

统一后端接口返回结果

yucheng 4 年之前
父節點
當前提交
d960d2f92d

+ 53 - 0
fm-common/src/main/java/com/persagy/fm/common/response/CommonResult.java

@@ -0,0 +1,53 @@
+package com.persagy.fm.common.response;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.persagy.common.enums.ResponseCode;
+import lombok.Data;
+
+/**
+ * 服务调用消息结果
+ * @author Charlie Yu
+ * @date 2021-03-25
+ */
+@Data
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class CommonResult<T> {
+
+    /** 不需要提示 */
+    public static final CommonResult SUCCESS = new CommonResult();
+
+    /** 响应码 */
+    private String respCode;
+    /** 响应码 */
+    private String respMsg;
+    /** 响应数据 */
+    private T content;
+
+    /**
+     * 构造方法
+     */
+    public CommonResult(){
+        this(ResponseCode.A00000.getCode(), ResponseCode.A00000.getDesc());
+    }
+
+    /**
+     * 构造方法
+     * @param respCode 响应码
+     * @param respMsg 提示信息
+     */
+    public CommonResult(String respCode, String respMsg) {
+        this(respCode, respMsg,null);
+    }
+
+    /**
+     * 构造方法
+     * @param respCode 响应码
+     * @param respMsg 提示信息
+     * @param content 数据
+     */
+    public CommonResult(String respCode, String respMsg, T content){
+        this.respCode = respCode;
+        this.respMsg = respMsg;
+        this.content = content;
+    }
+}

+ 22 - 0
fm-common/src/main/java/com/persagy/fm/common/response/PageList.java

@@ -0,0 +1,22 @@
+package com.persagy.fm.common.response;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * 分页列表
+ * @author Charlie Yu
+ * @data 2021-03-25
+ */
+@Data
+public class PageList<T> {
+
+    private List<T> records;
+    private long total;
+
+    public PageList(final List<T> records, final long total) {
+        this.records = records;
+        this.total = total;
+    }
+}

+ 87 - 0
fm-common/src/main/java/com/persagy/fm/common/utils/ResultHelper.java

@@ -0,0 +1,87 @@
+package com.persagy.fm.common.utils;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.persagy.common.enums.ResponseCode;
+import com.persagy.fm.common.response.CommonResult;
+import com.persagy.fm.common.response.PageList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.List;
+
+/**
+ * 响应结果包装助手
+ * @author Charlie Yu
+ * @date 2021-03-25
+ */
+public class ResultHelper {
+
+    /**
+     * 成功消息
+     * @return
+     */
+    public static CommonResult success(){
+        return new CommonResult(ResponseCode.A00000.getCode(), ResponseCode.A00000.getDesc());
+    }
+
+    /**
+     * 成功消息
+     * @return
+     */
+    public static CommonResult success(String respMsg){
+        return new CommonResult(ResponseCode.A00000.getCode(), respMsg);
+    }
+
+    /**
+     * 失败消息
+     * @param respCode
+     * @param respMsg
+     * @return
+     */
+    public static CommonResult failure(String respCode, String respMsg) {
+        return new CommonResult(respCode, respMsg);
+    }
+
+    /**
+     * 单个数据
+     * @param content
+     * @param <T>
+     * @return
+     */
+    public static <T> CommonResult<T> single(T content) {
+        return new CommonResult(ResponseCode.A00000.getCode(), ResponseCode.A00000.getDesc(), content);
+    }
+
+    /**
+     * 多个数据 - 列表
+     * @param records
+     * @param <T>
+     * @return
+     */
+    public static <T> CommonResult<PageList<T>> multi(List<T> records) {
+        return multi(records, CollectionUtils.size(records));
+    }
+
+    /**
+     * 多个数据 - 分页
+     * @param page
+     * @param <T>
+     * @return
+     */
+    public static <T> CommonResult<PageList<T>> multi(IPage<T> page) {
+        List<T> records = page == null ? null : page.getRecords();
+        long total = page == null ? 0 : page.getTotal();
+        return multi(records, total);
+    }
+
+    /**
+     * 多个数据
+     * @param records
+     * @param total
+     * @param <T>
+     * @return
+     */
+    private static <T> CommonResult<PageList<T>> multi(List<T> records, long total) {
+        PageList<T> pageList = new PageList<>(records, total);
+        return new CommonResult(ResponseCode.A00000.getCode(), ResponseCode.A00000.getDesc(), pageList);
+    }
+}