package com.dsh.course.util; /** * 定义统一返回对象 */ public class ResultUtil { public static final Integer SUCCESS = 200; public static final Integer PARAM_ERROR = 300; public static final Integer RUNTIME_ERROR = 400; public static final Integer ERROR = 500; public static final String Token = "Token失效"; private Integer status;//状态码 private Integer code;//备用状态码 private String msg;//返回说明 private T data;//返回数据 private String str;//存储单个字符串值 public Integer getStatus() { return status; } public String getMsg() { return msg; } public T getData() { return data; } public String getStr() { return str; } public Integer getCode() { return code; } public ResultUtil(Integer status, Integer code, String msg) { this.status = status; this.code = code; this.msg = msg; } private ResultUtil(Integer status, Integer code, String msg, T data) { this.status = status; this.code = code; this.msg = msg; this.data = data; } public ResultUtil(Integer status, Integer code, String msg, T data, String str) { this.status = status; this.code = code; this.msg = msg; this.data = data; this.str = str; } private ResultUtil(Integer status, Integer code, String msg, String str) { this.status = status; this.code = code; this.msg = msg; this.str = str; } public static ResultUtil getResult(Integer status, Integer code, String msg) { return new ResultUtil<>(status, code, msg); } public static ResultUtil getResult(Integer status, Integer code, String msg, T data) { return new ResultUtil<>(status, code, msg, data); } public static ResultUtil getResult(Integer status, Integer code, String msg, T data, String str) { return new ResultUtil<>(status, code, msg, data, str); } /** * 错误信息 * * @return */ public static ResultUtil error(String mag) { return ResultUtil.getResult(ResultUtil.ERROR, ResultUtil.ERROR, mag); } /** * 错误信息 * * @return */ public static ResultUtil error(String mag, T obj) { return ResultUtil.getResult(ResultUtil.ERROR, ResultUtil.ERROR, mag, obj); } /** * token失效 * * @return */ public static ResultUtil tokenErr() { return ResultUtil.getResult(ResultUtil.ERROR, ResultUtil.ERROR, ResultUtil.Token); } /** * token失效 * * @return */ public static ResultUtil tokenErr(String msg) { return ResultUtil.getResult(ResultUtil.ERROR, ResultUtil.ERROR, msg); } /** * 参数异常 * * @return */ public static ResultUtil paranErr() { return ResultUtil.getResult(ResultUtil.PARAM_ERROR, ResultUtil.PARAM_ERROR, "参数异常"); } /** * 参数异常 * * @return */ public static ResultUtil paranErr(T data) { return ResultUtil.getResult(ResultUtil.PARAM_ERROR, ResultUtil.PARAM_ERROR, "参数异常", data); } /** * 运行异常 * * @return */ public static ResultUtil runErr() { return ResultUtil.getResult(ResultUtil.RUNTIME_ERROR, ResultUtil.RUNTIME_ERROR, "服务器运行异常"); } /** * 运行异常 * * @return */ public static ResultUtil runErr(T data) { return ResultUtil.getResult(ResultUtil.RUNTIME_ERROR, ResultUtil.RUNTIME_ERROR, "服务器运行异常", data); } /** * 返回成功 * * @param * @return */ public static ResultUtil success() { return ResultUtil.getResult(ResultUtil.SUCCESS, ResultUtil.SUCCESS, "成功"); } /** * 返回成功 * * @param data * @param * @return */ public static ResultUtil success(T data) { return ResultUtil.getResult(ResultUtil.SUCCESS, ResultUtil.SUCCESS, "成功", data); } /** * 返回成功 * * @param data * @param str * @param * @return */ public static ResultUtil success(T data, String str) { return ResultUtil.getResult(ResultUtil.SUCCESS, ResultUtil.SUCCESS, "成功", data, str); } public static ResultUtil success(String msg, T data) { return ResultUtil.getResult(ResultUtil.SUCCESS, ResultUtil.SUCCESS, msg, data); } public static ResultUtil success(String msg, T data, String str) { return ResultUtil.getResult(ResultUtil.SUCCESS, ResultUtil.SUCCESS, msg, data, str); } }