一、IResultCode
package com.hubu.mybatisflex.enumerate;
public interface IResultCode {
Integer getCode();
String getMessage();
Boolean getStatus();
}
二、ResultCode
package com.hubu.mybatisflex.enumerate;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
public enum ResultCode implements IResultCode{
OK(10000,true,"操作成功"),
ERROR(20000,false,"操作失败"),
USER_NOT_LOGIN(20000,false,"用户未登录"),
USER_ACCOUNT_EXPIRED(20000,false,"账户已过期"),
USER_ACCOUNT_LOCKED(20000,false,"账户已锁定"),
USER_ACCOUNT_NOT_EXISTS(20000,false,"账户不存在"),
TOKEN_IS_INVALID(20000,false,"token无效"),
TOKEN_IS_NULL(20000,false,"token不能为空"),
TOKEN_IS_EXPIRED(20000,false,"token失效");
private final Integer code;
private final Boolean status;
private final String message;
ResultCode(Integer code,Boolean status,String message){
this.code=code;
this.status=status;
this.message=message;
}
}
三、Result
package com.hubu.mybatisflex.utils;
import com.hubu.mybatisflex.enumerate.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
private Integer code;
private String message;
private Boolean status;
//是否展示
private Boolean shown;
private T data;
public static <T> Result<T> success(T data) {
return new Result<T>(ResultCode.OK.getCode(),ResultCode.OK.getMessage(),ResultCode.OK.getStatus(),true,data);
}
public static <T> Result<T> success(Boolean shown,T data) {
return new Result<T>(ResultCode.OK.getCode(),ResultCode.OK.getMessage(),ResultCode.OK.getStatus(),shown,data);
}
public static <T> Result<T> success(ResultCode resultCode,T data) {
return new Result<T>(resultCode.getCode(),resultCode.getMessage(),resultCode.getStatus(),true,data);
}
public static <T> Result<T> success(ResultCode resultCode,Boolean shown,T data) {
return new Result<T>(resultCode.getCode(),resultCode.getMessage(),resultCode.getStatus(),shown,data);
}
public static <T> Result<T> fail(T data) {
return new Result<T>(ResultCode.ERROR.getCode(),ResultCode.ERROR.getMessage(),ResultCode.ERROR.getStatus(),true,data);
}
public static <T> Result<T> fail(Boolean shown,T data) {
return new Result<T>(ResultCode.ERROR.getCode(),ResultCode.ERROR.getMessage(),ResultCode.ERROR.getStatus(),shown,data);
}
public static <T> Result<T> fail(ResultCode resultCode,T data) {
return new Result<T>(resultCode.getCode(),resultCode.getMessage(),resultCode.getStatus(),true,data);
}
public static <T> Result<T> fail(ResultCode resultCode,Boolean shown,T data) {
return new Result<T>(resultCode.getCode(),resultCode.getMessage(),resultCode.getStatus(),shown,data);
}
}