package com.yc.utils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.yc.action.grid.GtTranInfo; import com.yc.exception.ApplicationException; import com.yc.service.grid.GridData; import org.apache.commons.io.IOUtils; import org.jetbrains.annotations.NotNull; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import java.util.Arrays; import java.util.List; /** * 转换json2java * * @author 邓文峰 * 2010-2-24 **/ public class JOSNUtils { public static GTJson fromJosn(String json) { Gson gson = new Gson(); GTJson gt = gson.fromJson(json, new com.google.gson.reflect.TypeToken() { }.getType()); //----增加处理 gt.process(); return gt; } public static GtTranInfo fromJosn2(String json) { Gson gson = new Gson(); GtTranInfo gt = gson.fromJson(json, new com.google.gson.reflect.TypeToken() { }.getType()); //----增加处理 //gt.process(); return gt; } public static PageInfo fromJosnTree(String json) { Gson gson = new Gson(); return gson.fromJson(json, new com.google.gson.reflect.TypeToken() { }.getType()); } /** * 处理分页信息 **/ public static Page init(HttpServletRequest request) { GTJson json = JOSNUtils.fromJosn(request.getParameter("TGData"));// 取得gt-grid返回的信息 Page page = new Page(); page.setPageNum(json.getPageInfo().getPageNum()); page.setPageSize(json.getPageInfo().getPageSize()); page.setExpr(json.getExpr()); page.setSql(Array2String(json.getFieldsName())); return page; } public static String prossBase64(String exwhere) { try { if (exwhere == null) return ""; byte[] bytes = exwhere.getBytes("utf-8"); if (EncodeUtil.isArrayByteBase64(bytes)) { exwhere = EncodeUtil.base64Decode(exwhere);//base64解密所有请求where参数 if (exwhere.indexOf("%2B") != -1) { exwhere = exwhere.replaceAll("%2B", "+");//base64编码中包含+ / 等符号 所以要进行处理 } if (exwhere.indexOf("%2F") != -1) { exwhere = exwhere.replaceAll("%2F", "/"); } if (exwhere.indexOf("%3D") != -1) { exwhere = exwhere.replaceAll("%3D", "="); } return exwhere; } else { String st = ""; String orgwhere=exwhere; try { if (exwhere.indexOf("+") != -1) { exwhere = exwhere.replaceAll("\\+", "%2B");//包含+ / 等符号 所以要进行处理 } if (exwhere.indexOf("/") != -1) { exwhere = exwhere.replaceAll("\\/", "%2F"); } st = java.net.URLDecoder.decode(exwhere, "utf-8"); return st; } catch (Exception e) { return orgwhere; } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ""; } } /** * 处理分页信息 **/ public static Page initTree(HttpServletRequest request) { String tgData = null; if (request.getHeader("x-app-type") != null) {//手机端传过来 try { if ("ionic".equalsIgnoreCase(request.getHeader("x-app-type"))) tgData = IOUtils.toString(request.getInputStream(), "utf-8"); else tgData = prossBase64(request.getParameter("TGData")); } catch (IOException e) { e.printStackTrace(); throw new ApplicationException("转换数据出错"); } } else { tgData = prossBase64(request.getParameter("TGData")); } return getPage(request, tgData); } @NotNull public static Page getPage(HttpServletRequest request, String tgData) { PageInfo json = JOSNUtils.fromJosnTree(tgData);// 取得gt-grid返回的信息 Page page = new Page(); if (json != null) { int k = json.getBody() == null ? 0 : json.getBody().get(0).getPos(); page.setCp(json.getCps() == null ? "" : json.getCps().getCp()); page.setPageNum(k == 0 ? 1 : k + 1); page.setPos(k); page.setFirstIndex(request.getParameter("firstIndex")==null?0:Integer.parseInt(request.getParameter("firstIndex")+"")); page.setFilter("0".equalsIgnoreCase(json.getCutFilter() == null ? "0" : json.getCutFilter().getFilter()) ? "" : json.getCutFilter().getFilter()); page.setExpr(json.getExprs() == null ? null : json.getExprs().getExpr()); page.setCssexpr(json.getCssExprs() == null ? null : json.getCssExprs().getcssExpr()); page.setTipsexpr(json.getTipsExprs() == null ? null : json.getTipsExprs().getTipsexpr()); page.setOrderBy(getOrderBy(json)); page.setRowSpan(json.getRowspan() == null ? "" : json.getRowspan()); page.setExcelTitle(json.getExcelTitle() == null ? "" : json.getExcelTitle()); page.setFormName(json.getFormName() == null ? "" : json.getFormName()); String[] s = json.getFields() == null ? null : json.getFields().getFieldsName().split(","); page.setSql(Array2String(s)); } return page; } public static Page setPageInfoData(GridData json) { Page page = new Page(); int k =0;//json.getBody()==null?0:json.getBody().get(0).getPos(); page.setPageNum(k == 0 ? 1 : k + 1); page.setPos(k); page.setFilter(""); page.setExpr(json.expr); page.setCssexpr(json.cssexpr); page.setOrderBy(""); String[] s = json.field.split(","); page.setSql(Array2String(s)); return page; } private static String getOrderBy(PageInfo json) {//转换排序 String order = ""; if (json.getCfg().getSortCols().length() == 0) return ""; String[] cols = json.getCfg().getSortCols().split(","); cols = Arrays.stream(cols).filter(x -> !x.equals("_DefaultSort")).toArray(String[]::new); String s = json.getCfg().getSortTypes(); if ("".equalsIgnoreCase(s) || s == null) s = "0"; String[] types = s.split(","); for (int i = 0; i < cols.length; i++) { if (i > 0) order += ","; order += cols[i].replaceAll("_ycid_", "id") + ("1".equalsIgnoreCase(types[i]) ? " desc" : " asc"); } return order; } /** * 数组转字符串 **/ public static String Array2String(String[] s) { if (s == null) return ""; StringBuilder sb = new StringBuilder(); String temp = ""; if (s.length > 0) { for (String st : s) { if (st.indexOf("_expr") > -1 || st.equalsIgnoreCase("G") || st.equalsIgnoreCase("_DefaultSort")) continue; if (!st.equalsIgnoreCase(temp)) sb.append("[").append(st).append("],"); temp = st; } return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : " * ";//by 11-12-09 } return " * "; } /** * 处理删除对象 **/ @SuppressWarnings({"unchecked"}) public static List del(HttpServletRequest request) { Gson gson = new GsonBuilder().setDateFormat( "yyyy-MM-dd").disableHtmlEscaping().create(); GTJson json = gson.fromJson(request.getParameter("_gt_json"), new com.google.gson.reflect.TypeToken() { }.getType());// 取得gt-grid返回的信息 return json.getDeletedRecords(); } public static String toJosn(Page page, Type dataType) { StringBuffer sb = new StringBuffer(); sb.append(""); sb.append(JsonUtil.list2Xml(page)); sb.append(""); if (page.getIsCp() == 1) sb.append(";#^#;"); return sb.toString(); } public static String toJosn38(Page page) { StringBuffer sb = new StringBuffer(); sb.append(JsonUtil.list2Xml2(page)); return sb.toString(); } /** * 处理38,238类型图表功能所需数据 */ public static String toJosn38Graph(Page page) { StringBuffer sb = new StringBuffer(); sb.append(JsonUtil.list2json(page.getData())); return sb.toString(); } }