Ret 类优化

v1.4.1
Parker 4 years ago
parent 835f299ff6
commit a3dff6ff88

@ -1,18 +1,3 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.api.base.result;
import com.alibaba.fastjson.JSONObject;
@ -22,6 +7,7 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;
import java.io.Serializable;
/**
@ -42,6 +28,11 @@ public class ResultVo<T> implements Serializable {
private static final long serialVersionUID = 1L;
/** 默认成功信息 */
private static final String DEF_SUCCESS_MSG = "操作成功!";
/** 默认失败信息 */
private static final String DEF_ERROR_MSG = "操作失败!";
/** 成功状态 */
@ApiModelProperty(value = "成功状态")
@JsonProperty("success")
@ -92,7 +83,7 @@ public class ResultVo<T> implements Serializable {
public ResultVo() {
// 初始化值
this.success = true;
this.msg = "操作成功!";
this.msg = DEF_SUCCESS_MSG;
this.code = HttpStatus.OK.value();
this.timestamp = System.currentTimeMillis();
}
@ -103,7 +94,7 @@ public class ResultVo<T> implements Serializable {
*
* @return ResultVo<Object>
*/
@JsonIgnore//返回对象时忽略此属性
@JsonIgnore
public static ResultVo<Object> success() {
return new ResultVo<>();
}
@ -113,7 +104,7 @@ public class ResultVo<T> implements Serializable {
* @param msg
* @return ResultVo<Object>
*/
@JsonIgnore//返回对象时忽略此属性
@JsonIgnore
public static ResultVo<Object> success(String msg) {
ResultVo<Object> ret = new ResultVo<>();
ret.setMsg(msg);
@ -126,7 +117,7 @@ public class ResultVo<T> implements Serializable {
* @param <T>
* @return ResultVo<T>
*/
@JsonIgnore//返回对象时忽略此属性
@JsonIgnore
public static <T> ResultVo<T> success(T data) {
ResultVo<T> ret = new ResultVo<>();
ret.setData(data);
@ -140,41 +131,63 @@ public class ResultVo<T> implements Serializable {
* @param <T>
* @return ResultVo<T>
*/
@JsonIgnore//返回对象时忽略此属性
@JsonIgnore
public static <T> ResultVo<T> success(String msg, T data) {
ResultVo<T> ret = new ResultVo<>();
ret.setMsg(msg);
ret.setData(data);
return ret;
}
/**
*
*
* @param msg
* @return ResultVo<Object>
* @param data
* @param <T>
* @return ResultVo<T>
*/
@JsonIgnore//返回对象时忽略此属性
public static <T> ResultVo<T> error(String msg) {
@JsonIgnore
public static <T> ResultVo<T> success(Integer code, String msg, T data) {
ResultVo<T> ret = new ResultVo<>();
ret.setCode(code);
ret.setMsg(msg);
ret.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
ret.setSuccess(false);
ret.setData(data);
return ret;
}
/**
*
* @return ResultVo<Object>
*/
@JsonIgnore
public static ResultVo<Object> error() {
return ResultVo.error(
HttpStatus.INTERNAL_SERVER_ERROR.value()
, DEF_ERROR_MSG, null);
}
/**
*
* @param msg
* @return ResultVo<Object>
*/
@JsonIgnore
public static ResultVo<Object> error(String msg) {
return ResultVo.error(
HttpStatus.INTERNAL_SERVER_ERROR.value()
, msg, null);
}
/**
*
* @param code
* @param msg
* @return ResultVo<T>
*/
@JsonIgnore//返回对象时忽略此属性
public static <T> ResultVo<T> error(int code, String msg) {
ResultVo<T> ret = new ResultVo<>();
ret.setMsg(msg);
ret.setCode(code);
ret.setSuccess(false);
return ret;
@JsonIgnore
public static ResultVo<Object> error(Integer code, String msg) {
return ResultVo.error(code, msg, null);
}
/**
@ -184,8 +197,8 @@ public class ResultVo<T> implements Serializable {
* @param <T>
* @return ResultVo<T>
*/
@JsonIgnore//返回对象时忽略此属性
public static <T> ResultVo<T> error(int code, String msg, T data) {
@JsonIgnore
public static <T> ResultVo<T> error(Integer code, String msg, T data) {
ResultVo<T> ret = new ResultVo<>();
ret.setMsg(msg);
ret.setCode(code);

@ -81,7 +81,7 @@ public class CustomShiroFilter extends AuthenticatingFilter {
httpResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
httpResponse.setContentType("application/json; charset=utf-8");
// 401 Token失效请重新登录
ResultVo<Object> error = ResultVo.error(TokenMsg.EXCEPTION_TOKEN_LOSE_EFFICACY.getCode(),
ResultVo<?> error = ResultVo.error(TokenMsg.EXCEPTION_TOKEN_LOSE_EFFICACY.getCode(),
TokenMsg.EXCEPTION_TOKEN_LOSE_EFFICACY.getMessage());
httpResponse.getWriter().print(error.toJsonStr());
return false;
@ -102,7 +102,7 @@ public class CustomShiroFilter extends AuthenticatingFilter {
try {
//处理登录失败的异常
Throwable throwable = e.getCause() == null ? e : e.getCause();
ResultVo<Object> error = ResultVo.error(TokenMsg.EXCEPTION_TOKEN_LOSE_EFFICACY.getCode(),
ResultVo<?> error = ResultVo.error(TokenMsg.EXCEPTION_TOKEN_LOSE_EFFICACY.getCode(),
throwable.getMessage());
httpResponse.getWriter().print(error.toJsonStr());
} catch (IOException ignored) {}

@ -103,7 +103,7 @@ public class UserTokenUtil {
public static ResultVo<Map<String,Object>> createToken(UserModel user) {
if (user == null) {
// 生成Token失败
return ResultVo.error(TokenMsg.EXCEPTION_TOKEN_CREATE_ERROR.getMessage());
throw new TokenException(TokenMsg.EXCEPTION_TOKEN_CREATE_ERROR);
}
Map<String,Object> map = Maps.newHashMapWithExpectedSize(2);
@ -136,14 +136,11 @@ public class UserTokenUtil {
map.put("expire", endTimestamp);
return ResultVo.success(map);
}
// 生成Token失败
return ResultVo.error(TokenMsg.EXCEPTION_TOKEN_CREATE_ERROR.getMessage());
}catch (Exception e){
log.error(e.getMessage() , e);
return ResultVo.error(e.getMessage());
}
// 生成Token失败
throw new TokenException(TokenMsg.EXCEPTION_TOKEN_CREATE_ERROR);
}
/**

@ -54,9 +54,13 @@ public enum CreaterMsg implements BaseMsg {
*
*/
EXCEPTION_CREATE_NULL(50140,"生成失败,数据为空"),
EXCEPTION_CREATE_TABLE_NULL(50140,"生成失败,暂无表数据"),
EXCEPTION_CREATE_FIELD_NULL(50121,"生成失败,暂无表字段"),
EXCEPTION_CREATE_TABLE_NULL(50141,"生成失败,暂无表数据"),
EXCEPTION_CREATE_FIELD_NULL(50142,"生成失败,暂无表字段"),
/**
*
*/
EXCEPTION_OTHER_NULL(50200,"暂无数据"),
;

@ -80,7 +80,8 @@ public class TableRestController extends BaseRestController<CreaterTable, Create
);
if(currModel == null){
return ResultVo.error("暂无数据");
// 暂无数据
throw new CreaterException(CreaterMsg.EXCEPTION_OTHER_NULL);
}
List<CreaterTableColumnModel> columnModelList = iTableColumnService.

@ -58,6 +58,7 @@ public enum SystemMsg implements BaseMsg {
EXCEPTION_USER_NO_UNIQUE(20306,"该工号已存在"),
EXCEPTION_ORG_NOT_NULL(20307,"组织不可为空"),
EXCEPTION_USER_ORG_ERROR(20308,"用户组织设置失败"),
EXCEPTION_USER_NULL(20309,"暂无该用户"),
/**

@ -38,6 +38,7 @@ import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.annotation.EnableLog;
import org.opsli.common.annotation.RequiresPermissionsCus;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.exception.TokenException;
import org.opsli.common.utils.HumpUtil;
import org.opsli.common.utils.WrapperUtil;
@ -50,6 +51,7 @@ import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.persistence.querybuilder.WebQueryBuilder;
import org.opsli.core.utils.OrgUtil;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.org.entity.SysOrg;
import org.opsli.modulars.system.org.service.ISysOrgService;
import org.opsli.modulars.system.org.web.SysOrgRestController;
@ -462,10 +464,11 @@ public class UserRestController extends BaseRestController<SysUser, UserModel, I
@Override
public ResultVo<UserModel> getUserByUsername(String username) {
UserModel userModel = IService.queryByUserName(username);
if(userModel != null){
return ResultVo.success(userModel);
if(userModel == null){
// 暂无该用户
throw new ServiceException(SystemMsg.EXCEPTION_USER_NULL);
}
return ResultVo.error("没有该用户");
return ResultVo.success(userModel);
}
/**

Loading…
Cancel
Save