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 { @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 * @return */ public static CommonResult success(T data) { return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data); } /** * 成功返回结果 * * @param data 获取的数据 * @param message 提示信息 * @param * @return */ public static CommonResult success(T data, String message) { return new CommonResult(ResultCode.SUCCESS.getCode(), message, data); } /** * 失败返回结果 * * @param errorCode 错误码 * @param * @return */ public static CommonResult failed(IErrorCode errorCode) { return new CommonResult(errorCode.getCode(), errorCode.getMessage(), null); } /** * 失败返回结果 * * @param errorCode 错误码 * @param message 提示信息 * @param * @return */ public static CommonResult failed(IErrorCode errorCode, String message) { return new CommonResult(errorCode.getCode(), message, null); } /** * 失败返回结果 * * @param message 提示信息 * @param * @return */ public static CommonResult failed(String message) { return new CommonResult(ResultCode.FAILED.getCode(), message, null); } /** * 失败返回结果 * * @param * @return */ public static CommonResult failed() { return failed(ResultCode.FAILED); } /** * 参数验证失败返回结果 * * @param * @return */ public static CommonResult validateFailed() { return failed(ResultCode.VALIDATE_FAILED); } /** * 提示信息 * * @param message 提示信息 * @param * @return */ public static CommonResult validateFailed(String message) { return new CommonResult(ResultCode.VALIDATE_FAILED.getCode(), message, null); } /** * 未登录返回结果 * * @param data 获取的数据 * @param * @return */ public static CommonResult unauthorized(T data) { return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data); } /** * 重防及重复请求 * * @param data 获取的数据 * @param * @return */ public static CommonResult preventreplay(T data) { return new CommonResult(ResultCode.PREVENT_REPLAY.getCode(), ResultCode.PREVENT_REPLAY.getMessage(), data); } /** * 未授权返回结果 * * @param data 获取的数据 * @param * @return */ public static CommonResult forbidden(T data) { return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data); } }