Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持设备消息响应. #176

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions lib/wechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ var tpl = ['<xml>',
'<ToUserName><![CDATA[<%-toUsername%>]]></ToUserName>',
'<FromUserName><![CDATA[<%-fromUsername%>]]></FromUserName>',
'<CreateTime><%=createTime%></CreateTime>',
'<MsgType><![CDATA[<%=msgType%>]]></MsgType>',
'<% if (msgType === "device_event" && (Event === "subscribe_status" || Event === "unsubscribe_status")) { %>',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

设备团队也够奇葩的。无视msgType的设定。

'<% if (Event === "subscribe_status" || Event === "unsubscribe_status") { %>',
'<MsgType><![CDATA[device_status]]></MsgType>',
'<DeviceStatus><%=DeviceStatus%></DeviceStatus>',
'<% } else { %>',
'<MsgType><![CDATA[<%=msgType%>]]></MsgType>',
'<Event><![CDATA[<%-Event%>]]></Event>',
'<% } %>',
'<% } else { %>',
'<MsgType><![CDATA[<%=msgType%>]]></MsgType>',
'<% } %>',
'<% if (msgType === "news") { %>',
'<ArticleCount><%=content.length%></ArticleCount>',
'<Articles>',
Expand Down Expand Up @@ -62,6 +72,22 @@ var tpl = ['<xml>',
'<Title><![CDATA[<%-content.title%>]]></Title>',
'<Description><![CDATA[<%-content.description%>]]></Description>',
'</Video>',
'<% } else if (msgType === "hardware") { %>',
'<HardWare>',
'<MessageView><![CDATA[<%-HardWare.MessageView%>]]></MessageView>',
'<MessageAction><![CDATA[<%-HardWare.MessageAction%>]]></MessageAction>',
'</HardWare>',
'<FuncFlag>0</FuncFlag>',
'<% } else if (msgType === "device_text" || msgType === "device_event") { %>',
'<DeviceType><![CDATA[<%-DeviceType%>]]></DeviceType>',
'<DeviceID><![CDATA[<%-DeviceID%>]]></DeviceID>',
'<% if (msgType === "device_text") { %>',
'<Content><![CDATA[<%-content%>]]></Content>',
'<% } else if ((msgType === "device_event" && Event != "subscribe_status" && Event != "unsubscribe_status")) { %>',
'<Content><![CDATA[<%-content%>]]></Content>',
'<Event><![CDATA[<%-Event%>]]></Event>',
'<% } %>',
'<SessionID><%=SessionID%></SessionID>',
'<% } else if (msgType === "transfer_customer_service") { %>',
'<% if (content && content.kfAccount) { %>',
'<TransInfo>',
Expand Down Expand Up @@ -151,22 +177,42 @@ var formatMessage = function (result) {
/*!
* 将内容回复给微信的封装方法
*/
var reply = function (content, fromUsername, toUsername) {
var reply = function (content, fromUsername, toUsername, message) {
var info = {};
var type = 'text';
info.content = content || '';
if (Array.isArray(content)) {
info.createTime = new Date().getTime();
if (message && (message.MsgType === 'device_text' || message.MsgType === 'device_event')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有这里,message和content不能统一起来吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是很明白你的意思.message是直接从微信请求那里传进去的,content是服务器回复内容,请问需要统一成什么样子?不过把fromUsername和toUsername整到message里面还是可以的.

info.DeviceType = message.DeviceType;
info.DeviceID = message.DeviceID;
info.SessionID = isNaN(message.SessionID) ? 0 : message.SessionID;
info.createTime = Math.floor(info.createTime / 1000);
if (message['Event'] === 'subscribe_status' || message['Event'] === 'unsubscribe_status') {
delete info.content;
info.DeviceStatus = isNaN(content) ? 0 : content;
} else {
info.content = new Buffer(String(content)).toString('base64');
}
type = message.MsgType;
if (message.MsgType === 'device_event') {
info['Event'] = message['Event'];
}
} else if (Array.isArray(content)) {
type = 'news';
} else if (typeof content === 'object') {
if (content.hasOwnProperty('type')) {
type = content.type;
info.content = content.content;
if (content.content) {
info.content = content.content;
}
if (content.HardWare) {
info.HardWare = content.HardWare;
}
} else {
type = 'music';
}
}
info.msgType = type;
info.createTime = new Date().getTime();
info.toUsername = toUsername;
info.fromUsername = fromUsername;
return compiled(info);
Expand Down Expand Up @@ -195,7 +241,7 @@ var respond = function (handler) {
if (!content) {
return res.end('');
}
var xml = reply(content, message.ToUserName, message.FromUserName);
var xml = reply(content, message.ToUserName, message.FromUserName, message);
if (!req.query.encrypt_type || req.query.encrypt_type === 'raw') {
res.end(xml);
} else {
Expand Down Expand Up @@ -331,7 +377,7 @@ Handler.prototype.setHandler = function (type, fn) {
return this;
};

['text', 'image', 'voice', 'video', 'location', 'link', 'event'].forEach(function (method) {
['text', 'image', 'voice', 'video', 'location', 'link', 'event', 'hardware', 'device_text', 'device_event'].forEach(function (method) {
Handler.prototype[method] = function (fn) {
return this.setHandler(method, fn);
};
Expand Down
99 changes: 99 additions & 0 deletions test/reply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,103 @@ describe('wechat.js', function () {
result.should.be.include('<KfAccount><![CDATA[kf]]></KfAccount>');
});
});

describe('reply device text', function () {
it('device text reply("hello from device") should ok', function () {
var MsgType = 'device_text';
var DeviceType = 'to_user';
var DeviceID = 'device_id';
var SessionID = 709394;
var content = 'hello from device';
var message = {
MsgType: MsgType,
DeviceType: DeviceType,
DeviceID: DeviceID,
SessionID: SessionID,
};
var result = reply(content, 'from', 'to', message);
result.should.be.include('<FromUserName><![CDATA[from]]></FromUserName>');
result.should.be.include('<ToUserName><![CDATA[to]]></ToUserName>');
result.should.be.include('<MsgType><![CDATA[' + MsgType + ']]></MsgType>');
result.should.be.include('<DeviceType><![CDATA[' + DeviceType + ']]></DeviceType>');
result.should.be.include('<DeviceID><![CDATA[' +DeviceID + ']]></DeviceID>');
result.should.be.include('<SessionID>' + SessionID + '</SessionID>');
result.should.be.include('<Content><![CDATA[' + new Buffer(String(content)).toString('base64') + ']]></Content>');
});
});

describe('reply device binding event', function () {
it('device binding event reply("bind") should ok', function () {
var MsgType = 'device_event';
var Event = 'bind';
var DeviceType = 'to_user';
var DeviceID = 'device_id';
var SessionID = 709394;
var content = 'bind';
var message = {
MsgType: MsgType,
Event: Event,
DeviceType: DeviceType,
DeviceID: DeviceID,
SessionID: SessionID,
};
var result = reply(content, 'from', 'to', message);
result.should.be.include('<FromUserName><![CDATA[from]]></FromUserName>');
result.should.be.include('<ToUserName><![CDATA[to]]></ToUserName>');
result.should.be.include('<MsgType><![CDATA[' + MsgType + ']]></MsgType>');
result.should.be.include('<Event><![CDATA[' + Event + ']]></Event>');
result.should.be.include('<DeviceType><![CDATA[' + DeviceType + ']]></DeviceType>');
result.should.be.include('<DeviceID><![CDATA[' +DeviceID + ']]></DeviceID>');
result.should.be.include('<SessionID>' + SessionID + '</SessionID>');
result.should.be.include('<Content><![CDATA[' + new Buffer(String(content)).toString('base64') + ']]></Content>');
});
});

describe('reply WIFI device status', function () {
it('WIFI device status reply("bind") should ok', function () {
var MsgType = 'device_event';
var Event = 'subscribe_status';
var DeviceType = 'to_user';
var DeviceID = 'device_id';
var OpType = 0;
var OpenID = 'open_id';
var content = 0;
var message = {
MsgType: MsgType,
Event: Event,
DeviceType: DeviceType,
DeviceID: DeviceID,
OpType: OpType,
OpenID: OpenID
};
var result = reply(content, 'from', 'to', message);
result.should.be.include('<FromUserName><![CDATA[from]]></FromUserName>');
result.should.be.include('<ToUserName><![CDATA[to]]></ToUserName>');
result.should.be.include('<MsgType><![CDATA[device_status]]></MsgType>');
result.should.be.include('<DeviceStatus>' + content + '</DeviceStatus>');
result.should.be.include('<DeviceType><![CDATA[' + DeviceType + ']]></DeviceType>');
result.should.be.include('<DeviceID><![CDATA[' +DeviceID + ']]></DeviceID>');
});
});

describe('reply social function', function () {
it('social function reply({type: "hardware", HardWare: {MessageView: "myrank", MessageAction: "ranklist"}}) should ok', function () {
var MsgType = 'hardware';
var view = 'myrank';
var action = 'ranklist';
var content = {
type: MsgType,
HardWare:{
MessageView: view,
MessageAction: action
}
};
var result = reply(content, 'from', 'to');
result.should.be.include('<FromUserName><![CDATA[from]]></FromUserName>');
result.should.be.include('<ToUserName><![CDATA[to]]></ToUserName>');
result.should.be.include('<MsgType><![CDATA[' + MsgType + ']]></MsgType>');
result.should.be.include('<HardWare><MessageView><![CDATA[' + view + ']]></MessageView><MessageAction><![CDATA[' + action + ']]></MessageAction></HardWare>');
result.should.be.include('<FuncFlag>0</FuncFlag>');
});
});
});