簡介
上一篇將瞭如何利用wechat-api設置自定義菜單,這一篇說下事件監聽的問題。
什麼是微信事件
在開發文檔中,消息管理–》接收消息-接收事件推送,事件消息分爲如下幾種:
1 關注/取消關注事件
2 掃描帶參數二維碼事件
3 上報地理位置事件
4 自定義菜單事件
5 點擊菜單拉取消息時的事件推送
6 點擊菜單跳轉鏈接時的事件推送
7.當然用戶發送一條消息給公衆號,公衆號也可以收到事件(這個文檔上沒有,難道這個不是事件麼(⊙﹏⊙)b)。
當我們設置服務器url後,這些事件都將通過post請求轉發到這個url上,記得是---post請求
監聽事件
這裏用到了node-wechat,再次膜拜大神們。
先看實現的監聽關注事件的代碼,這裏實現了當用戶關注後再服務器保存用戶openId的例子。
//設置
var config = {
token: '這裏是你在服務器設置時候的token',
appid: constant.WX_OPEN_APPID,
encodingAESKey: '這個也是設置的時候自動生成的'
};
/*
比如關注事件
{ ToUserName: 'gh_409828134b4a',
FromUserName: 'oZca1v6aUyyJyytYCUVcmQ8sPgUU',
CreateTime: '1467947631',
MsgType: 'event',
Event: 'subscribe',
EventKey: '' }*/
//公衆號消息和事件接收,這裏是post
exports.getWxRequest = wechat(config, function (req, res, next) {
var message = req.weixin;
if (message.MsgType === 'event') {
//關注事件,需要將用戶openId保存到用戶
if (message.Event === 'subscribe') {
_wxSubscribeEvent(message, res);
}
//用戶點擊自定義菜單事件
if (message.Event === 'CLICK') {
//_wxMenuEvent(message,res);
}
}
})
//關注
function _wxSubscribeEvent(message, res) {
var openId = message.FromUserName;
//獲取Unionid,我們平臺保存的是unionid,你可以使用openId
wxOpenApi.getUser(openId, function (error, data, result) {
if (!error) {
var unionid = data.unionid;
//這裏做你想做的事情嘍
};
}
原理:
先看下wechat的封裝吧,在wechat-》lib-》wechat.js 裏。
看關鍵代碼:
/**
* 根據口令
*
* Examples:
* 使用wechat作爲自動回覆中間件的三種方式
* ```
* wechat(token, function (req, res, next) {});
*
* wechat(token, wechat.text(function (message, req, res, next) {
* // TODO
* }).location(function (message, req, res, next) {
* // TODO
* }));
*
* wechat(token)
* .text(function (message, req, res, next) {
* // TODO
* }).location(function (message, req, res, next) {
* // TODO
* }).middlewarify();
* ```
* 加密模式下token爲config
*
* ```
* var config = {
* token: 'token',
* appid: 'appid',
* encodingAESKey: 'encodinAESKey'
* };
* wechat(config, function (req, res, next) {});
* ```
*
* 靜態方法
*
* - `text`,處理文字推送的回調函數,接受參數爲(text, req, res, next)。
* - `image`,處理圖片推送的回調函數,接受參數爲(image, req, res, next)。
* - `voice`,處理聲音推送的回調函數,接受參數爲(voice, req, res, next)。
* - `video`,處理視頻推送的回調函數,接受參數爲(video, req, res, next)。
* - `location`,處理位置推送的回調函數,接受參數爲(location, req, res, next)。
* - `link`,處理鏈接推送的回調函數,接受參數爲(link, req, res, next)。
* - `event`,處理事件推送的回調函數,接受參數爲(event, req, res, next)。
* - `shortvideo`,處理短視頻推送的回調函數,接受參數爲(event, req, res, next)。
* @param {String} token 在微信平臺填寫的口令
* @param {Function} handle 生成的回調函數,參見示例
*/
var middleware = function (token, handle) {
if (arguments.length === 1) {
return new Handler(token);
}
if (handle instanceof Handler) {
handle.setToken(token);
return handle.middlewarify();
} else {
return new Handler(token, handle).middlewarify();
}
};
['text', 'image', 'voice', 'video', 'shortvideo', 'location', 'link', 'event'].forEach(function (method) {
middleware[method] = function (fn) {
return (new Handler())[method](fn);
};
});
使用方法寫的還是非常簡單的,寫的也比較清晰,這裏封裝成了中間件,曾靜自己去寫過,最大的問題,莫過於微信用的是 XML格式的數據,需喲啊用到xml2js,var xml2js = require(‘xml2js’);可以看到wechat的開發者使用的也是這個,曾經的代碼就不放出來了,有興趣的童鞋可以去看源碼,大神們寫的太好了。
結語
用別人寫好的東西固然爽,但是還是要研究透如何寫出來的,才能從根本上提高自己的水平,這裏一起共勉。
ITDogFire–sky