ssjygl-xsct-service/ssjygl-xsx-common/src/main/java/com/cowr/common/view/Result.java

228 lines
6.9 KiB
Java
Raw Normal View History

2020-08-07 17:11:12 +08:00
package com.cowr.common.view;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Record;
import java.util.Map;
public class Result {
private static Log log = Log.getLog(Result.class);
public static final int SUCCESS = 200; // 成功
public static final int PARAM_ERROR = 400; // 失败、参数错误等
public static final int UNAUTHORIZED = 401; // 用户验证失败,或者用户验证信息过期
public static final int PERMISSION_DENIED = 403; // 没有权限
public static final int NOT_FOUND = 404; // 未找到资源
public static final int NOT_ALLOWED = 405; // 请求太频繁同一个用户token)、同一个url、同样的请求参数请求间隔小于0.5秒
public static final int SERVER_ERROR = 500; // 后台错误
public static final int SRC_TIMEOUT = 504; // 请求资源超时
public static final String PERMISSION_DENIED_STR = "没有权限";
2020-10-18 00:39:17 +08:00
public static final String NOT_FOUND_STR = "未找到资源";
public static final String SERVER_ERROR_STR = "后台错误";
public static final String SRC_TIMEOUT_STR = "请求第三方资源超时";
2020-08-07 17:11:12 +08:00
private int code;
private String msg; // 只用字符串,如果有其他类型,用 validerr 返回
private Object data;
private Map<String, String> validerr; // 表单验证失败返回消息
public Result() {
}
public Result(Map<String, String> validerr) {
2020-10-18 00:39:17 +08:00
this.code = PARAM_ERROR;
this.data = null;
this.msg = "表单验证失败";
2020-08-07 17:11:12 +08:00
this.validerr = validerr;
failedlog(this.msg, this.code);
}
public Result(int code, Object data, String msg) {
this.code = code;
this.data = data;
if (msg == null || "".equals(msg)) {
switch (code) {
case PERMISSION_DENIED:
this.msg = PERMISSION_DENIED_STR;
break;
case NOT_FOUND:
this.msg = NOT_FOUND_STR;
break;
case SERVER_ERROR:
this.msg = SERVER_ERROR_STR;
break;
case SRC_TIMEOUT:
this.msg = SRC_TIMEOUT_STR;
break;
}
} else {
this.msg = msg;
}
if (code != SUCCESS) {
failedlog(this.msg, this.code);
}
}
public Result(int code) {
this(code, null, null);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public Object getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
/**
* Record
* TODO
2020-10-18 00:39:17 +08:00
*
2020-08-07 17:11:12 +08:00
* @return Record
*/
2020-10-18 00:39:17 +08:00
@JSONField(serialize = false)
2020-08-07 17:11:12 +08:00
public Record getRecordData() {
return data != null ? (Record) data : null;
}
public void setData(Object data) {
this.data = data;
}
public Map<String, String> getValiderr() {
return validerr;
}
public void setValiderr(Map<String, String> validerr) {
this.validerr = validerr;
}
public String toJSONString() {
return JSONObject.toJSONString(this);
}
public static Result success(Object data) {
return new Result(Result.SUCCESS, data, null);
}
/**
* data == null msg data != null msg
* Result.object
* code 200, Result.object code 400
*
* @param data
* @param msg
* @return
*/
public static Result success(Object data, String msg) {
return new Result(Result.SUCCESS, data, msg);
}
public static Result success() {
return new Result(Result.SUCCESS);
}
public static Result failed(String msg) {
return new Result(Result.PARAM_ERROR, null, msg);
}
public static Result failedstr(String format, Object... objs) {
return new Result(Result.PARAM_ERROR, null, String.format(format, objs));
}
2020-10-18 00:39:17 +08:00
public static Result failed(Map<String, String> validerr) {
2020-08-07 17:11:12 +08:00
return new Result(validerr);
}
public static Result failed(Object ret, String msg) {
return new Result(Result.PARAM_ERROR, ret, msg);
}
public static Result noauth() {
return new Result(Result.UNAUTHORIZED, null, "没有登录,或者登录超时");
}
public static Result notAllowed() {
return new Result(Result.NOT_ALLOWED, null, "请求太频繁");
}
public static Result error() {
return new Result(Result.SERVER_ERROR);
}
public static Result permissionDenied() {
return permissionDenied("没有权限");
}
public static Result permissionDenied(String msg) {
return new Result(Result.PERMISSION_DENIED, null, msg);
}
public static Result object(Object data) {
return Result.object(data, "");
}
/**
*
*
* @param data data != null
* @param msg data == null msg
* @return result
*/
public static Result object(Object data, String msg) {
if (data instanceof Result) {
return (Result) data;
} else if (data instanceof Boolean) {
int code = (Boolean) data ? Result.SUCCESS : Result.PARAM_ERROR;
return new Result(code, data, msg);
} else {
if (data != null) {
return new Result(Result.SUCCESS, data, null);
}
return new Result(Result.PARAM_ERROR, null, msg);
}
}
/**
* SUCCESS debug log
*
* @param msg
*/
private static void failedlog(String msg, int code) {
if (code == NOT_FOUND) { // 404 的就不输出了
return;
}
log.debug(msg);
StackTraceElement[] mStacks = Thread.currentThread().getStackTrace();
for (StackTraceElement s : mStacks) {
if (s.getClassName().contains("com.cowr")
&& !s.getClassName().contains("com.cowr.common.view.Result")
&& !s.getClassName().contains("com.cowr.common.handler.GlobalHandler")
&& !s.getClassName().contains("com.cowr.common.Interceptor")
2020-08-25 16:39:53 +08:00
&& !s.getClassName().contains("com.cowr.local.ssjygl.main.AuthInterceptor")
&& !s.getClassName().contains("com.cowr.service.ssjygl.main.AuthInterceptor")
2020-08-07 17:11:12 +08:00
)
log.debug("-> " + s);
}
}
}