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

Patch 1 #13

Merged
merged 2 commits into from
Apr 13, 2016
Merged
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
121 changes: 62 additions & 59 deletions lib/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,36 @@ function readable(original, pmap, mmap) {
}
}
data.createTime = new Date(parseInt(data.createTime, 10) * 1000)
// for compatibility
// for compatibility
data.created = data.createTime
return data
}

function flattern(tree) {
var ret = {}
if (tree.childs) {
tree.childs.forEach(function(item) {
if (!item.name) {
ret = item
return false
}
var value = flattern(item)
if (item.name in ret) {
ret[item.name] = [ret[item.name], value]
}
ret[item.name] = value
})
tree.childs
.filter(function (child) {
return child !== '\n' && child !== '\n\n' && child !== '\n\n\n'
}).forEach(function (item) {
if (!item.name) {
ret = item
return false
}
var value = flattern(item)
if (item.name in ret) {
ret[item.name] = [ret[item.name], value]
}
ret[item.name] = value
})
}
return ret
}

function parseXml(b, options) {
options = options || {}

var pmap = options.propMap|| propMap
var pmap = options.propMap || propMap
var mmap = options.paramMap || paramMap
var tree = xmllite.parseString(b)
var xml = flattern(tree)
Expand All @@ -81,52 +84,52 @@ function parseXml(b, options) {

var renderXml = lodash_tmpl([
'<xml>',
'<ToUserName><![CDATA[<%- uid %>]]></ToUserName>',
'<FromUserName><![CDATA[<%- sp %>]]></FromUserName>',
'<CreateTime><%= Math.floor(createTime.valueOf() / 1000) %></CreateTime>',
'<MsgType><![CDATA[<%= msgType %>]]></MsgType>',
'<% if (msgType === "transfer_customer_service" && kfAccount) { %>',
'<TransInfo>',
'<KfAccount><%- kfAccount %></KfAccount>',
'</TransInfo>',
'<% } %>',
'<% if (msgType === "news") { %>',
'<ArticleCount><%=content.length%></ArticleCount>',
'<Articles>',
'<% content.forEach(function(item){ %>',
'<item>',
'<Title><![CDATA[<%=item.title%>]]></Title>',
'<Description><![CDATA[<%=item.description%>]]></Description>',
'<PicUrl><![CDATA[<%=item.picUrl || item.picurl || item.pic %>]]></PicUrl>',
'<Url><![CDATA[<%=item.url%>]]></Url>',
'</item>',
'<% }) %>',
'</Articles>',
'<% } else if (msgType === "music") { %>',
'<Music>',
'<Title><![CDATA[<%=content.title%>]]></Title>',
'<Description><![CDATA[<%=content.description%>]]></Description>',
'<MusicUrl><![CDATA[<%=content.musicUrl || content.url %>]]></MusicUrl>',
'<HQMusicUrl><![CDATA[<%=content.hqMusicUrl || content.hqUrl %>]]></HQMusicUrl>',
'</Music>',
'<% } else if (msgType === "voice") { %>',
'<Voice>',
'<MediaId><![CDATA[<%=content.mediaId%>]]></MediaId>',
'</Voice>',
'<% } else if (msgType === "image") { %>',
'<Image>',
'<MediaId><![CDATA[<%-content.mediaId%>]]></MediaId>',
'</Image>',
'<% } else if (msgType === "video") { %>',
'<Video>',
'<Title><![CDATA[<%=content.title%>]]></Title>',
'<Description><![CDATA[<%=content.description%>]]></Description>',
'<MediaId><![CDATA[<%=content.mediaId%>]]></MediaId>',
'<ThumbMediaId><![CDATA[<%=content.thumbMediaId%>]]></ThumbMediaId>',
'</Video>',
'<% } else { %>',
'<Content><![CDATA[<%=content%>]]></Content>',
'<% } %>',
'<ToUserName><![CDATA[<%- uid %>]]></ToUserName>',
'<FromUserName><![CDATA[<%- sp %>]]></FromUserName>',
'<CreateTime><%= Math.floor(createTime.valueOf() / 1000) %></CreateTime>',
'<MsgType><![CDATA[<%= msgType %>]]></MsgType>',
'<% if (msgType === "transfer_customer_service" && kfAccount) { %>',
'<TransInfo>',
'<KfAccount><%- kfAccount %></KfAccount>',
'</TransInfo>',
'<% } %>',
'<% if (msgType === "news") { %>',
'<ArticleCount><%=content.length%></ArticleCount>',
'<Articles>',
'<% content.forEach(function(item){ %>',
'<item>',
'<Title><![CDATA[<%=item.title%>]]></Title>',
'<Description><![CDATA[<%=item.description%>]]></Description>',
'<PicUrl><![CDATA[<%=item.picUrl || item.picurl || item.pic %>]]></PicUrl>',
'<Url><![CDATA[<%=item.url%>]]></Url>',
'</item>',
'<% }) %>',
'</Articles>',
'<% } else if (msgType === "music") { %>',
'<Music>',
'<Title><![CDATA[<%=content.title%>]]></Title>',
'<Description><![CDATA[<%=content.description%>]]></Description>',
'<MusicUrl><![CDATA[<%=content.musicUrl || content.url %>]]></MusicUrl>',
'<HQMusicUrl><![CDATA[<%=content.hqMusicUrl || content.hqUrl %>]]></HQMusicUrl>',
'</Music>',
'<% } else if (msgType === "voice") { %>',
'<Voice>',
'<MediaId><![CDATA[<%=content.mediaId%>]]></MediaId>',
'</Voice>',
'<% } else if (msgType === "image") { %>',
'<Image>',
'<MediaId><![CDATA[<%-content.mediaId%>]]></MediaId>',
'</Image>',
'<% } else if (msgType === "video") { %>',
'<Video>',
'<Title><![CDATA[<%=content.title%>]]></Title>',
'<Description><![CDATA[<%=content.description%>]]></Description>',
'<MediaId><![CDATA[<%=content.mediaId%>]]></MediaId>',
'<ThumbMediaId><![CDATA[<%=content.thumbMediaId%>]]></ThumbMediaId>',
'</Video>',
'<% } else { %>',
'<Content><![CDATA[<%=content%>]]></Content>',
'<% } %>',
'</xml>'
].join(''))

Copy link
Member

Choose a reason for hiding this comment

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

这里的 indentation 能调整一下不?

Expand Down