171 lines
4.7 KiB
Java
171 lines
4.7 KiB
Java
package com.gunshi.project.xyt.util;
|
||
|
||
import io.swagger.v3.oas.annotations.media.Schema;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import java.io.Serializable;
|
||
import java.util.Date;
|
||
|
||
@Schema(description = "数据传输对象")
|
||
@Component
|
||
@SuppressWarnings("all")
|
||
public class ResultJson<T> implements Serializable {
|
||
|
||
private static final long serialVersionUID = 1L;
|
||
/**
|
||
* 成功
|
||
*/
|
||
public static final Integer SUCCESS = 200;
|
||
public static final String SUCCESS_MSG = "success";
|
||
|
||
/**
|
||
* 失败
|
||
*/
|
||
public static final Integer FAIL = 900;
|
||
public static final String FAIL_MSG = "fail";
|
||
|
||
public static final Integer PARAM_ERROR = 400; // 失败、参数错误等
|
||
public static final Integer UNAUTHORIZED = 401; // 用户验证失败,或者用户验证信息过期
|
||
public static final Integer PERMISSION_DENIED = 403; // 没有权限
|
||
public static final Integer NOT_FOUND = 404; // 未找到资源
|
||
public static final Integer METHOD_NOT_ALLOWED = 405; // 不支持的类型
|
||
public static final Integer NSUPPORTED_MEDIA_TYPE = 415; // 不支持的媒体
|
||
public static final Integer NOT_ALLOWED = 405; // 请求太频繁,同一个用户(token)、同一个url、同样的请求参数,请求间隔小于0.5秒
|
||
public static final Integer SERVER_ERROR = 500; // 后台错误
|
||
public static final Integer SRC_TIMEOUT = 504; // 请求资源超时
|
||
|
||
|
||
@Schema(description="数据消息")
|
||
private String msg;
|
||
|
||
@Schema(description="传输状态码(200=成功;400=参数错误; 500=后端错误;900=失败)")
|
||
private Integer code;
|
||
|
||
@Schema(description="传输数据")
|
||
private T data;
|
||
|
||
@Schema(description="接口响应时间戳")
|
||
private String restm; // 接口响应时间戳。如果有缓存,则该时间表示最后接口响应时间
|
||
|
||
public static ResultJson error() {
|
||
return error(FAIL, "未知异常,请联系管理员");
|
||
}
|
||
|
||
public static ResultJson error(String msg) {
|
||
return error(FAIL, msg);
|
||
}
|
||
|
||
public static ResultJson error(int code, String msg) {
|
||
ResultJson r = new ResultJson();
|
||
r.setCode(code);
|
||
r.setMsg(msg);
|
||
r.setRestm(DateUtil.sdfhmsS.get().format(new Date()));
|
||
return r;
|
||
}
|
||
|
||
public static ResultJson error(int code, String msg, Object data) {
|
||
ResultJson r = new ResultJson();
|
||
r.setCode(code);
|
||
r.setMsg(msg);
|
||
r.setData(data);
|
||
r.setRestm(DateUtil.sdfhmsS.get().format(new Date()));
|
||
return r;
|
||
}
|
||
|
||
public static ResultJson ok(Integer code, String msg) {
|
||
ResultJson r = new ResultJson();
|
||
r.setMsg(msg);
|
||
r.setCode(code);
|
||
r.setRestm(DateUtil.sdfhmsS.get().format(new Date()));
|
||
return r;
|
||
}
|
||
|
||
public static ResultJson ok() {
|
||
ResultJson r = new ResultJson();
|
||
r.setCode(SUCCESS);
|
||
r.setMsg(SUCCESS_MSG);
|
||
r.setRestm(DateUtil.sdfhmsS.get().format(new Date()));
|
||
return r;
|
||
}
|
||
|
||
public static ResultJson ok(Object data) {
|
||
ResultJson r = new ResultJson();
|
||
r.setCode(SUCCESS);
|
||
r.setMsg(SUCCESS_MSG);
|
||
r.setData(data);
|
||
r.setRestm(DateUtil.sdfhmsS.get().format(new Date()));
|
||
return r;
|
||
}
|
||
|
||
public static ResultJson ok(Object data, String msg) {
|
||
ResultJson r = new ResultJson();
|
||
r.setData(data);
|
||
r.setCode(SUCCESS);
|
||
r.setMsg(msg);
|
||
r.setRestm(DateUtil.sdfhmsS.get().format(new Date()));
|
||
return r;
|
||
}
|
||
|
||
public String getMsg() {
|
||
return msg;
|
||
}
|
||
|
||
public void setMsg(String msg) {
|
||
this.msg = msg;
|
||
}
|
||
|
||
public Integer getCode() {
|
||
return code;
|
||
}
|
||
|
||
public void setCode(Integer code) {
|
||
this.code = code;
|
||
}
|
||
|
||
public T getData() {
|
||
return data;
|
||
}
|
||
|
||
public void setData(T data) {
|
||
this.data = data;
|
||
}
|
||
|
||
public static long getSerialVersionUID() {
|
||
return serialVersionUID;
|
||
}
|
||
|
||
public static Integer getSUCCESS() {
|
||
return SUCCESS;
|
||
}
|
||
|
||
public static String getSuccessMsg() {
|
||
return SUCCESS_MSG;
|
||
}
|
||
|
||
public static Integer getFAIL() {
|
||
return FAIL;
|
||
}
|
||
|
||
public static String getFailMsg() {
|
||
return FAIL_MSG;
|
||
}
|
||
|
||
public String getRestm() {
|
||
return restm;
|
||
}
|
||
|
||
public void setRestm(String restm) {
|
||
this.restm = restm;
|
||
}
|
||
|
||
@Override
|
||
public String toString() {
|
||
return "ResultJson{" +
|
||
"msg='" + msg + "'" +
|
||
", code=" + code +
|
||
", data=" + data +
|
||
", restm=" + restm +
|
||
'}';
|
||
}
|
||
}
|