¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: Chill åºéª (smallchill@163.com) |
| | | */ |
| | | package org.springblade.core.tool.api; |
| | | |
| | | import io.swagger.annotations.ApiModel; |
| | | import io.swagger.annotations.ApiModelProperty; |
| | | import lombok.*; |
| | | import org.springblade.core.tool.constant.BladeConstant; |
| | | import org.springblade.core.tool.utils.ObjectUtil; |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.Serializable; |
| | | import java.util.Optional; |
| | | |
| | | /** |
| | | * ç»ä¸APIååºç»æå°è£
|
| | | * |
| | | * @author Chill |
| | | */ |
| | | @Getter |
| | | @Setter |
| | | @ToString |
| | | @ApiModel(description = "è¿åä¿¡æ¯") |
| | | @NoArgsConstructor |
| | | public class R<T> implements Serializable { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | @ApiModelProperty(value = "ç¶æç ", required = true) |
| | | private int code; |
| | | @ApiModelProperty(value = "æ¯å¦æå", required = true) |
| | | private boolean success; |
| | | @ApiModelProperty(value = "æ¿è½½æ°æ®") |
| | | private T data; |
| | | @ApiModelProperty(value = "è¿åæ¶æ¯", required = true) |
| | | private String msg; |
| | | |
| | | private R(IResultCode resultCode) { |
| | | this(resultCode, null, resultCode.getMessage()); |
| | | } |
| | | |
| | | private R(IResultCode resultCode, String msg) { |
| | | this(resultCode, null, msg); |
| | | } |
| | | |
| | | private R(IResultCode resultCode, T data) { |
| | | this(resultCode, data, resultCode.getMessage()); |
| | | } |
| | | |
| | | private R(IResultCode resultCode, T data, String msg) { |
| | | this(resultCode.getCode(), data, msg); |
| | | } |
| | | |
| | | private R(int code, T data, String msg) { |
| | | this.code = code; |
| | | this.data = data; |
| | | this.msg = msg; |
| | | this.success = ResultCode.SUCCESS.code == code; |
| | | } |
| | | |
| | | /** |
| | | * 夿è¿åæ¯å¦ä¸ºæå |
| | | * |
| | | * @param result Result |
| | | * @return æ¯å¦æå |
| | | */ |
| | | public static boolean isSuccess(@Nullable R<?> result) { |
| | | return Optional.ofNullable(result) |
| | | .map(x -> ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.code, x.code)) |
| | | .orElse(Boolean.FALSE); |
| | | } |
| | | |
| | | /** |
| | | * 夿è¿åæ¯å¦ä¸ºæå |
| | | * |
| | | * @param result Result |
| | | * @return æ¯å¦æå |
| | | */ |
| | | public static boolean isNotSuccess(@Nullable R<?> result) { |
| | | return !R.isSuccess(result); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param data æ°æ® |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> data(T data) { |
| | | return data(data, BladeConstant.DEFAULT_SUCCESS_MESSAGE); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param data æ°æ® |
| | | * @param msg æ¶æ¯ |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> data(T data, String msg) { |
| | | return data(HttpServletResponse.SC_OK, data, msg); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param code ç¶æç |
| | | * @param data æ°æ® |
| | | * @param msg æ¶æ¯ |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> data(int code, T data, String msg) { |
| | | return new R<>(code, data, data == null ? BladeConstant.DEFAULT_NULL_MESSAGE : msg); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param msg æ¶æ¯ |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> success(String msg) { |
| | | return new R<>(ResultCode.SUCCESS, msg); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param resultCode ä¸å¡ä»£ç |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> success(IResultCode resultCode) { |
| | | return new R<>(resultCode); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param resultCode ä¸å¡ä»£ç |
| | | * @param msg æ¶æ¯ |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> success(IResultCode resultCode, String msg) { |
| | | return new R<>(resultCode, msg); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param msg æ¶æ¯ |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> fail(String msg) { |
| | | return new R<>(ResultCode.FAILURE, msg); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param code ç¶æç |
| | | * @param msg æ¶æ¯ |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> fail(int code, String msg) { |
| | | return new R<>(code, null, msg); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param resultCode ä¸å¡ä»£ç |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> fail(IResultCode resultCode) { |
| | | return new R<>(resultCode); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param resultCode ä¸å¡ä»£ç |
| | | * @param msg æ¶æ¯ |
| | | * @param <T> T æ³åæ è®° |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> fail(IResultCode resultCode, String msg) { |
| | | return new R<>(resultCode, msg); |
| | | } |
| | | |
| | | /** |
| | | * è¿åR |
| | | * |
| | | * @param flag æåç¶æ |
| | | * @return R |
| | | */ |
| | | public static <T> R<T> status(boolean flag) { |
| | | return flag ? success(BladeConstant.DEFAULT_SUCCESS_MESSAGE) : fail(BladeConstant.DEFAULT_FAILURE_MESSAGE); |
| | | } |
| | | |
| | | } |