You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.6 KiB
Java
153 lines
3.6 KiB
Java
package com.example.util;
|
|
|
|
import io.swagger.annotations.ApiModel;
|
|
import io.swagger.annotations.ApiModelProperty;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.Data;
|
|
import lombok.NoArgsConstructor;
|
|
|
|
/**
|
|
*
|
|
* @author docus
|
|
* @date 2020/3/28 16:23
|
|
*/
|
|
@Data
|
|
@ApiModel("响应")
|
|
@AllArgsConstructor
|
|
@NoArgsConstructor
|
|
public class CommonResult<T> {
|
|
@ApiModelProperty("响应码")
|
|
private Integer code;
|
|
@ApiModelProperty("响应消息")
|
|
private String msg;
|
|
@ApiModelProperty("响应实体")
|
|
private T data;
|
|
|
|
public CommonResult(Integer code, String msg) {
|
|
this.code = code;
|
|
this.msg = msg;
|
|
}
|
|
|
|
/**
|
|
* 成功返回结果
|
|
*
|
|
* @param data 获取的数据
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> success(T data) {
|
|
return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
|
|
}
|
|
|
|
/**
|
|
* 成功返回结果
|
|
*
|
|
* @param data 获取的数据
|
|
* @param message 提示信息
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> success(T data, String message) {
|
|
return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
|
|
}
|
|
|
|
/**
|
|
* 失败返回结果
|
|
*
|
|
* @param errorCode 错误码
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> failed(IErrorCode errorCode) {
|
|
return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
|
|
}
|
|
|
|
/**
|
|
* 失败返回结果
|
|
*
|
|
* @param errorCode 错误码
|
|
* @param message 提示信息
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> failed(IErrorCode errorCode, String message) {
|
|
return new CommonResult<T>(errorCode.getCode(), message, null);
|
|
}
|
|
|
|
/**
|
|
* 失败返回结果
|
|
*
|
|
* @param message 提示信息
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> failed(String message) {
|
|
return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
|
|
}
|
|
|
|
|
|
/**
|
|
* 失败返回结果
|
|
*
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> failed() {
|
|
return failed(ResultCode.FAILED);
|
|
}
|
|
|
|
/**
|
|
* 参数验证失败返回结果
|
|
*
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> validateFailed() {
|
|
return failed(ResultCode.VALIDATE_FAILED);
|
|
}
|
|
|
|
/**
|
|
* 提示信息
|
|
*
|
|
* @param message 提示信息
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> validateFailed(String message) {
|
|
return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
|
|
}
|
|
|
|
/**
|
|
* 未登录返回结果
|
|
*
|
|
* @param data 获取的数据
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> unauthorized(T data) {
|
|
return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
|
|
}
|
|
|
|
/**
|
|
* 重防及重复请求
|
|
*
|
|
* @param data 获取的数据
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> preventreplay(T data) {
|
|
return new CommonResult<T>(ResultCode.PREVENT_REPLAY.getCode(), ResultCode.PREVENT_REPLAY.getMessage(), data);
|
|
}
|
|
|
|
/**
|
|
* 未授权返回结果
|
|
*
|
|
* @param data 获取的数据
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static <T> CommonResult<T> forbidden(T data) {
|
|
return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
|
|
}
|
|
}
|