package com.yc.exception;
|
|
import com.alibaba.fastjson.JSON;
|
import com.yc.action.grid.GridUtils;
|
import lombok.Data;
|
import lombok.NoArgsConstructor;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 封装json格式返回到前端的消息
|
* {state:0
|
* msg:{
|
* success:'成功信息'
|
* }
|
* }
|
* {state:-1
|
* msg:{
|
* error:'失败信息'
|
* }
|
* }
|
*/
|
@Data
|
@NoArgsConstructor
|
public class CallBackMessage {
|
int state;//0表示成功,-1表示失败
|
String success;
|
String error;
|
String data;//
|
Object info;//输出复杂格式数据
|
String msg;//用于接收信息后再转化回来给error,success
|
String redirect; //用于页面重定向
|
String memo; //22类型输出参数
|
String linkdocinfo; //22类型输出参数
|
|
public CallBackMessage(int state, String success, String error) {
|
this.state = 0;
|
this.success = success;
|
this.error = error;
|
}
|
|
public CallBackMessage setData(String data) {
|
this.data = data;
|
return this;
|
}
|
|
public CallBackMessage setRedirect(String redirect) {
|
this.redirect = redirect;
|
return this;
|
}
|
|
public CallBackMessage setMemo(String memo) {
|
this.memo = memo;
|
return this;
|
}
|
|
public CallBackMessage setLinkdocinfo(String linkdocinfo) {
|
this.linkdocinfo = linkdocinfo;
|
return this;
|
}
|
|
public CallBackMessage setInfo(Object data) {
|
this.info = data;
|
return this;
|
}
|
|
public String sendSuccessMessage(String success) {
|
this.state = 0;
|
this.success = success;
|
return toString();
|
}
|
|
public String sendSuccessMessageByDefault() {
|
this.state = 0;
|
this.success = "成功";
|
return toString();
|
}
|
|
public String sendErrorMessage(String error) {
|
this.state = -1;
|
this.error = error;
|
return toString();
|
}
|
|
public String sendErrorMessage(String error, int errorCode) {
|
this.state = errorCode;
|
this.error = error;
|
return print();
|
}
|
|
@Override
|
public String toString() {
|
Map<String, Object> map = new HashMap<>(4);
|
map.put("state", this.state);
|
if (this.state == 0) {
|
map.put("msg", this.success);
|
} else {
|
map.put("msg", this.error);
|
}
|
if (data != null) {
|
map.put("data", data);
|
}
|
if (redirect != null) {
|
map.put("redirect", redirect);
|
}
|
if (info != null) {
|
map.put("info", this.info);
|
}
|
return GridUtils.toJson(map);
|
}
|
|
public Object toJSONObject() {
|
Map<String, Object> map = new HashMap<>(4);
|
map.put("state", this.state);
|
if (this.state == 0) {
|
map.put("msg", this.success);
|
} else {
|
map.put("msg", this.error);
|
}
|
if (data != null) {
|
map.put("data", data);
|
}
|
|
if (info != null) {
|
if (info instanceof Map) {
|
Map tMap = ((Map) info);
|
if (!tMap.containsKey("redirect") && redirect != null) {
|
tMap.put("redirect", redirect);
|
}
|
if (!tMap.containsKey("memo") && memo != null) {
|
tMap.put("memo", memo);
|
}
|
if (!tMap.containsKey("linkdocinfo") && linkdocinfo != null) {
|
tMap.put("linkdocinfo", linkdocinfo);
|
}
|
}
|
map.put("data", info);
|
} else {
|
Map tMap = new HashMap();
|
if (!tMap.containsKey("redirect") && redirect != null) {
|
tMap.put("redirect", redirect);
|
}
|
if (!tMap.containsKey("memo") && memo != null) {
|
tMap.put("memo", memo);
|
}
|
if (!tMap.containsKey("linkdocinfo") && linkdocinfo != null) {
|
tMap.put("linkdocinfo", linkdocinfo);
|
}
|
if (tMap.size() > 0) {
|
map.put("data", tMap);
|
}
|
}
|
return map;
|
}
|
|
/**
|
* 输出成功返回的内容,生成json字符串,用于返回给前端
|
*
|
* @return
|
*/
|
public String toJSONStringByMemo(String memo) {
|
this.setMemo(memo).sendSuccessMessageByDefault();//表示成功
|
return JSON.toJSONString(this.toJSONObject());
|
}
|
|
/**
|
* 输出成功且有跳转参数的内容,生成json字符串,用于返回给前端
|
*
|
* @return
|
*/
|
public String toJSONStringByLinkDocInfo(String linkdocinfo) {
|
this.setLinkdocinfo(linkdocinfo).sendSuccessMessageByDefault();//表示成功
|
return JSON.toJSONString(this.toJSONObject());
|
}
|
|
/**
|
* 输出成功且有提示内容和跳转参数的内容,生成json字符串,用于返回给前端
|
*
|
* @return
|
*/
|
public String toJSONStringByLinkDocInfoAndMemo(String linkdocinfo, String memo) {
|
this.setLinkdocinfo(linkdocinfo).setMemo(memo).sendSuccessMessageByDefault();//表示成功
|
return JSON.toJSONString(this.toJSONObject());
|
}
|
|
public String print() {
|
Map<String, Object> map = new HashMap<>(4);
|
map.put("state", this.state);
|
if (this.state == 0) {
|
map.put("success", this.success);
|
} else {
|
map.put("error", this.error);
|
}
|
if (data != null) {
|
map.put("data", data);
|
}
|
if (redirect != null) {
|
map.put("redirect", redirect);
|
}
|
return GridUtils.toJson(map);
|
}
|
|
public static void main(String[] args) {
|
CallBackMessage callBackMessage = new CallBackMessage(1, "aaa", "bbbb");
|
System.out.println(callBackMessage.toString());
|
}
|
}
|