forked from kidgrow-microservices-platform

zhaoxiaohao
2021-02-06 01d42cce4abdbd5d232d241b1b6b662314cf3999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
60
61
62
63
64
65
66
67
68
package com.kidgrow.common.base;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: <br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/3 17:01 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
public class ResponseBuilder {
    /**
     * 通用错误
     *
     * @param msg 响应消息
     */
    public static <T> ApiResponse<T> buildResultError(String msg) {
        return  new ApiResponse<>(ResponseCode.ERROR, msg);
    }
 
    /**
     * 错误响应
     *
     * @param code 错误代码,备选值参见 {@link ResponseCode}
     * @param msg  响应消息
     */
    public static <T> ApiResponse<T> buildResultError(Integer code, String msg) {
        return new ApiResponse<>(code, msg);
    }
 
    /**
     * 通用成功响应
     */
    public static  <T> ApiResponse<T> buildResultSuccess() {
        return new ApiResponse<>(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
    }
 
    /**
     * 通用成功响应,带响应消息
     *
     * @param msg 响应消息
     */
    public static  <T> ApiResponse<T> buildResultSuccess(String msg) {
        return new ApiResponse<>(ResponseCode.SUCCESS, msg);
    }
 
    /**
     * 成功响应
     *
     * @param code 响应代码,备选值参见 {@link ResponseCode}
     * @param msg  响应消息
     * @param data 响应数据
     */
    public static <T> ApiResponse<T> buildResult(Integer code, String msg, T data) {
        ApiResponse<T> response = new ApiResponse<>(code, msg);
        response.setData(data);
        return response;
    }
 
    /**
     * 成功响应
     *
     * @param data 响应数据
     */
    public static <T> ApiResponse<T> buildResultSuccess(T data) {
        return buildResult(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, data);
    }
}