前段时间,由于一些原因突然让我去搞公众号二维码带参事件,随后跟着官方文档来走一切都很顺利直到要接收微信推送的事件哪里卡了一下由于也是第一次搞没啥经验被卡了一会不过还好捣鼓了一会也是把这个东东搞好了。今天就抽空总结下,好了废话就到此为止了。
在接收微信推送的消息或者事件前我们要先检查下微信公众号开发者平台的服务是否开启 配置是否正确,这里我就不插入图片了。还有获取带参二维码也很简单这个跟着文档说的接口调试即可,如果错误请检查ip是否在公众号后台把ip加入了白名单中。发送的参数是正确
关于带参二维码生成的文档地址在此微信开发文档
既然要交互我们先来看看微信推送过来的xml格式,下面这张图就是微信发给我们的关于文本消息的xml格式从图上可以得知参数格式
微信推送消息/事件的xml格式文档
既然格式那么多我们就首先写个类用来判断推送过来的是什么类型的事件消息,然后在做出相应的处理即可
public class MessageUtil {
public static final String MESSAGE_TEXT = "text";//文本消息
public static final String MESSAGE_NEWS = "news";//
public static final String MESSAGE_IMAGE = "image";//图片消息
public static final String MESSAGE_MUSIC = "music";//
public static final String MESSAGE_VOICE = "voice";//语音消息
public static final String MESSAGE_VIDEO = "video";//视频消息
public static final String MESSAGE_LINK = "link";//连接消息
public static final String MESSAGE_LOCATION = "location";//发送地理事件
public static final String MESSAGE_EVENT = "event";//关注实践
public static final String MESSAGE_SUBSCRIBE = "subscribe";//关注
public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";//取关
public static final String MESSAGE_CLICK = "CLICK";//自定义菜单界面
public static final String MESSAGE_VIEW = "VIEW";//点击菜单跳转链接时
public static final String MESSAGE_SCANCODE = "scancode_push";
}
从前面我们得知微信发给我们的事件消息与我们反馈给微信的信息都是通过xml来完成数据交互的,所以我们还需准备一个有关xml的工具类
public class MessageFormatXml {
/**
* xml 转 map
*/
public static Map<String, String> xmlToMap(HttpServletRequest request)
throws IOException, DocumentException {
Map<String, String> map = new HashMap<String, String>();
SAXReader reader = new SAXReader();
InputStream ins = request.getInputStream();
Document doc = reader.read(ins);
Element root = doc.getRootElement();
List<Element> list = root.elements();
for (Element e : list) {
map.put(e.getName(), e.getText());
}
ins.close();
return map;
}
/**
* 处理下要返回给微信的数据格式
*/
public static String initText(String toUserName, String fromUserName,
String content) {
TextMessage text = new TextMessage();
text.setFromUserName(toUserName);
text.setToUserName(fromUserName);
text.setMsgType(MessageUtil.MESSAGE_TEXT);
text.setCreateTime(new Date().getTime());
text.setContent(content);
return textMessageToXml(text);
}
}
接下来我们要开始准备我们处理消息的controller层的代码了,在这里我们要特别注意一定要与我们在公众号后台配置的url路径对上。一定一定一定要对上。
还有一定要把地址在公众号后台白名单里面添加上自己的ip
@ResponseBody
@RequestMapping(value = "/xxxx",method =RequestMethod.POST)
public String getMessage(HttpServletRequest request, HttpServletResponse response) throws Exception{
Map<String,String> map = new MessageFormat().xmlToMap(request);
String fromUserName = map.get("FromUserName");//这是正在你公众号操作的用户id
String toUserName = map.get("ToUserName");//发过来的公众号id
String msgType = map.get("MsgType");//发送的消息类型[比如 文字,图片,语音。。。]
String content = map.get("Content");//发送的消息内容
String message = null;
//判断发送的类型
switch (msgType){
case MessageUtil.MESSAGE_TEXT://文本消息
message = MessageFormat.initText(toUserName, fromUserName, "你好啊,欢迎你来到XXX号");
break;
case MessageUtil.MESSAGE_IMAGE://图片消息
//具体如何操作看自己需求来即可
break;
case MessageUtil.MESSAGE_EVENT://关注事件
String event=map.get("Event");
if (MessageUtil.MESSAGE_SUBSCRIBE.equals(event)){
message = MessageFormat.initText(toUserName, fromUserName, "你好啊,欢迎关注XXX!");
}
break;
}
return message;
}