/** * 客流计数UI交互工具类 * */ public class PassengerFlowCountingUIUtil { //返回状态码 public static String RETURN_CODE = "retCode"; //返回状态描述 public static String RETURN_DESC = "retDesc"; //返回数据 public static String RETURN_DATA = "retData"; //定义gson对像 public static Gson gson = new Gson(); /** * 成功返回结果 * * @return 返回结果对像 */ public static Map<String, Object> getSuccessResult() { Map<String, Object> result = new HashMap<String, Object>(); result.put(RETURN_CODE, UIStatusCode.RET_SUCESS.getCode()); result.put(RETURN_DESC, UIStatusCode.RET_SUCESS.getDesc()); return result; } /** * 返回失败结果 * * @param cause 失败原因 * @return 返回JSON对像 */ public static Map<String, Object> getFailedResult(String cause) { Map<String, Object> result = new HashMap<String, Object>(); result.put(RETURN_CODE, UIStatusCode.RET_FAILED.getCode()); if (cause != null && cause.length() > 0) { //返回指定失败原因 result.put(RETURN_DESC, cause); } else { //使用默认描述 result.put(RETURN_DESC, UIStatusCode.RET_FAILED.getDesc()); } return result; } /** * 添加属性 * * @param obj 对像 * @param key 关键字 * @param value 值 */ public static void addElement(Map<String, Object> obj, String key, String value) { obj.put(key, value); } /** * 添加属性 * * @param obj 对像 * @param key 关键字 * @param value 值 */ public static void addElement(Map<String, Object> obj, String key, Object value) { obj.put(key, value); } /** * 添加返回数据 * * @param obj 要添加数据的对像 * @param data 数据 */ public static void addReturnData(Map<String, Object> obj, Object data) { obj.put(RETURN_DATA, data); } /** * 对像转换成JSON字符串 * * @param obj 要转换的对像 * @return 转换后的JSON字符串 */ public static String toJSONString(Object obj) { return gson.toJson(obj); } /** * UI状态码 */ public static enum UIStatusCode { RET_SUCESS(1, "接口调用成功"), RET_FAILED(0, "接口调用失败"); UIStatusCode(int code, String desc) { this.code = code; this.desc = desc; } private int code;//状态码 private String desc; //描述 public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } } }
与前端交互设计工具类
最新推荐文章于 2024-09-05 19:07:41 发布