##### 前言:
事件處理是非常重要的,這一章講講常見的事件處理
1、關注/取消關注
2、菜單點擊
事件類型介紹:
- 在微信中有事件請求是消息請求中的一種。請求類型為:event
- 而event事件類型又分多種事件類型,具體分
- 關注:subscribe
- 取消關注:unsubscribe
- 自定義菜單點擊:CLICK
- 根據上面的類型分類可建對應的常量
~~~
/**
* 請求消息類型:事件
*/
public static final String REQ_MESSAGE_TYPE_EVENT = "event";
/**
* 事件類型:subscribe(關注)
*/
public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
/**
* 事件類型:unsubscribe(取消關注)
*/
public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
/**
* 事件類型:CLICK(自定義菜單點擊事件)
*/
public static final String EVENT_TYPE_CLICK = "CLICK";
~~~
再在CoreServiceImpl中處理對應事件類型,先上CoreServiceImpl的源碼
~~~
package com.ifp.weixin.biz.core.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import com.ifp.weixin.biz.core.CoreService;
import com.ifp.weixin.constant.Constant;
import com.ifp.weixin.entity.Message.resp.Article;
import com.ifp.weixin.entity.Message.resp.NewsMessage;
import com.ifp.weixin.entity.Message.resp.TextMessage;
import com.ifp.weixin.util.MessageUtil;
@Service("coreService")
public class CoreServiceImpl implements CoreService {
public static Logger log = Logger.getLogger(CoreServiceImpl.class);
@Override
public String processRequest(HttpServletRequest request) {
String respMessage = null;
try {
// xml請求解析
Map<String, String> requestMap = MessageUtil.parseXml(request);
// 發送方帳號(open_id)
String fromUserName = requestMap.get("FromUserName");
// 公眾帳號
String toUserName = requestMap.get("ToUserName");
// 消息類型
String msgType = requestMap.get("MsgType");
TextMessage textMessage = new TextMessage();
textMessage.setToUserName(fromUserName);
textMessage.setFromUserName(toUserName);
textMessage.setCreateTime(new Date().getTime());
textMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_TEXT);
textMessage.setFuncFlag(0);
String respContent = "";
// 文本消息
if (msgType.equals(Constant.REQ_MESSAGE_TYPE_TEXT)) {
// 接收用戶發送的文本消息內容
String content = requestMap.get("Content");
// 創建圖文消息
NewsMessage newsMessage = new NewsMessage();
newsMessage.setToUserName(fromUserName);
newsMessage.setFromUserName(toUserName);
newsMessage.setCreateTime(new Date().getTime());
newsMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_NEWS);
newsMessage.setFuncFlag(0);
List<Article> articleList = new ArrayList<Article>();
// 單圖文消息
if ("1".equals(content)) {
Article article = new Article();
article.setTitle("我是一條單圖文消息");
article.setDescription("我是描述信息,哈哈哈哈哈哈哈。。。");
article.setPicUrl("http://www.iteye.com/upload/logo/user/603624/2dc5ec35-073c-35e7-9b88-274d6b39d560.jpg");
article.setUrl("http://tuposky.iteye.com");
articleList.add(article);
// 設置圖文消息個數
newsMessage.setArticleCount(articleList.size());
// 設置圖文消息包含的圖文集合
newsMessage.setArticles(articleList);
// 將圖文消息對象轉換成xml字符串
respMessage = MessageUtil.newsMessageToXml(newsMessage);
}
// 多圖文消息
else if ("3".equals(content)) {
Article article1 = new Article();
article1.setTitle("我是一條多圖文消息");
article1.setDescription("");
article1.setPicUrl("http://www.isic.cn/viewResourcesAction//logo/20130913/2013091314543416032.jpg");
article1.setUrl("http://tuposky.iteye.com/blog/2008583");
Article article2 = new Article();
article2.setTitle("微信公眾平臺開發教程Java版(二)接口配置 ");
article2.setDescription("");
article2.setPicUrl("http://www.isic.cn/viewResourcesAction//logo/20131021/2013102111243367254.jpg");
article2.setUrl("http://tuposky.iteye.com/blog/2008655");
Article article3 = new Article();
article3.setTitle("微信公眾平臺開發教程Java版(三) 消息接收和發送");
article3.setDescription("");
article3.setPicUrl("http://www.isic.cn/viewResourcesAction//logo/20131021/2013102111291287031.jpg");
article3.setUrl("http://tuposky.iteye.com/blog/2017429");
articleList.add(article1);
articleList.add(article2);
articleList.add(article3);
newsMessage.setArticleCount(articleList.size());
newsMessage.setArticles(articleList);
respMessage = MessageUtil.newsMessageToXml(newsMessage);
}
//事件處理開始
} else if (msgType.equals(Constant.REQ_MESSAGE_TYPE_EVENT)) {
// 事件類型
String eventType = requestMap.get("Event");
if (eventType.equals(Constant.EVENT_TYPE_SUBSCRIBE)) {
// 關注
respContent = "感謝您關注偶,這里會給您提供最新的公司資訊和公告!\n";
StringBuffer contentMsg = new StringBuffer();
contentMsg.append("您還可以回復下列數字,體驗相應服務").append("\n\n");
contentMsg.append("1 我就是個測試的").append("\n");
contentMsg.append("2 我木有").append("\n");
contentMsg.append("3 我是多圖文").append("\n");
respContent = respContent+contentMsg.toString();
} else if (eventType.equals(Constant.EVENT_TYPE_UNSUBSCRIBE)) {
// 取消關注,用戶接受不到我們發送的消息了,可以在這里記錄用戶取消關注的日志信息
} else if (eventType.equals(Constant.EVENT_TYPE_CLICK)) {
// 事件KEY值,與創建自定義菜單時指定的KEY值對應
String eventKey = requestMap.get("EventKey");
// 自定義菜單點擊事件
if (eventKey.equals("11")) {
respContent = "天氣預報菜單項被點擊!";
} else if (eventKey.equals("12")) {
respContent = "公交查詢菜單項被點擊!";
}
}
textMessage.setContent(respContent);
respMessage = MessageUtil.textMessageToXml(textMessage);
}
} catch (Exception e) {
e.printStackTrace();
}
return respMessage;
}
}
~~~
從第108行開始做事件處理,根據請求的事件類型,做出相應的反應。
菜單點擊事件:
單獨把菜單點擊事件拉出來詳細說說
用戶點擊自定義菜單后,如果菜單按鈕設置為click類型,則微信會把此次點擊事件推送給開發者,注意view類型(跳轉到URL)的菜單點擊不會上報。
參數說明:

要注意的是 EventKey 這個參數,與菜單創建的時候中的key值是對應的。
寫出來的效果圖為:依次觸發的事件是
關注,點擊天氣預報菜單,點擊公交查詢菜單

事件處理講到這里就結束了,
大家可加我的微信公眾號一起討論
微信公眾號:andedaohang
或掃描二維碼

轉載請注明出處:[http://blog.csdn.net/tuposky/article/details/40589325](http://blog.csdn.net/tuposky/article/details/40589325)
[](http://dl2.iteye.com/upload/attachment/0093/9940/39242cdb-225a-3a79-9a58-93dae3bb603e.jpg)
大小: 38.1 KB
[](http://dl2.iteye.com/upload/attachment/0093/9961/1b2fa9b8-99fe-3bc4-b037-673defacf609.png)
大小: 127.3 KB
[查看圖片附件](#)