wwl преди 3 години
родител
ревизия
7f273806f5

+ 2 - 45
twzd-common/src/main/java/com/miaxis/common/utils/wx/MessageUtil.java

@@ -52,8 +52,7 @@ public class MessageUtil {
         returnMap.put("CreateTime", new Date().getTime()+"");
         returnMap.put("MsgType", "text");
         returnMap.put("Content", content);
-        String encryptMsg = mapToXml(returnMap);
-        return encryptMsg;
+        return XmlUtil.mapToXml(returnMap);
     }
 
 
@@ -81,49 +80,7 @@ public class MessageUtil {
         returnMap.put("Description", description);
         returnMap.put("PicUrl", picUrl);
         returnMap.put("Url", url);
-        return mapToXml(returnMap);
-    }
-
-
-    /**
-     * 将Map转换为XML格式的字符串
-     *
-     * @param data Map类型数据
-     * @return XML格式的字符串
-     * @throws Exception
-     */
-    public static String mapToXml(Map<String, String> data) throws Exception {
-
-        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
-        DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
-        org.w3c.dom.Document document = documentBuilder.newDocument();
-        org.w3c.dom.Element root = document.createElement("xml");
-        document.appendChild(root);
-        for (String key: data.keySet()) {
-            String value = data.get(key);
-            if (value == null) {
-                value = "";
-            }
-            value = value.trim();
-            org.w3c.dom.Element filed = document.createElement(key);
-            filed.appendChild(document.createTextNode(value));
-            root.appendChild(filed);
-        }
-        TransformerFactory tf = TransformerFactory.newInstance();
-        Transformer transformer = tf.newTransformer();
-        DOMSource source = new DOMSource(document);
-        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
-        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-        StringWriter writer = new StringWriter();
-        StreamResult result = new StreamResult(writer);
-        transformer.transform(source, result);
-        String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
-        try {
-            writer.close();
-        } catch (Exception ex) {
-
-        }
-        return output;
+        return XmlUtil.mapToXml(returnMap);
     }
 
 

+ 6 - 112
twzd-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

@@ -170,33 +170,16 @@ public class WxGzhServiceImpl implements IWxGzhService {
                 log.info("2.1...");
                 String content = jsonObjectData.getStr("Content");
                 if (content.startsWith("你好")){
-                    String message = initText(fromUserName, toUserName, "你好");
-                    return message;
+                    return MessageUtil.initText(fromUserName, toUserName, "你好啊");
                 }else if (content.startsWith("傻逼")){
-                    Map<String, String> returnMap = new HashMap<>();
-                    returnMap.put("ToUserName", fromUserName);
-                    returnMap.put("FromUserName", toUserName);
-                    returnMap.put("CreateTime", new Date().getTime()+"");
-                    returnMap.put("MsgType", "text");
-                    returnMap.put("Content", "傻逼");
-                    String encryptMsg = mapToXml(returnMap);
-                    return encryptMsg;
+                    return MessageUtil.initText(fromUserName, toUserName, "你才是煞笔");
                 }else {
                     return MessageUtil.initText(fromUserName, toUserName, "文本消息-默认回复信息");
                 }
 
             } else if (MessageUtil.MESSAGE_IMAGE.equals(msgType)) { // 图片消息
                 log.info("2.2...");
-//                return MessageUtil.initText(fromUserName, toUserName, "抱歉,暂时无法识别图片信息!");
-                Map<String, String> returnMap = new HashMap<>();
-                returnMap.put("ToUserName", fromUserName);
-                returnMap.put("FromUserName", toUserName);
-                returnMap.put("CreateTime", new Date().getTime()+"");
-                returnMap.put("MsgType", "text");
-                returnMap.put("Content", "已关注扫描二维码事件");
-                String encryptMsg = mapToXml(returnMap);
-                return encryptMsg;
-
+                return MessageUtil.initText(fromUserName, toUserName, "抱歉,暂时无法识别图片信息!");
             }else if (MessageUtil.MESSAGE_EVENT.equals(msgType)) { // 事件消息
                 log.info("3....");
                 // 区分事件推送
@@ -214,14 +197,14 @@ public class WxGzhServiceImpl implements IWxGzhService {
                     }else {
                         content = "关注事件";
                     }
-                    return initText(fromUserName, toUserName, content);
+                    return MessageUtil.initText(fromUserName, toUserName, content);
 
                 }  else if (MessageUtil.MESSAGE_UNSUBSCRIBE.equals(event)) { // 取消订阅事件
                     // todo 处理取消订阅事件
 
                 } else if (MessageUtil.MESSAGE_SCAN.equals(event)) { // 已关注扫描二维码事件
                     log.info("3.2...");
-                    return initText(fromUserName, toUserName, "已关注扫描二维码事件");
+                    return MessageUtil.initText(fromUserName, toUserName, "已关注扫描二维码事件");
                     
                 } else if (MessageUtil.MESSAGE_LOCATION.equals(event)) { // 上报地理位置事件
                     // todo 处理上报地理位置事件
@@ -229,7 +212,7 @@ public class WxGzhServiceImpl implements IWxGzhService {
                 } else if (MessageUtil.MESSAGE_CLICK.equals(event)) { // 点击菜单拉取消息时的事件推送事件
                     //判断事件KEY值,与自定义菜单接口中KEY值对应
                     if ("generateTicket".equals(jsonObjectData.get("EventKey"))){ //获取分销二维码
-                        return initNews(fromUserName,
+                        return MessageUtil.initNews(fromUserName,
                                 toUserName,
                                 "图文消息信息",
                                 "title",
@@ -250,95 +233,6 @@ public class WxGzhServiceImpl implements IWxGzhService {
     }
 
 
-    /**
-     * 回复文本消息
-     * @param toUserName
-     * @param fromUserName
-     * @param content
-     * @return
-     */
-    public static String initText(String fromUserName, String toUserName, String content) throws Exception{
-        // 返回消息时ToUserName的值与FromUserName的互换
-        Map<String, String> returnMap = new HashMap<>();
-        returnMap.put("ToUserName", fromUserName);
-        returnMap.put("FromUserName", toUserName);
-        returnMap.put("CreateTime", new Date().getTime()+"");
-        returnMap.put("MsgType", "text");
-        returnMap.put("Content", content);
-        return mapToXml(returnMap);
-    }
-
-
-    /**
-     * 回复图文消息
-     * @param toUserName 接收方帐号(收到的OpenID)
-     * @param fromUserName 开发者微信号
-     * @param articles 图文消息信息,注意,如果图文数超过限制,则将只发限制内的条数
-     * @param title 图文消息标题
-     * @param description 图文消息描述
-     * @param picUrl 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
-     * @param url 点击图文消息跳转链接
-     * @return
-     */
-    public static String initNews(String toUserName, String fromUserName, String articles, String title, String description, String picUrl, String url)  throws Exception{
-        // 返回消息时ToUserName的值与FromUserName的互换
-        Map<String, String> returnMap = new HashMap<>();
-        returnMap.put("ToUserName", fromUserName);
-        returnMap.put("FromUserName", toUserName);
-        returnMap.put("CreateTime", new Date().getTime()+"");
-        returnMap.put("MsgType", "news");
-        returnMap.put("ArticleCount", "1");
-        returnMap.put("Content", articles);
-        returnMap.put("Title", title);
-        returnMap.put("Description", description);
-        returnMap.put("PicUrl", picUrl);
-        returnMap.put("Url", url);
-        return mapToXml(returnMap);
-    }
-
-
-    /**
-     * 将Map转换为XML格式的字符串
-     *
-     * @param data Map类型数据
-     * @return XML格式的字符串
-     * @throws Exception
-     */
-    public static String mapToXml(Map<String, String> data) throws Exception {
-        log.info("----mapToXml-----"+data);
-        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
-        DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
-        org.w3c.dom.Document document = documentBuilder.newDocument();
-        org.w3c.dom.Element root = document.createElement("xml");
-        document.appendChild(root);
-        for (String key: data.keySet()) {
-            String value = data.get(key);
-            if (value == null) {
-                value = "";
-            }
-            value = value.trim();
-            org.w3c.dom.Element filed = document.createElement(key);
-            filed.appendChild(document.createTextNode(value));
-            root.appendChild(filed);
-        }
-        TransformerFactory tf = TransformerFactory.newInstance();
-        Transformer transformer = tf.newTransformer();
-        DOMSource source = new DOMSource(document);
-        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
-        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-        StringWriter writer = new StringWriter();
-        StreamResult result = new StreamResult(writer);
-        transformer.transform(source, result);
-        String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
-        try {
-            writer.close();
-        } catch (Exception ex) {
-
-        }
-        log.info("----mapToXml-return-----"+output);
-        return output;
-    }
-
 
 
 }