You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

173 lines
5.7 KiB

package com.ccsens.util;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel
public class JsonResponse<T> {
/**
* Common Error Constant
*/
public static class RegularError {
public static final int ERRCODE_SUCCESS = 200;
public static final String ERRCODE_SUCCESS_PHASE = "ok";
public static final int ERRCODE_FAIL = -1;
public static final String ERRCODE_FAIL_PHASE = "error";
}
/**
* Jwt Error Constant
*/
public static class TokenError{
public static final int TOKEN_ERRCODE_OK = JwtUtil.JwtError.TOKEN_ERRCODE_OK;
public static final String TOKEN_ERRCODE_OK_PHASE = JwtUtil.JwtError.TOKEN_ERRCODE_OK_PHASE;
public static final int TOKEN_ERRCODE_NOTFOUND = JwtUtil.JwtError.TOKEN_ERRCODE_NOTFOUND;
public static final String TOKEN_ERRCODE_NOTFOUND_PHASE = JwtUtil.JwtError.TOKEN_ERRCODE_NOTFOUND_PHASE;
public static final int TOKEN_ERRCODE_SIGNATURE_INVALIDATE = JwtUtil.JwtError.TOKEN_ERRCODE_SIGNATURE_INVALIDATE;
public static final String TOKEN_ERRCODE_SIGNATURE_INVALIDATE_PHASE = JwtUtil.JwtError.TOKEN_ERRCODE_SIGNATURE_INVALIDATE_PHASE;
public static final int TOKEN_ERRCODE_EXPIRE = JwtUtil.JwtError.TOKEN_ERRCODE_EXPIRE;
public static final String TOKEN_ERRCODE_EXPIRE_PHASE = JwtUtil.JwtError.TOKEN_ERRCODE_EXPIRE_PHASE;
public static final int TOKEN_ERRCODE_STUB_NOT_FOUND = JwtUtil.JwtError.TOKEN_ERRCODE_STUB_NOT_FOUND;
public static final String TOKEN_ERRCODE_STUB_NOT_FOUND_PHASE = JwtUtil.JwtError.TOKEN_ERRCODE_STUB_NOT_FOUND_PHASE;
public static final int TOKEN_ERRCODE_USER_DISABLED = JwtUtil.JwtError.TOKEN_ERRCODE_USER_DISABLED;
public static final String TOKEN_ERRCODE_USER_DISABLED_PHASE = JwtUtil.JwtError.TOKEN_ERRCODE_USER_DISABLED_PHASE;
public static final int TOKEN_ERRCODE_FAILED = JwtUtil.JwtError.TOKEN_ERRCODE_FAILED;
public static final String TOKEN_ERRCODE_FAILED_PHASE = JwtUtil.JwtError.TOKEN_ERRCODE_FAILED_PHASE;
}
@ApiModelProperty(value="状态码")
private Integer code;
@ApiModelProperty(value="数据")
private T data;
@ApiModelProperty(value="消息")
private String msg;
@ApiModelProperty(value="成功与否")
private boolean success;
private JsonResponse() {
}
public static JsonResponse newInstance(){
return new JsonResponse();
}
public JsonResponse ok(){
this.code = CodeEnum.SUCCESS.getCode();
this.msg = CodeEnum.SUCCESS.getMsg();
this.success = CodeEnum.SUCCESS.isSuccess();
return this;
}
public JsonResponse ok(T data){
ok();
this.data = data;
return this;
}
public JsonResponse ok(CodeEnum codeEnum){
this.code = codeEnum.getCode();
this.msg = codeEnum.getMsg();
this.success = codeEnum.isSuccess();
return this;
}
public JsonResponse ok(CodeEnum codeEnum, T data){
this.code = codeEnum.getCode();
this.msg = codeEnum.getMsg();
this.success = codeEnum.isSuccess();
this.data = data;
return this;
}
public JsonResponse fail(){
this.code = RegularError.ERRCODE_FAIL;
this.msg = RegularError.ERRCODE_FAIL_PHASE;
this.success = false;
return this;
}
public JsonResponse fail(String error){
fail();
this.msg = error;
return this;
}
public JsonResponse fail(int code){
fail();
this.code = code;
return this;
}
public JsonResponse fail(int code, String error){
fail();
this.code = code;
this.msg = error;
return this;
}
public JsonResponse fail(int code, String error, T obj){
fail();
this.code = code;
this.msg = error;
this.data = obj;
return this;
}
//Token null
public JsonResponse tokenNotFound(){
fail(TokenError.TOKEN_ERRCODE_NOTFOUND, TokenError.TOKEN_ERRCODE_NOTFOUND_PHASE);
return this;
}
//Token expire
public JsonResponse tokenExpire(){
fail(TokenError.TOKEN_ERRCODE_EXPIRE, TokenError.TOKEN_ERRCODE_EXPIRE_PHASE);
return this;
}
public JsonResponse tokenExpire(String phase){
fail(TokenError.TOKEN_ERRCODE_EXPIRE,phase);
return this;
}
public JsonResponse tokenSignatureFail(){
fail(TokenError.TOKEN_ERRCODE_SIGNATURE_INVALIDATE, TokenError.TOKEN_ERRCODE_SIGNATURE_INVALIDATE_PHASE);
return this;
}
public JsonResponse tokenSignatureFail(String phase){
fail(TokenError.TOKEN_ERRCODE_SIGNATURE_INVALIDATE,phase);
return this;
}
//Token Stub Not Found
public JsonResponse tokenStubNotFound(){
fail(TokenError.TOKEN_ERRCODE_STUB_NOT_FOUND, TokenError.TOKEN_ERRCODE_STUB_NOT_FOUND_PHASE);
return this;
}
public JsonResponse tokenDisabled(String phase){
fail(TokenError.TOKEN_ERRCODE_STUB_NOT_FOUND,phase);
return this;
}
//User Disabled
public JsonResponse userDisabled(){
fail(TokenError.TOKEN_ERRCODE_USER_DISABLED, TokenError.TOKEN_ERRCODE_USER_DISABLED_PHASE);
return this;
}
public JsonResponse userDisabled(String phase){
fail(TokenError.TOKEN_ERRCODE_USER_DISABLED,phase);
return this;
}
//Token Failed
public JsonResponse tokenFailed(){
fail(TokenError.TOKEN_ERRCODE_FAILED, TokenError.TOKEN_ERRCODE_FAILED_PHASE);
return this;
}
public JsonResponse tokenFailed(String phase){
fail(TokenError.TOKEN_ERRCODE_FAILED,phase);
return this;
}
}