2026-02-24 23:26:41 +08:00
|
|
|
|
package com.mini.mybigscreen.Model;
|
|
|
|
|
|
|
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
|
|
2026-03-02 13:57:22 +08:00
|
|
|
|
import java.io.Serial;
|
2026-02-24 23:26:41 +08:00
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
2026-03-02 13:57:22 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 通用响应结果类
|
|
|
|
|
|
*/
|
2026-02-24 23:26:41 +08:00
|
|
|
|
@Data
|
|
|
|
|
|
public class Result<T> implements Serializable {
|
|
|
|
|
|
|
2026-03-02 13:57:22 +08:00
|
|
|
|
// 序列化版本号,避免序列化/反序列化异常
|
|
|
|
|
|
@Serial
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
2026-02-24 23:26:41 +08:00
|
|
|
|
private Integer code;
|
|
|
|
|
|
// 提示信息
|
|
|
|
|
|
private String msg;
|
2026-03-02 13:57:22 +08:00
|
|
|
|
// 业务数据体
|
2026-02-24 23:26:41 +08:00
|
|
|
|
private T data;
|
|
|
|
|
|
|
2026-03-02 13:57:22 +08:00
|
|
|
|
private Result() {}
|
|
|
|
|
|
|
|
|
|
|
|
private Result(Integer code, String msg, T data) {
|
|
|
|
|
|
this.code = code;
|
|
|
|
|
|
this.msg = msg;
|
|
|
|
|
|
this.data = data;
|
2026-02-24 23:26:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-02 13:57:22 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 成功返回(无数据,默认code=200,msg=操作成功)
|
|
|
|
|
|
*/
|
2026-02-24 23:26:41 +08:00
|
|
|
|
public static <T> Result<T> success() {
|
2026-03-02 13:57:22 +08:00
|
|
|
|
return new Result<>(200, "操作成功", null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 成功返回(带数据,默认code=200,msg=操作成功)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> success(T data) {
|
|
|
|
|
|
return new Result<>(200, "操作成功", data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 成功返回(自定义msg,默认code=200,无数据)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> successMsg(String msg) {
|
|
|
|
|
|
return new Result<>(200, msg, null);
|
2026-02-24 23:26:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-02 13:57:22 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 成功返回(自定义msg+数据,默认code=200)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> successMsg(String msg, T data) {
|
|
|
|
|
|
return new Result<>(200, msg, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 失败返回(默认code=500,自定义msg,无数据)
|
|
|
|
|
|
*/
|
2026-02-24 23:26:41 +08:00
|
|
|
|
public static <T> Result<T> error(String msg) {
|
2026-03-02 13:57:22 +08:00
|
|
|
|
return new Result<>(500, msg, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 失败返回(自定义code+msg,无数据)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> error(Integer code, String msg) {
|
|
|
|
|
|
return new Result<>(code, msg, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 失败返回(自定义code+msg+数据,按需使用)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> error(Integer code, String msg, T data) {
|
|
|
|
|
|
return new Result<>(code, msg, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 自定义响应结果(全参数)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> custom(Integer code, String msg, T data) {
|
|
|
|
|
|
return new Result<>(code, msg, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 仅通过状态码返回(默认msg为空,无数据)
|
|
|
|
|
|
* @param code 状态码
|
|
|
|
|
|
* @return 响应结果
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> byCode(Integer code) {
|
|
|
|
|
|
return new Result<>(code, "", null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 仅通过状态码+提示信息返回(无数据)
|
|
|
|
|
|
* @param code 状态码
|
|
|
|
|
|
* @param msg 提示信息
|
|
|
|
|
|
* @return 响应结果
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> byCode(Integer code, String msg) {
|
|
|
|
|
|
return new Result<>(code, msg, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 仅通过状态码+数据返回(默认msg为空)
|
|
|
|
|
|
* @param code 状态码
|
|
|
|
|
|
* @param data 业务数据
|
|
|
|
|
|
* @return 响应结果
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> byCode(Integer code, T data) {
|
|
|
|
|
|
return new Result<>(code, "", data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 快捷返回400参数错误(无数据)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> badRequest() {
|
|
|
|
|
|
return new Result<>(400, "参数错误", null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 快捷返回401未授权(无数据)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> unauthorized() {
|
|
|
|
|
|
return new Result<>(401, "未授权", null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 快捷返回403禁止访问(无数据)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> forbidden() {
|
|
|
|
|
|
return new Result<>(403, "禁止访问", null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 快捷返回404资源不存在(无数据)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> Result<T> notFound() {
|
|
|
|
|
|
return new Result<>(404, "资源不存在", null);
|
2026-02-24 23:26:41 +08:00
|
|
|
|
}
|
2026-03-02 13:57:22 +08:00
|
|
|
|
}
|