234 lines
7.1 KiB
Java
234 lines
7.1 KiB
Java
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 = "没有权限";
|
||
public static final String NOT_FOUND_STR = "未找到资源";
|
||
public static final String SERVER_ERROR_STR = "后台错误";
|
||
public static final String SRC_TIMEOUT_STR = "请求第三方资源超时";
|
||
|
||
private int code;
|
||
private String msg; // 只用字符串,如果有其他类型,用 validerr 返回
|
||
private Object data;
|
||
private Map<String, String> validerr; // 表单验证失败返回消息
|
||
|
||
public Result() {
|
||
}
|
||
|
||
public Result(Map<String, String> validerr) {
|
||
this.code = PARAM_ERROR;
|
||
this.data = null;
|
||
this.msg = "表单验证失败";
|
||
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 可能会有类型转换错误
|
||
*
|
||
* @return Record
|
||
*/
|
||
@JSONField(serialize = false)
|
||
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) {
|
||
try {
|
||
return new Result(Result.PARAM_ERROR, null, String.format(format, objs));
|
||
}catch (Exception e){
|
||
log.error(e.getMessage());
|
||
|
||
return new Result(Result.PARAM_ERROR, null, "请求失败");
|
||
}
|
||
}
|
||
|
||
public static Result failed(Map<String, String> validerr) {
|
||
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")
|
||
&& !s.getClassName().contains("com.cowr.local.ssjygl.main.AuthInterceptor")
|
||
&& !s.getClassName().contains("com.cowr.service.ssjygl.main.AuthInterceptor")
|
||
)
|
||
log.debug("-> " + s);
|
||
}
|
||
}
|
||
}
|