package space.anyi.BI.entity; import com.fasterxml.jackson.annotation.JsonInclude; import java.io.Serializable; /** * @Projectname: gditSpringBootLibrary * @Filename: ResponseResult * @Author: 杨逸 * @Data:2023/12/14 17:48 * @Description: 统一响应实体 */ @JsonInclude(JsonInclude.Include.NON_NULL) public class ResponseResult implements Serializable { private Integer code; private String msg; private T data; public ResponseResult() { this.code = AppHttpCodeEnum.SUCCESS.getCode(); this.msg = AppHttpCodeEnum.SUCCESS.getMsg(); } public ResponseResult(Integer code, T data) { this.code = code; this.data = data; } public ResponseResult(Integer code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } public ResponseResult(Integer code, String msg) { this.code = code; this.msg = msg; } public static ResponseResult errorResult(int code, String msg) { ResponseResult result = new ResponseResult(); return result.error(code, msg); } public static ResponseResult okResult() { ResponseResult result = new ResponseResult(); return result; } public static ResponseResult okResult(int code, String msg) { ResponseResult result = new ResponseResult(); return result.ok(code, null, msg); } public static ResponseResult okResult(Object data) { ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg()); if(data!=null) { result.setData(data); } return result; } public static ResponseResult errorResult(AppHttpCodeEnum enums){ return setAppHttpCodeEnum(enums,enums.getMsg()); } public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg){ return setAppHttpCodeEnum(enums,msg); } public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){ return okResult(enums.getCode(),enums.getMsg()); } private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg){ return okResult(enums.getCode(),msg); } public ResponseResult error(Integer code, String msg) { this.code = code; this.msg = msg; return this; } public ResponseResult ok(Integer code, T data) { this.code = code; this.data = data; return this; } public ResponseResult ok(Integer code, T data, String msg) { this.code = code; this.data = data; this.msg = msg; return this; } public ResponseResult ok(T data) { this.data = data; return this; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } /** * 内部类,响应状态枚举 */ public enum AppHttpCodeEnum { // 成功 SUCCESS(200,"操作成功"), // 登录 NEED_LOGIN(401,"需要登录后操作"), NO_OPERATOR_AUTH(403,"无权限操作"), FILE_NOT_NULL(410,"文件不能为空"), FILE_TYPE_ERROR(411,"文件类型错误"), SYSTEM_ERROR(500,"出现错误"), USERNAME_EXIST(501,"用户名已存在"), REQUIRE_USERNAME(502, "必需填写用户名"), LOGIN_ERROR(503,"用户名或密码错误"), USER_NOT_NULL(510,"用户不能为空"), PASSWORD_NOT_NULL(511,"密码不能为空"), PASSWORD_ERROR(514, "密码错误"), USER_NOT_EXIST(515, "用户不存在"), PASSWORD_SAME(516, "新密码不能与旧密码一样"), VERIFY_CODE_ERROR(517, "验证码错误"), USER_STATE_ERROR(518, "用户状态值不正确"), FILE_ERROR(528, "文件读取错误"), FILE_IS_EMPTY_ERROR(529, "文件内容不能为空"); private int code; private String msg; AppHttpCodeEnum(int code, String errorMessage){ this.code = code; this.msg = errorMessage; } public int getCode() { return code; } public String getMsg() { return msg; } } }