|
@@ -21,8 +21,18 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.xml.parsers.DocumentBuilder;
|
|
|
+import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
+import javax.xml.transform.OutputKeys;
|
|
|
+import javax.xml.transform.Transformer;
|
|
|
+import javax.xml.transform.TransformerFactory;
|
|
|
+import javax.xml.transform.dom.DOMSource;
|
|
|
+import javax.xml.transform.stream.StreamResult;
|
|
|
+import java.io.StringWriter;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
@@ -169,7 +179,16 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
|
|
|
|
} else if (MessageUtil.MESSAGE_IMAGE.equals(msgType)) { // 图片消息
|
|
|
log.info("2.2...");
|
|
|
- return MessageUtil.initText(fromUserName, toUserName, "抱歉,暂时无法识别图片信息!");
|
|
|
+// 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;
|
|
|
+
|
|
|
}else if (MessageUtil.MESSAGE_EVENT.equals(msgType)) { // 事件消息
|
|
|
log.info("3....");
|
|
|
// 区分事件推送
|
|
@@ -223,4 +242,55 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<xml>\n<Content>测试哈哈哈哈</Content>\n<CreateTime>1635140564536</CreateTime>\n<ToUserName>ovKTX5wEWRRJMWDeLimRpbu9QLy0</ToUserName>\n<FromUserName>gh_628da25a95e5</FromUserName>\n<MsgType>text</MsgType>\n</xml>\n"
|
|
|
+ *
|
|
|
+ * "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<xml>\n<Content>抱歉,暂时无法识别图片信息!</Content>\n<CreateTime>1635233457889</CreateTime>\n<ToUserName>gh_628da25a95e5</ToUserName>\n<FromUserName>ovKTX528FTUWEc-0qNDen5_RgGCQ</FromUserName>\n<MsgType>text</MsgType>\n</xml>\n"
|
|
|
+ */
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|