package com.ruoyi.platform.weixin.service;
import com.ruoyi.platform.weixin.domain.Article;
import com.ruoyi.platform.weixin.domain.Image;
import com.ruoyi.platform.weixin.domain.Music;
import com.ruoyi.platform.weixin.domain.Video;
import com.ruoyi.platform.weixin.domain.Voice;
import com.ruoyi.platform.weixin.domainResponse.ArticleMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.ImageMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.MusicMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.TextMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.VideoMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.VoiceMessageResponse;
import com.ruoyi.platform.weixin.wxUtil.MessageUtil;
import com.ruoyi.platform.weixin.wxUtil.WxpubOAuth;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class MessageAndEventService {
@Autowired
private ISysConfigService sysConfigService;
private static final Logger logger = LoggerFactory.getLogger(MessageAndEventService.class);
public String processRequest(HttpServletRequest request) {
System.out.println(">>>>>>>>>>>>>>CoreService开始处理请求>>>>>>>>>>");
String respXml = null;
try {
Map<String, String> requestMap = MessageUtil.parseXml(request);
respXml = this.setMessageResponse(requestMap);
logger.info("处理结束,返回参数为: {}", respXml);
} catch (Exception e) {
e.printStackTrace();
}
return respXml;
}
public String setMessageResponse(Map<String, String> requestMap) {
String respXml = null;
String appId = sysConfigService.selectConfigByKey("APPID");
String redirectUri1 = sysConfigService.selectConfigByKey("redirectUri");
Map<String, String> responseMap = new HashMap<String, String>();
String msgType = requestMap.get("MsgType");
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
String eventType = requestMap.get("Event");
if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
String urlForCode = WxpubOAuth.createOauthUrlForCode(appId, redirectUri1, false);
String respContent = "感谢您关注中旭昊!\r\n1.回复数字2可获取智慧消防平台APP链接下载地址" +
"\r\n2.请点击<a href=\"" + urlForCode + "\">这里</a>进行授权登录\r\n注意:重复认证会覆盖掉之前的认证信息!";
responseMap.put("content", respContent);
respXml = this.respText(requestMap, responseMap);
} else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
} else if (eventType.equals(MessageUtil.EVENT_TYPE_SCAN)) {
} else if (eventType.equals(MessageUtil.EVENT_TYPE_LOCATION)) {
} else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
String key = requestMap.get("EventKey");
if (key.equals("product_001")) {
responseMap.put("content", "这是我们的消火栓!");
respXml = this.respText(requestMap, responseMap);
}
if (key.equals("product_002")) {
responseMap.put("content", "这是我们的烟感器!");
respXml = this.respText(requestMap, responseMap);
}
if (key.equals("product_003")) {
responseMap.put("content", "这是我们的消防水压!");
respXml = this.respText(requestMap, responseMap);
}
if (key.equals("product_004")) {
responseMap.put("content", "这是我们的消防水箱!");
respXml = this.respText(requestMap, responseMap);
}
if (key.equals("product_012")) {
responseMap.put("content", "这是我们的安全用电仪表设备!");
respXml = this.respText(requestMap, responseMap);
}
if (key.equals("authLogin")) {
System.out.println("==============这是授权登录点击事件!============");
String urlForCode = WxpubOAuth.createOauthUrlForCode(appId, redirectUri1, false);
String respContent = "请点击<a href=\"" + urlForCode + "\">这里</a>进行授权登录\r\n注意:重复认证会覆盖掉之前的认证信息!";
responseMap.put("content", respContent);
respXml = this.respText(requestMap, responseMap);
}
}
} else {
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
String content = requestMap.get("Content");
String text = "授权,登录,授权登录";
String text1 = "二维码,智慧消防二维码,2";
if (content.equals("1") || text.contains(content)) {
String urlForCode = WxpubOAuth.createOauthUrlForCode(appId, redirectUri1, false);
String respContent = "请点击<a href=\"" + urlForCode + "\">这里</a>进行授权登录\r\n注意:重复认证会覆盖掉之前的认证信息!";
responseMap.put("content", respContent);
respXml = this.respText(requestMap, responseMap);
} else if (text1.contains(content)) {
responseMap.put("title", "智慧消防APP");
responseMap.put("description", "根据自己需求扫描对应的二维码下载并安装APP");
responseMap.put("picUrl", "http://dd.zxhzn.com/img/logos.png");
responseMap.put("url", "http://45.96.779.110:8078/download/indoorFire/qrCode.html");
respXml = this.respNews(requestMap, responseMap);
} else {
responseMap.put("content", "感谢您关注中旭昊智能科技");
respXml = this.respText(requestMap, responseMap);
}
} else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
responseMap.put("content", "送您一个图片消息~~~~~~!");
respXml = this.respText(requestMap, responseMap);
} else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
responseMap.put("content", "送您一个语音消息~~~~~~!");
respXml = this.respText(requestMap, responseMap);
} else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)) {
responseMap.put("content", "送您一个视频消息~~~~~~!");
respXml = this.respText(requestMap, responseMap);
} else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_SHORTVIDEO)) {
responseMap.put("content", "送您一个小视频消息~~~~~~!");
respXml = this.respText(requestMap, responseMap);
} else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
responseMap.put("content", "送您一个地理位置消息~~~~~~!");
respXml = this.respText(requestMap, responseMap);
} else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
responseMap.put("content", "送您一个链接消息~~~~~~!");
respXml = this.respText(requestMap, responseMap);
}
}
return respXml;
}
private String respText(Map<String, String> requestMap, Map<String, String> responseMap) {
TextMessageResponse textMessageResponse = new TextMessageResponse();
textMessageResponse.setFromUserName(requestMap.get("ToUserName"));
textMessageResponse.setToUserName(requestMap.get("FromUserName"));
textMessageResponse.setCreateTime(new Date().getTime());
textMessageResponse.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
textMessageResponse.setContent(responseMap.get("content"));
String respXml = MessageUtil.messageToXml(textMessageResponse);
logger.info("处理结束,返回参数为: {}", respXml);
return respXml;
}
private String respImage(Map<String, String> requestMap, Map<String, String> responseMap) {
ImageMessageResponse imageMessageResponse = new ImageMessageResponse();
imageMessageResponse.setFromUserName(requestMap.get("ToUserName"));
imageMessageResponse.setToUserName(requestMap.get("FromUserName"));
imageMessageResponse.setCreateTime(new Date().getTime());
imageMessageResponse.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_IMAGE);
Image image = new Image();
image.setMediaId(responseMap.get("mediaId"));
imageMessageResponse.setImage(image);
String respXml = MessageUtil.messageToXml(imageMessageResponse);
logger.info("处理结束,返回参数为: {}", respXml);
return respXml;
}
private String respNews(Map<String, String> requestMap, Map<String, String> responseMap) {
ArticleMessageResponse articleMessageResponse = new ArticleMessageResponse();
articleMessageResponse.setFromUserName(requestMap.get("ToUserName"));
articleMessageResponse.setToUserName(requestMap.get("FromUserName"));
articleMessageResponse.setCreateTime(new Date().getTime());
articleMessageResponse.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_ARTICLE);
articleMessageResponse.setArticleCount(1);
List<Article> listArticles = new ArrayList<Article>();
Article article = new Article();
article.setTitle(responseMap.get("title"));
article.setDescription(responseMap.get("description"));
article.setPicUrl(responseMap.get("picUrl"));
article.setUrl(responseMap.get("url"));
listArticles.add(article);
articleMessageResponse.setArticles(listArticles);
String respXml = null;
try {
respXml = MessageUtil.messageToXml(articleMessageResponse);
logger.info("处理结束,返回参数为: {}", respXml);
} catch (Exception e) {
e.printStackTrace();
}
return respXml;
}
private String respVoice(Map<String, String> requestMap, Map<String, String> responseMap) {
VoiceMessageResponse voiceMessageResponse = new VoiceMessageResponse();
voiceMessageResponse.setFromUserName(requestMap.get("ToUserName"));
voiceMessageResponse.setToUserName(requestMap.get("FromUserName"));
voiceMessageResponse.setCreateTime(new Date().getTime());
voiceMessageResponse.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_VOICE);
Voice voice = new Voice();
voice.setMediaId(responseMap.get("mediaId"));
voiceMessageResponse.setVoice(voice);
String respXml = MessageUtil.messageToXml(voiceMessageResponse);
logger.info("处理结束,返回参数为: {}", respXml);
return respXml;
}
private String respVideo(Map<String, String> requestMap, Map<String, String> responseMap) {
VideoMessageResponse videoMessageResponse = new VideoMessageResponse();
videoMessageResponse.setFromUserName(requestMap.get("ToUserName"));
videoMessageResponse.setToUserName(requestMap.get("FromUserName"));
videoMessageResponse.setCreateTime(new Date().getTime());
videoMessageResponse.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_VIDEO);
Video video = new Video();
video.setMediaId(responseMap.get("mediaId"));
video.setTitle(requestMap.get("title"));
video.setDescription(requestMap.get("description"));
videoMessageResponse.setVideo(video);
String respXml = MessageUtil.messageToXml(videoMessageResponse);
logger.info("处理结束,返回参数为: {}", respXml);
return respXml;
}
private String respMusic(Map<String, String> requestMap, Map<String, String> responseMap) {
MusicMessageResponse musicMessageResponse = new MusicMessageResponse();
musicMessageResponse.setFromUserName(requestMap.get("ToUserName"));
musicMessageResponse.setToUserName(requestMap.get("FromUserName"));
musicMessageResponse.setCreateTime(new Date().getTime());
musicMessageResponse.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_MUSIC);
Music music = new Music();
music.setTitle(requestMap.get("title"));
music.setDescription(requestMap.get("description"));
music.setMusicURL(requestMap.get("musicURL"));
music.setHQMusicUrl(requestMap.get("hQMusicUrl"));
music.setThumbMediaId(requestMap.get("ThumbMediaId"));
musicMessageResponse.setMusic(music);
String respXml = MessageUtil.messageToXml(musicMessageResponse);
logger.info("处理结束,返回参数为: {}", respXml);
return respXml;
}
}
消息服务用到的xml封装工具类MessageUtil
package com.ruoyi.platform.weixin.wxUtil;
import com.ruoyi.platform.weixin.domain.Article;
import com.ruoyi.platform.weixin.domainResponse.ArticleMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.ImageMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.MusicMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.TextMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.VideoMessageResponse;
import com.ruoyi.platform.weixin.domainResponse.VoiceMessageResponse;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MessageUtil {
public static final String REQ_MESSAGE_TYPE_TEXT = "text";
public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
public static final String REQ_MESSAGE_TYPE_VIDEO = "video";
public static final String REQ_MESSAGE_TYPE_SHORTVIDEO = "shortvideo";
public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
public static final String REQ_MESSAGE_TYPE_LINK = "link";
public static final String REQ_MESSAGE_TYPE_EVENT = "event";
public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
public static final String EVENT_TYPE_SCAN = "SCAN";
public static final String EVENT_TYPE_LOCATION = "LOCATION";
public static final String EVENT_TYPE_CLICK = "CLICK";
public static final String RESP_MESSAGE_TYPE_TEXT = "text";
public static final String RESP_MESSAGE_TYPE_IMAGE = "image";
public static final String RESP_MESSAGE_TYPE_VOICE = "voice";
public static final String RESP_MESSAGE_TYPE_VIDEO = "video";
public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
public static final String RESP_MESSAGE_TYPE_ARTICLE = "news";
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
Map<String, String> map = new HashMap<>();
InputStream inputStream = request.getInputStream();
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
Element rootElement = document.getRootElement();
List<Element> elements = rootElement.elements();
System.out.println("==节点大小==" + elements.size());
for (Element e : elements) {
map.put(e.getName(), e.getText());
System.out.println("==键==" + e.getName() + "==值==" + e.getText());
}
inputStream.close();
inputStream = null;
return map;
}
@SuppressWarnings("unused")
private static XStream xStream = new XStream(new XppDriver() {
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
boolean cdate = true;
public void startNode(String name, Class clazz) {
super.startNode(name, clazz);
}
protected void writeText(QuickWriter writer, String text) {
if (cdate) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
public static String messageToXml(TextMessageResponse textMessageResponse) {
xStream.alias("xml", textMessageResponse.getClass());
return xStream.toXML(textMessageResponse);
}
public static String messageToXml(ImageMessageResponse imageMessageResponse) {
xStream.alias("xml", imageMessageResponse.getClass());
return xStream.toXML(imageMessageResponse);
}
public static String messageToXml(VoiceMessageResponse voiceMessageResponse) {
xStream.alias("xml", voiceMessageResponse.getClass());
return xStream.toXML(voiceMessageResponse);
}
public static String messageToXml(VideoMessageResponse videoMessageResponse) {
xStream.alias("xml", videoMessageResponse.getClass());
return xStream.toXML(videoMessageResponse);
}
public static String messageToXml(MusicMessageResponse musicMessageResponse) {
xStream.alias("xml", musicMessageResponse.getClass());
return xStream.toXML(musicMessageResponse);
}
public static String messageToXml(ArticleMessageResponse articleMessageResponse) {
xStream.alias("xml", articleMessageResponse.getClass());
xStream.alias("item", new Article().getClass());
return xStream.toXML(articleMessageResponse);
}
}