/**
*
* 处理微信消息
*
*/
@RequestMapping(value = "/verifyToken", method = RequestMethod.POST)
public void processMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 设置utf-8编码防止中文乱码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
// 发送给微信端的的xml数据
String respXmlMsg = null;
// xml请求解析
Map<String, String> requestMap;
try{
requestMap = MessageUtil.parseXml(request);// 解析xml对象
String fromUserName = requestMap.get("FromUserName");// 发送方帐号(open_id)
String toUserName = requestMap.get("ToUserName");// 公众帐号
String msgType = requestMap.get("MsgType");// 消息类型
String content = requestMap.get("Content");// text内容
String event = requestMap.get("Event");// event内容
String eventKey = requestMap.get("EventKey");
// 根据用户输入回复不同内容
if (msgType.equals("text")) {
if (content.equals("0")) {
ImageMessage imgmsg=new ImageMessage();
imgmsg.setToUserName(fromUserName);
imgmsg.setFromUserName(toUserName);
imgmsg.setCreateTime(new Date().getTime());
imgmsg.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
Image image = new Image();
image.setMediaId("u2VnxsayUJLJw2duyRBIgKU0yXdc7gG4o3JT8oJdhpk");
imgmsg.setImage(image);
respXmlMsg = MessageUtil.imageMessageToXml(imgmsg);
//WeixinUtil.formatXmlPicureAnswer(fromUserName, toUserName,"u2VnxsayUJLJw2duyRBIgKU0yXdc7gG4o3JT8oJdhpk");
} else if (content.equals("1")) {
NewsMessage newmsg=new NewsMessage();
newmsg.setToUserName(fromUserName);
newmsg.setFromUserName(toUserName);
newmsg.setCreateTime(new Date().getTime());
newmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
Article article=new Article();
article.setDescription("《我的前半生》这部剧最好的地方,就在于它呈现了中国人最常见的两种婚姻: 被男人抛弃的婚姻——比如罗子君被出轨,然后被男人扔出婚姻。 无法抛弃男人的婚姻——比如罗子群,困守婚姻围城,明明知道自己的男人渣,却无法走出婚姻的大门。"); //图文消息的描述
article.setPicUrl("http://mmbiz.qpic.cn/mmbiz_png/NYUq6ha4XQiardUE7lNAzk6WXbOLtZhDj9s8YatHACUWEuFibbDHn3KDic1aVicIJGtpHbHia2FjXn2GOA3HSZhAb8w/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1"); //图文消息图片地址
article.setTitle("《我的前半生》:如何走出“过不好”也“离不了”的中国式婚姻? | 懂心大咖免费课程"); //图文消息标题
article.setUrl("https://mp.weixin.qq.com/s?__biz=MzI5MTQ1Nzg5OA==&tempkey=OTE2Xy9vdDFLLzVuNnZ4K0NhVUZ3Vzd4NU1Zb2RDd081dWV6TVhhbEhjZmVlZm93NHA3dERvMVpWalRaWjVncmwwVWtsZURndnNZU0dRQkJPTXBmc0cxaGRYbjJTLWk2Wl85cTBWaUVNNVZ1VmZTdTFoQnlGR2VhdmJpRndFNGdiY2owNm5maFVhZHp3MS1PWGp4Nm5ZZFpVYkJ2c2dUemlHRHFybUN1Wnd%2Bfg%3D%3D&chksm=6c1117895b669e9ff88dcff076b60ffc8bea510981a897228dea5cd53625812431e722d177f1#rd"); //图文url链接
List<Article> list=new ArrayList<Article>();
list.add(article); //这里发送的是单图文,如果需要发送多图文则在这里list中加入多个Article即可!
newmsg.setArticleCount(list.size());
newmsg.setArticles(list);
respXmlMsg = MessageUtil.newsMessageToXml(newmsg);
} else if (content.equals("99")) {
ImageMessage imgmsg=new ImageMessage();
imgmsg.setToUserName(fromUserName);
imgmsg.setFromUserName(toUserName);
imgmsg.setCreateTime(new Date().getTime());
imgmsg.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
Image image = new Image();
image.setMediaId("u2VnxsayUJLJw2duyRBIgGEDz5BCL6Q5Wln7KJXbgUE");
imgmsg.setImage(image);
respXmlMsg = MessageUtil.imageMessageToXml(imgmsg);
}
} else if (msgType.equals("event")) {
if (event.equals("subscribe")) {
// 订阅时 返回消息
TextMessage txtmsg=new TextMessage();
txtmsg.setToUserName(fromUserName);
txtmsg.setFromUserName(toUserName);
txtmsg.setCreateTime(new Date().getTime());
txtmsg.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
txtmsg.setContent("春风再美也比不过你的笑");
respXmlMsg = MessageUtil.textMessageToXml(txtmsg);
} else if (event.equals("CLICK")) {
if (eventKey.equals("22")) {
ImageMessage imgmsg=new ImageMessage();
imgmsg.setToUserName(fromUserName);
imgmsg.setFromUserName(toUserName);
imgmsg.setCreateTime(new Date().getTime());
imgmsg.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
Image image = new Image();
image.setMediaId("u2VnxsayUJLJw2duyRBIgNvbc77upHnrAlj5MtMQogE");
imgmsg.setImage(image);
respXmlMsg = MessageUtil.imageMessageToXml(imgmsg);
}else if(eventKey.equals("31")) {
ImageMessage imgmsg=new ImageMessage();
imgmsg.setToUserName(fromUserName);
imgmsg.setFromUserName(toUserName);
imgmsg.setCreateTime(new Date().getTime());
imgmsg.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
Image image = new Image();
image.setMediaId("u2VnxsayUJLJw2duyRBIgNYCQwRVs9n7Mj8BQCv34xg");
imgmsg.setImage(image);
respXmlMsg = MessageUtil.imageMessageToXml(imgmsg);
}
}
}else{
TextMessage txtmsg=new TextMessage();
txtmsg.setToUserName(fromUserName);
txtmsg.setFromUserName(toUserName);
txtmsg.setCreateTime(new Date().getTime());
txtmsg.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
txtmsg.setContent("还不支持这种消息哇。");
respXmlMsg = MessageUtil.textMessageToXml(txtmsg);
}
out = response.getWriter();
out.print(respXmlMsg);
}catch(Exception e){
e.printStackTrace();
}finally{
out.close();
out = null;
}
}