Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
calidion committed Apr 18, 2016
1 parent f67e456 commit 580fbec
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,29 @@ var nodeWeixinMessage = require('node-weixin-message');
//在http请求里的处理方式
app.get('weixin/text', function(req, res) {
var messages = nodeWeixinMessage.messages;
function text(message) {
//只会被调用一次

function text(message, res, callback, extra) {
//message => 解析后的JSON
//res => res
//callback => callback
//extra => 'some data',

//Extra
res.send(message);
}
//多次相同的回调函数只会被调用一次

//多次侦听相同的回调函数只会被调用一次
messages.on.text(text);
messages.on.text(text);
messages.on.text(text);
messages.onXML(req.body, res, function callback(message) {
//After message handled.
}
//后面可以接系统允许的最大数量的参数,只要跟text的处理函数一一对应就可以了。
//唯一不同的是req.body会被解析成JSON
//,
//'some data');

x2j.parseString(req.body, {
explicitArray: false,
ignoreAttrs: true
}, function(error, json) {
messages.parse(json.xml);
});
});
```
Expand Down
5 changes: 3 additions & 2 deletions lib/incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
on: ons,
onXML: function(xml) {
var self = this;
var preArg = arguments;
x2j.parseString(xml, {
explicitArray: false,
ignoreAttrs: true
Expand All @@ -44,8 +45,8 @@ module.exports = {
throw Error('Failed Parsing XML!');
}
var args = [json.xml];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
for (var i = 1; i < preArg.length; i++) {
args.push(preArg[i]);
}
self.parse.apply(self, args);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-weixin-message",
"version": "0.0.9",
"version": "0.0.10",
"description": "",
"homepage": "",
"author": {
Expand Down
31 changes: 31 additions & 0 deletions test/incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,35 @@ describe('node-weixin-message', function() {
}
http(r1, r2);
});

it('should not be able to use onXML', function(done) {
var r1 = {};
var r2 = {};
r2.send = function(message) {
assert.equal(true, message.FromUserName === 'fromUser');
assert.equal(true, message.ToUserName === 'toUser');
assert.equal(true, message.CreateTime === '1351776360');
assert.equal(true, message.MsgType === 'link');
assert.equal(true, message.Title === '公众平台官网链接');
assert.equal(true, message.Description === '公众平台官网链接');
assert.equal(true, message.Url === 'url');
assert.equal(true, message.MsgId === '1234567890123456');
};

function http(req, res) {
var messages = nodeWeixinMessage.messages;
function A(message, res, cb, more) {
assert.equal(res, r2);
assert.equal(more, 'and more');
res.send(message);
cb();
}
messages.on.link(A);
var xml = fs.readFileSync(path.resolve(__dirname, './messages/link.xml'));
messages.onXML(xml, res, function() {
done();
}, 'and more');
}
http(r1, r2);
});
});
2 changes: 1 addition & 1 deletion test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('node-weixin-message', function () {
}, function (error, data) {
assert.equal(true, !error);
if (data.errcode === 40036) {
console.log(error, data);
console.error(error, data);
} else {
assert.equal(true, data.errcode === 0);
assert.equal(true, data.errmsg === 'ok');
Expand Down

0 comments on commit 580fbec

Please sign in to comment.