微信公共平台Node库
分发消息
var Event = require('wechat').Event;
var events = new Event();
events.add('pic_weixin', function (message, req, res, next) {
// 弹出微信相册发图器的事件推送
});
var handle = Event.dispatch(events);
app.use('/wechat', wechat(config).event(handle).middlewarify());
方法签名
| 方法 | Event.dispatch() | ||
回复列表类型
方法签名
| 函数 | List() | ||
从List对象中根据key取出对应的handler
方法签名
| 方法 | List.prototype.get() | ||
| 参数 | key(String) | 列表中的关键词 |
|
静态方法,根据items生成List对象,并放置到缓存中
方法签名
| 方法 | List.add() | ||
| 参数 | name(String) | 列表名字 |
|
| 参数 | items(Array) | 元素列表 |
|
| 参数 | head(String) | 回复开头 |
|
| 参数 | delimiter(String) | 回复分隔符 |
|
| 参数 | foot(String) | 回复底部 |
|
静态方法,从缓存中根据名字取出List对象
方法签名
| 方法 | List.get() | ||
| 参数 | name(String) | 列表名字 |
|
静态方法,清空缓存的所有的List对象
方法签名
| 方法 | List.clear() | ||
| 参数 | name(String) | 列表名字 |
|
Session构造函数,用于与Connect的Session中间件集成的会话脚本
方法签名
| 函数 | Session() | ||
| 参数 | id(String) | Session ID |
|
| 参数 | req(Object) | Connect中的请求对象 |
|
| 参数 | data(Object) | 可选的其余数据,将被合并进Session对象中 |
|
保存Session对象到实际的存储中
Callback:
err, 错误对象,保存发生错误时传入方法签名
| 方法 | Session.prototype.save() | ||
| 参数 | callback(Function) | 保存Session的回调函数 |
|
销毁Session对象
Callback:
err, 错误对象,删除发生错误时传入方法签名
| 方法 | Session.prototype.destroy() | ||
| 参数 | callback(Function) | 从存储中删除Session数据后的回调函数 |
|
检查签名
方法签名
| 函数 | checkSignature() | ||
微信自动回复平台的内部的Handler对象
方法签名
| 函数 | Handler() | ||
| 参数 | config(String,Object) | 配置 |
|
| 参数 | handle(Function) | handle对象 |
|
设置handler对象
按消息设置handler对象的快捷方式
text(fn)image(fn)voice(fn)video(fn)location(fn)link(fn)event(fn)方法签名
| 方法 | Handler.prototype.setHandler() | ||
| 参数 | type(String) | handler处理的消息类型 |
|
| 参数 | handle(Function) | handle对象 |
|
根据消息类型取出handler对象
方法签名
| 方法 | Handler.prototype.getHandler() | ||
| 参数 | type(String) | 消息类型 |
|
根据Handler对象生成响应方法,并最终生成中间件函数
方法签名
| 方法 | Handler.prototype.middlewarify() | ||
根据口令
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)。方法签名
| 函数 | middleware() | ||
| 参数 | token(String) | 在微信平台填写的口令 |
|
| 参数 | handle(Function) | 生成的回调函数,参见示例 |
|