ResponseUtil

Posted tonggc1668

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ResponseUtil相关的知识,希望对你有一定的参考价值。

package org.linlinjava.litemall.core.util;

import com.github.pagehelper.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 响应操作结果
 * <pre>
 *  {
 *      errno: 错误码,
 *      errmsg:错误消息,
 *      data:  响应数据
 *  }
 * </pre>
 *
 * <p>
 * 错误码:
 * <ul>
 * <li> 0,成功;
 * <li> 4xx,前端错误,说明前端开发者需要重新了解后端接口使用规范:
 * <ul>
 * <li> 401,参数错误,即前端没有传递后端需要的参数;
 * <li> 402,参数值错误,即前端传递的参数值不符合后端接收范围。
 * </ul>
 * <li> 5xx,后端错误,除501外,说明后端开发者应该继续优化代码,尽量避免返回后端错误码:
 * <ul>
 * <li> 501,验证失败,即后端要求用户登录;
 * <li> 502,系统内部错误,即没有合适命名的后端内部错误;
 * <li> 503,业务不支持,即后端虽然定义了接口,但是还没有实现功能;
 * <li> 504,更新数据失效,即后端采用了乐观锁更新,而并发更新时存在数据更新失效;
 * <li> 505,更新数据失败,即后端数据库更新失败(正常情况应该更新成功)。
 * </ul>
 * <li> 6xx,小商城后端业务错误码,
 * 具体见litemall-admin-api模块的AdminResponseCode。
 * <li> 7xx,管理后台后端业务错误码,
 * 具体见litemall-wx-api模块的WxResponseCode。
 * </ul>
 */
public class ResponseUtil {
    public static Object ok() {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", 0);
        obj.put("errmsg", "成功");
        return obj;
    }

    public static Object ok(Object data) {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", 0);
        obj.put("errmsg", "成功");
        obj.put("data", data);
        return obj;
    }

    public static Object okList(List list) {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", list);

        if (list instanceof Page) {
            Page page = (Page) list;
            data.put("total", page.getTotal());
            data.put("page", page.getPageNum());
            data.put("limit", page.getPageSize());
            data.put("pages", page.getPages());
        } else {
            data.put("total", list.size());
            data.put("page", 1);
            data.put("limit", list.size());
            data.put("pages", 1);
        }

        return ok(data);
    }

    public static Object okList(List list, List pagedList) {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", list);

        if (pagedList instanceof Page) {
            Page page = (Page) pagedList;
            data.put("total", page.getTotal());
            data.put("page", page.getPageNum());
            data.put("limit", page.getPageSize());
            data.put("pages", page.getPages());
        } else {
            data.put("total", pagedList.size());
            data.put("page", 1);
            data.put("limit", pagedList.size());
            data.put("pages", 1);
        }

        return ok(data);
    }

    public static Object fail() {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", -1);
        obj.put("errmsg", "错误");
        return obj;
    }

    public static Object fail(int errno, String errmsg) {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", errno);
        obj.put("errmsg", errmsg);
        return obj;
    }

    public static Object badArgument() {
        return fail(401, "参数不对");
    }

    public static Object badArgumentValue() {
        return fail(402, "参数值不对");
    }

    public static Object unlogin() {
        return fail(501, "请登录");
    }

    public static Object serious() {
        return fail(502, "系统内部错误");
    }

    public static Object unsupport() {
        return fail(503, "业务不支持");
    }

    public static Object updatedDateExpired() {
        return fail(504, "更新数据已经失效");
    }

    public static Object updatedDataFailed() {
        return fail(505, "更新数据失败");
    }

    public static Object unauthz() {
        return fail(506, "无操作权限");
    }
}

 

package org.linlinjava.litemall.admin.util;

public class AdminResponseCode {
    public static final Integer ADMIN_INVALID_NAME = 601;
    public static final Integer ADMIN_INVALID_PASSWORD = 602;
    public static final Integer ADMIN_NAME_EXIST = 602;
    public static final Integer ADMIN_ALTER_NOT_ALLOWED = 603;
    public static final Integer ADMIN_DELETE_NOT_ALLOWED = 604;
    public static final Integer ADMIN_INVALID_ACCOUNT = 605;
    public static final Integer GOODS_UPDATE_NOT_ALLOWED = 610;
    public static final Integer GOODS_NAME_EXIST = 611;
    public static final Integer ORDER_CONFIRM_NOT_ALLOWED = 620;
    public static final Integer ORDER_REFUND_FAILED = 621;
    public static final Integer ORDER_REPLY_EXIST = 622;
    public static final Integer USER_INVALID_NAME = 630;
    public static final Integer USER_INVALID_PASSWORD = 631;
    public static final Integer USER_INVALID_MOBILE = 632;
    public static final Integer USER_NAME_EXIST = 633;
    public static final Integer USER_MOBILE_EXIST = 634;
    public static final Integer ROLE_NAME_EXIST = 640;
    public static final Integer ROLE_SUPER_SUPERMISSION = 641;
    public static final Integer ROLE_USER_EXIST = 642;

}
    private Object validate(LitemallAdmin admin) {
        String name = admin.getUsername();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        if (!RegexUtil.isUsername(name)) {
            return ResponseUtil.fail(ADMIN_INVALID_NAME, "管理员名称不符合规定");
        }
        String password = admin.getPassword();
        if (StringUtils.isEmpty(password) || password.length() < 6) {
            return ResponseUtil.fail(ADMIN_INVALID_PASSWORD, "管理员密码长度不能小于6");
        }
        return null;
    }

 

以上是关于ResponseUtil的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

jquery easyui-combobox 只能选第一个选项

VSCode自定义代码片段——.vue文件的模板