package com.sinata.rest.common;
|
|
import com.sinata.rest.common.i18n.I18nUtils;
|
import io.swagger.annotations.ApiModel;
|
import io.swagger.annotations.ApiModelProperty;
|
import lombok.Data;
|
|
@Data
|
@ApiModel("API接口工具类")
|
public class ApiUtils<T> {
|
|
/**
|
* 返回参数代码
|
*/
|
public static final int CODE_OK = 0;
|
public static final String MESSAGE_OK = "message.ok";
|
public static final int CODE_NG = 1;
|
public static final String MESSAGE_NG = "message.error";
|
|
/**
|
* 基础参数
|
*/
|
@ApiModelProperty("标识码:0成功,1失败,其他自定义")
|
public int code;
|
@ApiModelProperty("提示消息")
|
public String message;
|
|
/**
|
* 扩展参数
|
*/
|
@ApiModelProperty("返回数据")
|
public T data;
|
|
public ApiUtils() {
|
this.code = CODE_OK;
|
}
|
|
public ApiUtils(Integer code) {
|
this.code = code;
|
}
|
|
public ApiUtils(Integer code, String message) {
|
this.code = code;
|
this.message = message;
|
}
|
|
public ApiUtils(Integer code, String message, T data) {
|
this.code = code;
|
this.message = message;
|
this.data = data;
|
}
|
|
/**
|
* 国际化数据映射消息
|
*
|
* @param message
|
*/
|
public void setI8nMessage(String message) {
|
this.message = I18nUtils.getMessage(message);
|
}
|
|
/**
|
* 封装接口返回数据
|
*/
|
public static <T> ApiUtils<T> returnOK() {
|
return new ApiUtils(CODE_OK, I18nUtils.getMessage(MESSAGE_OK));
|
}
|
|
public static <T> ApiUtils<T> returnOK(T data) {
|
return new ApiUtils(CODE_OK, I18nUtils.getMessage(MESSAGE_OK), data);
|
}
|
|
public static <T> ApiUtils<T> returnOK(T data, String message) {
|
return new ApiUtils(CODE_OK, message, data);
|
}
|
|
public static <T> ApiUtils<T> returnOK(T data, String message, Integer code) {
|
return new ApiUtils(code, message, data);
|
}
|
|
/**
|
* 封装接口返回数据(失败)
|
*/
|
public static <T> ApiUtils<T> returnNG() {
|
return new ApiUtils(CODE_NG, I18nUtils.getMessage(MESSAGE_NG));
|
}
|
|
public static <T> ApiUtils<T> returnNG(T data) {
|
return new ApiUtils(CODE_NG, I18nUtils.getMessage(MESSAGE_NG), data);
|
}
|
|
public static <T> ApiUtils<T> returnNG(T data, String message) {
|
return new ApiUtils(CODE_NG, message, data);
|
}
|
|
public static <T> ApiUtils<T> returnNG(T data, String message, Integer code) {
|
return new ApiUtils(code, message, data);
|
}
|
|
}
|