统计美化

This commit is contained in:
Leo
2025-04-01 00:32:22 +08:00
parent 63aa72fea4
commit e9f01b7e5b
6 changed files with 116 additions and 54 deletions

View File

@@ -0,0 +1,61 @@
package cn.van.business.model;
import com.alibaba.fastjson2.annotation.JSONField;
public class ApiResponse<T> {
// 状态码(建议使用 HTTP 状态码或自定义业务状态码)
private int code;
// 返回消息
private String message;
// 业务数据(使用泛型支持不同类型)
private T data;
// 时间戳(毫秒)
@JSONField(name = "timestamp")
private long timestamp;
// 常用状态码常量
public static final int CODE_SUCCESS = 200;
public static final int CODE_BAD_REQUEST = 400;
public static final int CODE_UNAUTHORIZED = 401;
public static final int CODE_SERVER_ERROR = 500;
// 构造方法私有化,通过静态工厂方法创建
private ApiResponse(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
this.timestamp = System.currentTimeMillis();
}
// 成功响应(带数据)
public static <T> ApiResponse<T> success(T data) {
return new ApiResponse<>(CODE_SUCCESS, "success", data);
}
// 成功响应(无数据)
public static ApiResponse<?> success() {
return success(null);
}
// 错误响应(自定义状态码和消息)
public static ApiResponse<?> error(int code, String message) {
return new ApiResponse<>(code, message, null);
}
// 快速错误响应(预定义类型)
public static ApiResponse<?> badRequest() {
return error(CODE_BAD_REQUEST, "Bad Request");
}
public static ApiResponse<?> unauthorized() {
return error(CODE_UNAUTHORIZED, "Unauthorized");
}
// getters 需要保留用于序列化
public int getCode() { return code; }
public String getMessage() { return message; }
public T getData() { return data; }
public long getTimestamp() { return timestamp; }
// setters 可省略或设为私有,根据实际需求
}