java05:封装前端页面数据的结果类

①新建返回结果中的代码类
ResultCode.java


/**
 * 枚举了一些常用API操作码
 */
public enum ResultCode implements IErrorCode {
    SUCCESS(200, "操作成功"),
    FAILED(500, "操作失败"),
    VALIDATE_FAILED(404, "参数检验失败"),
    UNAUTHORIZED(401, "暂未登录或token已经过期"),
    FORBIDDEN(403, "没有相关权限");
    private long code;
    private String msg;

     ResultCode(long code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public long getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

②新建返回结果中的错误类
IErrorCode.java

/**
 * 封装API的错误码
 */
public interface IErrorCode {
    long getCode();
    String getMsg();
}

③新建返回结果的封装类
CommonResult.java


/**
 * 通用返回对象
 */
public class CommonResult {
    private long code;
    private String msg;
    private T data;

    protected CommonResult() {
    }

    protected CommonResult(long code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static  CommonResult success(T data) {
        return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  msg 提示信息
     */
    public static  CommonResult success(T data, String msg) {
        return new CommonResult(ResultCode.SUCCESS.getCode(), msg, data);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     */
    public static  CommonResult failed(IErrorCode errorCode) {
        return new CommonResult(errorCode.getCode(), errorCode.getMsg(), null);
    }

    /**
     * 失败返回结果
     * @param msg 提示信息
     */
    public static  CommonResult failed(String msg) {
        return new CommonResult(ResultCode.FAILED.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     */
    public static  CommonResult failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static  CommonResult validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static  CommonResult validateFailed(String message) {
        return new CommonResult(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static  CommonResult unauthorized(T data) {
        return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMsg(), data);
    }

    /**
     * 未授权返回结果
     */
    public static  CommonResult forbidden(T data) {
        return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMsg(), data);
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

④封装类似map的键值类
Pair.java

package com.caih.cloud.iscs.common;

public class Pair {
    private F first;
    private S second;

    public Pair()
    {
    }

    public Pair(F first, S second)
    {
        this.first = first;
        this.second = second;
    }

    public F getFirst()
    {
        return first;
    }

    public S getSecond()
    {
        return second;
    }

    public void setFirst(F first)
    {
        this.first = first;
    }

    public void setSecond(S second)
    {
        this.second = second;
    }
}

⑤CommonResult.java应用举例
a.代码

 /**
     * 订单列表
     *
     * @return 订单列表
     */
    @ApiOperation(value = "订单列表", httpMethod = "GET")
    @GetMapping("/list")
    public CommonResult> list(OrderDto qo) {
        Integer userId = this.getUserId();
        qo.setUserId(userId);
        Page data = orderService.orderVos(qo);
        return CommonResult.success(data);
    }

b.结果

{
    "code": 200, 
    "msg": "操作成功", 
    "data": {
        "records": [
            {
                "id": 53, 
                "orderAmt": "12.05", 
                "orderNo": "201909021116000001", 
                "storeNo": "201909021114000001", 
                "dealNo": "190902111937958730", 
                "orderTime": "2019-09-02 11:18:19", 
                "payTime": "2019-09-02 11:19:58", 
                "deliveryTime": null, 
                "finishTime": null, 
                "goods": [
                    {
                        "goodsNo": "201909021117000001", 
                        "goodsName": "廖记棒棒鸡-冷串毛肚", 
                        "goodsNum": 1, 
                        "goodsPrice": "0.05"
                    }
                ], 
                "buyerName": "白敬亭", 
                "buyerPhone": "17707781234", 
                "buyerRemarks": "备注备注备注",
                "postage": "(含邮费:12.00)", 
                "status": 0, 
                "statusCN": "待付款", 
                "expressComNo": "", 
                "expressComName": "", 
                "expressNo": ""
            }, 
            {
                "id": 51, 
                "orderAmt": "0.05", 
                "orderNo": "201908231720000002", 
                "storeNo": "201908231556000001", 
                "dealNo": "190823172158343291", 
                "orderTime": "2019-08-23 17:20:58", 
                "payTime": "2019-08-23 17:22:18", 
                "deliveryTime": null, 
                "finishTime": null, 
                "goods": [
                    {
                        "goodsNo": "201908231719000001", 
                        "goodsName": "永和豆浆-黑豆浆", 
                        "goodsNum": 1, 
                        "goodsPrice": "0.05"
                    }
                ], 
                "buyerName": "魏大勋", 
                "buyerPhone": "17707783992", 
                "buyerRemarks": "备注备注备注", 
                "postage": "(包邮产品)", 
                "status": 1, 
                "statusCN": "待发货", 
                "expressComNo": "959004", 
                "expressComName": "菜鸟", 
                "expressNo": "959001959001"
            }, 
            {
                "id": 49, 
                "orderAmt": "0.03", 
                "orderNo": "201908221137000002", 
                "storeNo": "201908221009000001", 
                "dealNo": "", 
                "orderTime": "2019-08-22 11:39:06", 
                "payTime": null, 
                "deliveryTime": null, 
                "finishTime": null, 
                "goods": [
                    {
                        "goodsNo": "201908211531000001", 
                        "goodsName": "卫龙-豆腐皮", 
                        "goodsNum": 1, 
                        "goodsPrice": "0.03"
                    }
                ], 
                "buyerName": "魏无羡", 
                "buyerPhone": "17707783992", 
                "buyerRemarks": "备注备注", 
                "postage": "(包邮产品)", 
                "status": 0, 
                "statusCN": "待付款", 
                "expressComNo": "959001", 
                "expressComName": "顺丰", 
                "expressNo": "959001959001"
            }, 
            {
                "id": 47, 
                "orderAmt": "1.50", 
                "orderNo": "201908221137000001", 
                "storeNo": "201908221009000001", 
                "dealNo": "", 
                "orderTime": "2019-08-22 11:38:12", 
                "payTime": null, 
                "deliveryTime": null, 
                "finishTime": null, 
                "goods": [
                    {
                        "goodsNo": "201908211531000001", 
                        "goodsName": "卫龙-辣条", 
                        "goodsNum": 50, 
                        "goodsPrice": "0.03"
                    }
                ], 
                "buyerName": "魏无羡", 
                "buyerPhone": "17707783992", 
                "buyerRemarks": "备注备注", 
                "postage": "(包邮产品)", 
                "status": 0, 
                "statusCN": "待付款", 
                "expressComNo": "959002", 
                "expressComName": "中通", 
                "expressNo": "959001959001"
            }, 
            {
                "id": 39, 
                "orderAmt": "0.02", 
                "orderNo": "201908211005000001", 
                "storeNo": "201908121230000001", 
                "dealNo": "190821141423863877", 
                "orderTime": "2019-08-21 11:27:51", 
                "payTime": "2019-08-21 14:14:49", 
                "deliveryTime": null, 
                "finishTime": null, 
                "goods": [
                    {
                        "goodsNo": "201908194501000001", 
                        "goodsName": "格力空调1.5P 一级能耗", 
                        "goodsNum": 1, 
                        "goodsPrice": "0.01"
                    }, 
                    {
                        "goodsNo": "201908194501000002", 
                        "goodsName": "格力空调1P 二级能耗", 
                        "goodsNum": 1, 
                        "goodsPrice": "0.01"
                    }
                ], 
                "buyerName": "张无忌", 
                "buyerPhone": "13011112222", 
                "buyerRemarks": "备注信息", 
                "postage": "(包邮产品)", 
                "status": 1, 
                "statusCN": "待发货", 
                "expressComNo": "959001", 
                "expressComName": "顺丰", 
                "expressNo": "959001959001"
            }, 
            {
                "id": 37, 
                "orderAmt": "0.02", 
                "orderNo": "201908204501000015", 
                "storeNo": "201908191230000002", 
                "dealNo": "190820211206139613", 
                "orderTime": "2019-08-20 21:11:13", 
                "payTime": "2019-08-20 21:12:17", 
                "deliveryTime": null, 
                "finishTime": null, 
                "goods": [
                    {
                        "goodsNo": "201908194501000001", 
                        "goodsName": "格力空调1.5P 一级能耗", 
                        "goodsNum": 1, 
                        "goodsPrice": "0.01"
                    }, 
                    {
                        "goodsNo": "201908194501000002", 
                        "goodsName": "格力空调大1P 一级能耗", 
                        "goodsNum": 1, 
                        "goodsPrice": "0.01"
                    }
                ], 
                "buyerName": "李四", 
                "buyerPhone": "13011113333", 
                "buyerRemarks": "备注信息", 
                "postage": "(包邮产品)", 
                "status": 1, 
                "statusCN": "待发货", 
                "expressComNo": "959003", 
                "expressComName": "邮政", 
                "expressNo": "959001959001"
            }, 
            {
                "id": 35, 
                "orderAmt": "0.01", 
                "orderNo": "201908204501000014", 
                "storeNo": "201908191230000002", 
                "dealNo": "190820201130042878", 
                "orderTime": "2019-08-20 20:10:54", 
                "payTime": "2019-08-20 20:11:59", 
                "deliveryTime": null, 
                "finishTime": null, 
                "goods": [
                    {
                        "goodsNo": "201908194501000001", 
                        "goodsName": "格力空调1.5P 一级能耗", 
                        "goodsNum": 1, 
                        "goodsPrice": "0.01"
                    }
                ], 
                "buyerName": "李四", 
                "buyerPhone": "13011113333", 
                "buyerRemarks": "备注信息", 
                "postage": "(包邮产品)", 
                "status": 1, 
                "statusCN": "待发货", 
                "expressComNo": "959004", 
                "expressComName": "菜鸟", 
                "expressNo": "959001959001"
            }
        ], 
        "total": 12, 
        "size": 10, 
        "current": 1, 
        "searchCount": true, 
        "pages": 2
    }
}

⑥Pair.java应用举例
a.代码
serviceImpl

@Override
    public Pair order(Integer id) {
        OrderVo order = orderMapper.order(id);
        if (order == null) return new Pair<>(false, null);
        if (order.getOrderNo() == null) return new Pair<>(false, null);
        List goodInfoVos = goodInfoMapper.goodInfos(order.getOrderNo());
        if (order.getExpressComNo() != null) {
            String expressName = expressInfoMapper.getExpressName(order.getExpressComNo());
            if (expressName != null) {
                order.setExpressComName(expressName);
            }
        }
        order.setGoods(goodInfoVos);

        return new Pair<>(true, order);
    }

service

Pair order(Integer id);

controller

    /**
     * 订单详情
     *
     * @return 订单详情
     */
    @GetMapping("/{id}")
    public R order(@PathVariable Integer id) {
        Pair order = orderService.order(id);
        return new R<>(order.getFirst() ? R.SUCCESS : R.FAIL, order.getSecond(), order.getFirst() ? "成功" : "失败");

    }

b.结果

{
    "msg": "成功", 
    "code": 0, 
    "data": {
        "id": 53, 
        "orderAmt": null, 
        "orderNo": "201909021116000001", 
        "storeNo": "201909021114000001", 
        "dealNo": "190902111937958730", 
        "orderTime": "2019-09-02 11:18:19", 
        "payTime": "2019-09-02 11:19:58", 
        "deliveryTime": null, 
        "finishTime": null, 
        "goods": [
            {
                "goodsNo": "201909021117000001", 
                "goodsName": "廖记棒棒鸡-冷串毛肚", 
                "goodsNum": 1, 
                "goodsPrice": "0.050000"
            }
        ], 
        "buyerName": "白敬亭", 
        "buyerPhone": "17707781234", 
        "buyerRemarks": "备注备注备注", 
        "postage": "12.000000", 
        "status": 0, 
        "statusCN": "待付款", 
        "expressComNo": null, 
        "expressComName": null, 
        "expressNo": null
    }
}

你可能感兴趣的:(java)