| | |
| | | package com.ruoyi.common.core.domain; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Objects; |
| | | import java.io.Serializable; |
| | | import com.ruoyi.common.constant.HttpStatus; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * 操作消息提醒 |
| | | * 响应信息主体 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class Result extends HashMap<String, Object> |
| | | public class Result<T> implements Serializable |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** 状态码 */ |
| | | public static final String CODE_TAG = "code"; |
| | | /** 成功 */ |
| | | public static final int SUCCESS = HttpStatus.SUCCESS; |
| | | |
| | | /** 返回内容 */ |
| | | public static final String MSG_TAG = "msg"; |
| | | /** 失败 */ |
| | | public static final int FAIL = HttpStatus.ERROR; |
| | | |
| | | /** 数据对象 */ |
| | | public static final String DATA_TAG = "data"; |
| | | private int code; |
| | | |
| | | /** |
| | | * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。 |
| | | */ |
| | | public Result() |
| | | private String msg; |
| | | |
| | | private T data; |
| | | |
| | | public static <T> Result<T> success() |
| | | { |
| | | return restResult(null, SUCCESS, "操作成功"); |
| | | } |
| | | |
| | | /** |
| | | * 初始化一个新创建的 AjaxResult 对象 |
| | | * |
| | | * @param code 状态码 |
| | | * @param msg 返回内容 |
| | | */ |
| | | public Result(int code, String msg) |
| | | public static <T> Result<T> success(T data) |
| | | { |
| | | super.put(CODE_TAG, code); |
| | | super.put(MSG_TAG, msg); |
| | | return restResult(data, SUCCESS, "操作成功"); |
| | | } |
| | | |
| | | /** |
| | | * 初始化一个新创建的 AjaxResult 对象 |
| | | * |
| | | * @param code 状态码 |
| | | * @param msg 返回内容 |
| | | * @param data 数据对象 |
| | | */ |
| | | public Result(int code, String msg, Object data) |
| | | public static <T> Result<T> success(T data, String msg) |
| | | { |
| | | super.put(CODE_TAG, code); |
| | | super.put(MSG_TAG, msg); |
| | | if (StringUtils.isNotNull(data)) |
| | | { |
| | | super.put(DATA_TAG, data); |
| | | } |
| | | return restResult(data, SUCCESS, msg); |
| | | } |
| | | |
| | | /** |
| | | * 返回成功消息 |
| | | * |
| | | * @return 成功消息 |
| | | */ |
| | | public static Result success() |
| | | public static <T> Result<T> fail() |
| | | { |
| | | return Result.success("操作成功"); |
| | | return restResult(null, FAIL, "操作失败"); |
| | | } |
| | | |
| | | /** |
| | | * 返回成功数据 |
| | | * |
| | | * @return 成功消息 |
| | | */ |
| | | public static Result success(Object data) |
| | | public static <T> Result<T> fail(String msg) |
| | | { |
| | | return Result.success("操作成功", data); |
| | | return restResult(null, FAIL, msg); |
| | | } |
| | | |
| | | /** |
| | | * 返回成功消息 |
| | | * |
| | | * @param msg 返回内容 |
| | | * @return 成功消息 |
| | | */ |
| | | public static Result success(String msg) |
| | | public static <T> Result<T> fail(T data) |
| | | { |
| | | return Result.success(msg, null); |
| | | return restResult(data, FAIL, "操作失败"); |
| | | } |
| | | |
| | | /** |
| | | * 返回成功消息 |
| | | * |
| | | * @param msg 返回内容 |
| | | * @param data 数据对象 |
| | | * @return 成功消息 |
| | | */ |
| | | public static Result success(String msg, Object data) |
| | | public static <T> Result<T> fail(T data, String msg) |
| | | { |
| | | return new Result(HttpStatus.SUCCESS, msg, data); |
| | | return restResult(data, FAIL, msg); |
| | | } |
| | | |
| | | /** |
| | | * 返回警告消息 |
| | | * |
| | | * @param msg 返回内容 |
| | | * @return 警告消息 |
| | | */ |
| | | public static Result warn(String msg) |
| | | public static <T> Result<T> fail(int code, String msg) |
| | | { |
| | | return Result.warn(msg, null); |
| | | return restResult(null, code, msg); |
| | | } |
| | | |
| | | /** |
| | | * 返回警告消息 |
| | | * |
| | | * @param msg 返回内容 |
| | | * @param data 数据对象 |
| | | * @return 警告消息 |
| | | */ |
| | | public static Result warn(String msg, Object data) |
| | | private static <T> Result<T> restResult(T data, int code, String msg) |
| | | { |
| | | return new Result(HttpStatus.WARN, msg, data); |
| | | Result<T> apiResult = new Result<>(); |
| | | apiResult.setCode(code); |
| | | apiResult.setData(data); |
| | | apiResult.setMsg(msg); |
| | | return apiResult; |
| | | } |
| | | |
| | | /** |
| | | * 返回错误消息 |
| | | * |
| | | * @return 错误消息 |
| | | */ |
| | | public static Result error() |
| | | public int getCode() |
| | | { |
| | | return Result.error("操作失败"); |
| | | return code; |
| | | } |
| | | |
| | | /** |
| | | * 返回错误消息 |
| | | * |
| | | * @param msg 返回内容 |
| | | * @return 错误消息 |
| | | */ |
| | | public static Result error(String msg) |
| | | public void setCode(int code) |
| | | { |
| | | return Result.error(msg, null); |
| | | this.code = code; |
| | | } |
| | | |
| | | /** |
| | | * 返回错误消息 |
| | | * |
| | | * @param msg 返回内容 |
| | | * @param data 数据对象 |
| | | * @return 错误消息 |
| | | */ |
| | | public static Result error(String msg, Object data) |
| | | public String getMsg() |
| | | { |
| | | return new Result(HttpStatus.ERROR, msg, data); |
| | | return msg; |
| | | } |
| | | |
| | | /** |
| | | * 返回错误消息 |
| | | * |
| | | * @param code 状态码 |
| | | * @param msg 返回内容 |
| | | * @return 错误消息 |
| | | */ |
| | | public static Result error(int code, String msg) |
| | | public void setMsg(String msg) |
| | | { |
| | | return new Result(code, msg, null); |
| | | this.msg = msg; |
| | | } |
| | | |
| | | /** |
| | | * 是否为成功消息 |
| | | * |
| | | * @return 结果 |
| | | */ |
| | | public boolean isSuccess() |
| | | public T getData() |
| | | { |
| | | return Objects.equals(HttpStatus.SUCCESS, this.get(CODE_TAG)); |
| | | return data; |
| | | } |
| | | |
| | | /** |
| | | * 是否为警告消息 |
| | | * |
| | | * @return 结果 |
| | | */ |
| | | public boolean isWarn() |
| | | public void setData(T data) |
| | | { |
| | | return Objects.equals(HttpStatus.WARN, this.get(CODE_TAG)); |
| | | this.data = data; |
| | | } |
| | | |
| | | /** |
| | | * 是否为错误消息 |
| | | * |
| | | * @return 结果 |
| | | */ |
| | | public boolean isError() |
| | | public static <T> Boolean isError(Result<T> ret) |
| | | { |
| | | return Objects.equals(HttpStatus.ERROR, this.get(CODE_TAG)); |
| | | return !isSuccess(ret); |
| | | } |
| | | |
| | | /** |
| | | * 方便链式调用 |
| | | * |
| | | * @param key 键 |
| | | * @param value 值 |
| | | * @return 数据对象 |
| | | */ |
| | | @Override |
| | | public Result put(String key, Object value) |
| | | public static <T> Boolean isSuccess(Result<T> ret) |
| | | { |
| | | super.put(key, value); |
| | | return this; |
| | | return Result.SUCCESS == ret.getCode(); |
| | | } |
| | | } |