-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Closed
支持设备消息响应. #176
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")) { %>', | ||
'<% 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>', | ||
|
@@ -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>', | ||
|
@@ -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')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 还有这里,message和content不能统一起来吗 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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 { | ||
|
@@ -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); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
设备团队也够奇葩的。无视msgType的设定。