/**
* Copyright (c) 2011-2014, James Zhan 詹波 (jfinal@126.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.jfinal.weixin.sdk.msg;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.weixin.iot.msg.InEquDataMsg;
import com.jfinal.weixin.iot.msg.InEqubindEvent;
import com.jfinal.weixin.sdk.msg.in.InImageMsg;
import com.jfinal.weixin.sdk.msg.in.InLinkMsg;
import com.jfinal.weixin.sdk.msg.in.InLocationMsg;
import com.jfinal.weixin.sdk.msg.in.InMsg;
import com.jfinal.weixin.sdk.msg.in.InNotDefinedMsg;
import com.jfinal.weixin.sdk.msg.in.InShortVideoMsg;
import com.jfinal.weixin.sdk.msg.in.InTextMsg;
import com.jfinal.weixin.sdk.msg.in.InVideoMsg;
import com.jfinal.weixin.sdk.msg.in.InVoiceMsg;
import com.jfinal.weixin.sdk.msg.in.card.InCardPassCheckEvent;
import com.jfinal.weixin.sdk.msg.in.card.InCardPayOrderEvent;
import com.jfinal.weixin.sdk.msg.in.card.InCardSkuRemindEvent;
import com.jfinal.weixin.sdk.msg.in.card.InMerChantOrderEvent;
import com.jfinal.weixin.sdk.msg.in.card.InUpdateMemberCardEvent;
import com.jfinal.weixin.sdk.msg.in.card.InUserCardEvent;
import com.jfinal.weixin.sdk.msg.in.card.InUserConsumeCardEvent;
import com.jfinal.weixin.sdk.msg.in.card.InUserGetCardEvent;
import com.jfinal.weixin.sdk.msg.in.card.InUserGiftingCardEvent;
import com.jfinal.weixin.sdk.msg.in.card.InUserPayFromCardEvent;
import com.jfinal.weixin.sdk.msg.in.event.InCustomEvent;
import com.jfinal.weixin.sdk.msg.in.event.InFollowEvent;
import com.jfinal.weixin.sdk.msg.in.event.InLocationEvent;
import com.jfinal.weixin.sdk.msg.in.event.InMassEvent;
import com.jfinal.weixin.sdk.msg.in.event.InMenuEvent;
import com.jfinal.weixin.sdk.msg.in.event.InNotDefinedEvent;
import com.jfinal.weixin.sdk.msg.in.event.InPoiCheckNotifyEvent;
import com.jfinal.weixin.sdk.msg.in.event.InQrCodeEvent;
import com.jfinal.weixin.sdk.msg.in.event.InShakearoundUserShakeEvent;
import com.jfinal.weixin.sdk.msg.in.event.InShakearoundUserShakeEvent.AroundBeacon;
import com.jfinal.weixin.sdk.msg.in.event.InTemplateMsgEvent;
import com.jfinal.weixin.sdk.msg.in.event.InVerifyFailEvent;
import com.jfinal.weixin.sdk.msg.in.event.InVerifySuccessEvent;
import com.jfinal.weixin.sdk.msg.in.event.InWifiEvent;
import com.jfinal.weixin.sdk.msg.in.event.ScanCodeInfo;
import com.jfinal.weixin.sdk.msg.in.speech_recognition.InSpeechRecognitionResults;
import com.jfinal.weixin.sdk.utils.XmlHelper;
public class InMsgParser {
private InMsgParser() {}
/**
* 从 xml 中解析出各类消息与事件
* @param xml xml字符串
* @return {InMsg}
*/
public static InMsg parse(String xml) {
XmlHelper xmlHelper = XmlHelper.of(xml);
return doParse(xmlHelper);
}
/**
* 消息类型
* 1:text 文本消息
* 2:image 图片消息
* 3:voice 语音消息
* 4:video 视频消息
* shortvideo 小视频消息
* 5:location 地址位置消息
* 6:link 链接消息
* 7:event 事件
*/
private static InMsg doParse(XmlHelper xmlHelper) {
String toUserName = xmlHelper.getString("//ToUserName");
String fromUserName = xmlHelper.getString("//FromUserName");
Integer createTime = xmlHelper.getNumber("//CreateTime").intValue();
String msgType = xmlHelper.getString("//MsgType");
if ("text".equals(msgType))
return parseInTextMsg(xmlHelper, toUserName, fromUserName, createTime, msgType);
if ("image".equals(msgType))
return parseInImageMsg(xmlHelper, toUserName, fromUserName, createTime, msgType);
if ("voice".equals(msgType))
return parseInVoiceMsgAndInSpeechRecognitionResults(xmlHelper, toUserName, fromUserName, createTime, msgType);
if ("video".equals(msgType))
return parseInVideoMsg(xmlHelper, toUserName, fromUserName, createTime, msgType);
if ("shortvideo".equals(msgType)) //支持小视频
return parseInShortVideoMsg(xmlHelper, toUserName, fromUserName, createTime, msgType);
if ("location".equals(msgType))
return parseInLocationMsg(xmlHelper, toUserName, fromUserName, createTime, msgType);
if ("link".equals(msgType))
return parseInLinkMsg(xmlHelper, toUserName, fromUserName, createTime, msgType);
if ("event".equals(msgType))
return parseInEvent(xmlHelper, toUserName, fromUserName, createTime, msgType);
if (InEqubindEvent.DEVICE_EVENT.equals(msgType)) //微信硬件 绑定和解绑事件
return parseInDeviceEvent(xmlHelper, toUserName, fromUserName, createTime, msgType);
if (InEquDataMsg.DEVICE_TEXT.equals(msgType)) //微信硬件 接收数据
return parseInDeviceData(xmlHelper, toUserName, fromUserName, createTime, msgType);
LogKit.error("无法识别的消息类型 " + msgType + ",请查阅微信公众平台开发文档");
return parseInNotDefinedMsg(xmlHelper, toUserName, fromUserName, createTime, msgType);
}
private static InMsg parseInNotDefinedMsg(XmlHelper xmlHelper, String toUserName, String fromUserName, Integer createTime, String msgType) {
InNotDefinedMsg msg = new InNotDefinedMsg(toUserName, fromUserName, createTime, msgType);
msg.setXmlHelper(xmlHelper);
return msg;
}
private static InMsg parseInTextMsg(XmlHelper xmlHelper, String toUserName, String fromUserName, Integer createTime, String msgType) {
InTextMsg msg = new InTextMsg(toUserName, fromUserName, createTime, msgType);
msg.setContent(xmlHelper.getString("//Content"));
msg.setMsgId(xmlHelper.getString("//MsgId"));
return msg;
}
private static InMsg parseInImageMsg(XmlHelper xmlHelper, String toUserName, String fromUserName, Integer createTime, String msgType) {
InImageMsg msg = new InImageMsg(toUserName, fromUserName, createTime, msgType);
msg.setPicUrl(xmlHelper.getString("//PicUrl"));
msg.setMediaId(xmlHelper.getString("//MediaId"));
msg.setMsgId(xmlHelper.getString("//MsgId"));
return msg;
}
private static InMsg parseInVoiceMsgAndInSpeechRecognitionResults(XmlHelper xmlHelper, String toUserName, String fromUserName, Integer createTime, String msgType) {
String recognition = xmlHelper.getString("//Recognition");
String mediaId = xmlHelper.getString("//MediaId");
String format = xmlHelper.getString("//Format");
String msgId = xmlHelper.getString("//MsgId");
if (StrKit.isBlank(recognition)) {
InVoiceMsg msg = new InVoiceMsg(toUserName, fromUserName, createTime, msgType);
msg.setMediaId(mediaId);
msg.setFormat(format);
msg.setMsgId(msgId);
return msg;
} else {
InSpeechRecognitionResults msg = new InSpeechRecognitionResults(toUserName, fromUserName, createTime, msgType);
msg.setMediaId(mediaId);
msg.setFormat(format);
msg.setMsgId(msgId);
// 与 InVoiceMsg 唯一的不同之处
msg.setRecognition(recognition);
return msg;
}
}
private static InMsg parseInVideoMsg(XmlHelper xmlHelper, String toUserName, String fromUserName, Integer createTime, String msgType) {
InVideoMsg msg = new InVideoMsg(toUserName, fromUserName, createTime, msgType);
msg.setMediaId(xmlHelper.getString("//MediaId"));
msg.setThumbMediaId(xmlHelper.getString("//ThumbMediaId"));
msg.setMsgId(xmlHelper.getString("//MsgId"));
return msg;
}
private static InMsg parseInShortVideoMsg(XmlHelper xmlHelper, String toUserName, String fromUserName, Integer createTime, String msgType) {
InShortVideoMsg msg = new InShortVideoMsg(toUserName, fromUserName, createTi
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JFinal Weixin 是基于 JFinal 的微信公众号极速开发 SDK (694个子文件)
stylesheet.css 13KB
.editorconfig 437B
.gitignore 436B
index-all.html 552KB
ApiResult.html 171KB
serialized-form.html 100KB
MsgControllerAdapter.html 79KB
MsgController.html 59KB
CustomServiceApi.html 56KB
DatacubeApi.html 53KB
PaymentApi.html 52KB
overview-tree.html 49KB
Poi.html 45KB
constant-values.html 45KB
InCardPayOrderEvent.html 39KB
ShakeAroundDeviceApi.html 39KB
CardApi.html 37KB
InMenuEvent.html 37KB
MediaApi.html 35KB
InUserGetCardEvent.html 35KB
InMsg.html 34KB
InUserConsumeCardEvent.html 33KB
WxaOrder.html 32KB
CardCodeApi.html 30KB
InMassEvent.html 29KB
ShakeAroundPageApi.html 29KB
DeviceApi.html 28KB
InUserGiftingCardEvent.html 28KB
ApiResult.html 28KB
InShakearoundUserShakeEvent.html 28KB
PoiApi.html 28KB
XmlHelper.html 28KB
InUserPayFromCardEvent.html 27KB
OutMsg.html 27KB
InUserCardEvent.html 26KB
MsgModel.html 26KB
InWifiEvent.html 26KB
DeviceAuth.html 26KB
InEquDataMsg.html 26KB
InEqubindEvent.html 25KB
WxaTemplate.html 25KB
allclasses-frame.html 25KB
InCardPassCheckEvent.html 25KB
InUpdateMemberCardEvent.html 25KB
InCustomEvent.html 25KB
InMerChantOrderEvent.html 25KB
InMsg.html 25KB
InTemplateMsgEvent.html 24KB
InPoiCheckNotifyEvent.html 24KB
OutNewsMsg.html 24KB
OutMusicMsg.html 24KB
InCardSkuRemindEvent.html 23KB
InVerifySuccessEvent.html 23KB
InQrCodeEvent.html 23KB
InLocationMsg.html 23KB
InVerifyFailEvent.html 23KB
WXBizMsgCrypt.html 23KB
PaymentKit.html 22KB
ApiConfig.html 22KB
TagApi.html 22KB
TemplateData.html 22KB
OutEquDataMsg.html 22KB
InLocationEvent.html 22KB
SnsAccessTokenApi.html 22KB
allclasses-noframe.html 22KB
InLinkMsg.html 21KB
MediaFile.html 21KB
EventInMsg.html 21KB
ApiConfigKit.html 21KB
OutVideoMsg.html 21KB
MediaArticles.html 21KB
TemplateData.TemplateItem.html 21KB
WxaTemplate.TemplateItem.html 20KB
InShortVideoMsg.html 20KB
HttpUtils.html 20KB
SnsAccessToken.html 20KB
InVideoMsg.html 20KB
XmlHelper.html 20KB
GroupsApi.html 20KB
BlackUserApi.html 20KB
InVoiceMsg.html 20KB
InNotDefinedEvent.html 20KB
CardPayApi.html 20KB
InImageMsg.html 20KB
ShakeAroundStatisticsApi.html 20KB
AesException.html 20KB
InFollowEvent.html 20KB
BlueLight.html 19KB
News.html 19KB
BlueLight.CmdId.html 19KB
JsTicket.html 19KB
MediaApi.MediaType.html 19KB
RedPackApi.html 19KB
package-summary.html 19KB
PaymentApi.TradeType.html 19KB
UserApi.html 19KB
MenuApi.html 19KB
InSpeechRecognitionResults.html 19KB
EventInMsg.html 18KB
WxaMsg.html 18KB
共 694 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
do622
- 粉丝: 5
- 资源: 48
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于C语言的操作系统实验项目.zip
- (源码)基于C++的分布式设备配置文件管理系统.zip
- (源码)基于ESP8266和Arduino的HomeMatic水表读数系统.zip
- (源码)基于Django和OpenCV的智能车视频处理系统.zip
- (源码)基于ESP8266的WebDAV服务器与3D打印机管理系统.zip
- (源码)基于Nio实现的Mycat 2.0数据库代理系统.zip
- (源码)基于Java的高校学生就业管理系统.zip
- (源码)基于Spring Boot框架的博客系统.zip
- (源码)基于Spring Boot框架的博客管理系统.zip
- (源码)基于ESP8266和Blynk的IR设备控制系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功