返回信息设置代码

master
王皓雯 6 years ago
parent 599c7d9360
commit 45f22fc9fd

@ -0,0 +1,145 @@
package com.macro.mall.dto;
import com.github.pagehelper.PageInfo;
import com.macro.mall.util.JsonUtil;
import org.springframework.validation.BindingResult;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*/
public class CommonResult {
//操作成功
public static final int SUCCESS = 200;
//操作失败
public static final int FAILED = 500;
//参数校验失败
public static final int VALIDATE_FAILED = 404;
//未认证
public static final int UNAUTHORIZED = 401;
//未授权
public static final int FORBIDDEN = 403;
private int code;
private String message;
private Object data;
/**
*
*
* @param data
*/
public CommonResult success(Object data) {
this.code = SUCCESS;
this.message = "操作成功";
this.data = data;
return this;
}
/**
*
*/
public CommonResult pageSuccess(List data) {
PageInfo pageInfo = new PageInfo(data);//分页数据
Map<String, Object> result = new HashMap<>();
result.put("pageSize", pageInfo.getPageSize());
result.put("totalPage", pageInfo.getPages());
result.put("total", pageInfo.getTotal());
result.put("pageNum", pageInfo.getPageNum());
result.put("list", pageInfo.getList());
this.code = SUCCESS;
this.message = "操作成功";
this.data = result;
return this;
}
/**
*
*/
public CommonResult failed() {
this.code = FAILED;
this.message = "操作失败";
return this;
}
/**
* 使
*
* @param message
*/
public CommonResult validateFailed(String message) {
this.code = VALIDATE_FAILED;
this.message = message;
return this;
}
/**
* 使
*
* @param message
*/
public CommonResult unauthorized(String message) {
this.code = UNAUTHORIZED;
this.message = "暂未登录或token已经过期";//证书过期
this.data = message;
return this;
}
/**
* 使
*
* @param message
*/
public CommonResult forbidden(String message) {
this.code = FORBIDDEN;
this.message = "没有相关权限";
this.data = message;
return this;
}
/**
* 使
* @param result
*/
public CommonResult validateFailed(BindingResult result) {
validateFailed(result.getFieldError().getDefaultMessage());
return this;
}
@Override
public String toString() {
return JsonUtil.objectToJson(this);
}
public int getCode() {
/*获取验证码*/
return code;
}
public void setCode(int code) {
/*编辑验证码*/
this.code = code;
}
public String getMessage() {
/*获取信息*/
return message;
}
public void setMessage(String message) {
/*编辑信息*/
this.message = message;
}
public Object getData() {
/*获取数据*/
return data;
}
public void setData(Object data) {
/*编辑数据*/
this.data = data;
}
}
Loading…
Cancel
Save