-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
352 lines (306 loc) · 11.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const getRawBody = require('raw-body');
const xml2js = require('xml2js');
const crypto = require('crypto');
const ejs = require('ejs');
const WXBizMsgCrypt = require('wechat-crypto');
const Wechat = function Wechat(config) {
if (!(this instanceof Wechat)) {
return new Wechat(config);
}
return this.setToken(config);
};
Wechat.prototype.setToken = function setToken(config) {
if (typeof config === 'string') {
this.token = config;
} else if (typeof config === 'object' && config.token) {
this.token = config.token;
this.appid = config.appid || '';
this.encodingAESKey = config.encodingAESKey || '';
} else {
throw new TypeError('please check your config');
}
};
const getSignature = function getSignature(timestamp, nonce, token) {
const shasum = crypto.createHash('sha1');
const arr = [token, timestamp, nonce].sort();
shasum.update(arr.join(''));
return shasum.digest('hex');
};
const parseXML = function parseXML(xml) {
return new Promise((resolve, reject) => {
xml2js.parseString(xml, {trim: true}, (err, data) => {
if (err) {
return reject(err);
}
return resolve(data);
});
});
};
/*!
* 将xml2js解析出来的对象转换成直接可访问的对象
*/
const formatMessage = function formatMessage(result) {
const message = {};
if (typeof result === 'object') {
for (const key in result) {
if (!(result[key] instanceof Array) || result[key].length === 0) {
continue;
}
if (result[key].length === 1) {
const val = result[key][0];
if (typeof val === 'object') {
message[key] = formatMessage(val);
} else {
message[key] = (val || '').trim();
}
} else {
message[key] = result[key].map(item => formatMessage(item));
}
}
}
return message;
};
/*!
* 响应模版
*/
const tpl = `<xml>
<ToUserName><![CDATA[<%-toUsername%>]]></ToUserName>
<FromUserName><![CDATA[<%-fromUsername%>]]></FromUserName>
<CreateTime><%=createTime%></CreateTime>
<MsgType><![CDATA[<%=msgType%>]]></MsgType>
<% 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 || item.thumb_url %>]]></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>
<MediaId><![CDATA[<%-content.mediaId%>]]></MediaId>
<Title><![CDATA[<%-content.title%>]]></Title>
<Description><![CDATA[<%-content.description%>]]></Description>
</Video>
<% } else if (msgType === "transfer_customer_service") { %>
<% if (content && content.kfAccount) { %>
<TransInfo>
<KfAccount><![CDATA[<%-content.kfAccount%>]]></KfAccount>
</TransInfo>
<% } %>
<% } else { %>
<Content><![CDATA[<%-content%>]]></Content>
<% } %>
</xml>`;
/*!
* 编译过后的模版
*/
const compiled = ejs.compile(tpl);
const wrapTpl = `<xml>
<Encrypt><![CDATA[<%-encrypt%>]]></Encrypt>
<MsgSignature><![CDATA[<%-signature%>]]></MsgSignature>
<TimeStamp><%-timestamp%></TimeStamp>
<Nonce><![CDATA[<%-nonce%>]]></Nonce>
</xml>`;
const encryptWrap = ejs.compile(wrapTpl);
const reply2CustomerService = function reply2CustomerService(fromUsername, toUsername, kfAccount) {
const info = {};
info.msgType = 'transfer_customer_service';
info.createTime = new Date().getTime();
info.toUsername = toUsername;
info.fromUsername = fromUsername;
info.content = {};
if (typeof kfAccount === 'string') {
info.content.kfAccount = kfAccount;
}
return compiled(info);
};
/*!
* 将内容回复给微信的封装方法
*/
const reply = function reply(content, fromUsername, toUsername) {
const info = {};
let type = 'text';
info.content = content || '';
if (Array.isArray(content)) {
type = 'news';
} else if (typeof content === 'object') {
if (content.type) {
if (content.type === 'customerService') {
return reply2CustomerService(fromUsername, toUsername, content.kfAccount);
}
type = content.type;
info.content = content.content;
} else {
type = 'music';
}
}
info.msgType = type;
info.createTime = new Date().getTime();
info.toUsername = toUsername;
info.fromUsername = fromUsername;
return compiled(info);
};
Wechat.prototype._session = function _session(handle, sessionId, ctx) {
// 取session数据
this.wxSessionId = sessionId;
const _this = this;
return this.sessionStore.get(this.wxSessionId).then(session => {
_this.wxsession = session;
if (!session) {
_this.wxsession = {};
_this.wxsession.cookie = _this.session.cookie;
}
// 业务逻辑处理
return Reflect.apply(handle, this, [ctx]);
}).then(() => {
// 更新 session
if (_this.wxsession) {
return _this.sessionStore.set(_this.wxSessionId, _this.wxsession);
}
if (_this.wxSessionId) {
return _this.sessionStore.destroy(_this.wxSessionId);
}
return null;
});
};
Wechat.prototype.middleware = function middleware(handle) {
const _this = this;
if (this.encodingAESKey) {
this.cryptor = new WXBizMsgCrypt(this.token, this.encodingAESKey, this.appid);
}
return (ctx, next) => {
const query = ctx.query;
let method = ctx.method.toLowerCase();
const timestamp = query.timestamp;
const nonce = query.nonce;
const echostr = query.echostr;
// 加密模式
if (query.encrypt_type && query.encrypt_type === 'aes' && query.msg_signature) {
const encrypt = 'aes';
method = encrypt + method.replace(/^./ig, first => first.toUpperCase());
}
const methodHandles = {
get() {
if (query.signature !== getSignature(timestamp, nonce, _this.token)) {
return methodHandles._errorSign();
}
ctx.body = echostr;
return null;
},
aesGet() {
if (query.msg_signature !== _this.cryptor.getSignature(timestamp, nonce, echostr)) {
return methodHandles._errorSign();
}
const decrypted = _this.cryptor.decrypt(echostr);
ctx.body = decrypted.message;
return null;
},
post() {
// 校验
if (query.signature !== getSignature(timestamp, nonce, _this.token)) {
return methodHandles._errorSign();
}
// 取原始数据
return getRawBody(ctx.req, {
length: ctx.length,
limit: '1mb',
encoding: ctx.charset
}).then(xml => {
// 解析xml
ctx.state.wechatXml = xml;
return parseXML(xml);
}).then(parsed => {
const formated = formatMessage(parsed.xml);
// 挂载处理后的微信消息
ctx.state.wechat = formated;
if (this.sessionStore) {
return this._session(handle, formated.FromUserName, ctx);
}
return Reflect.apply(handle, this, [ctx, next]);
}).then(() => {
const formated = ctx.state.wechat;
if (ctx.body !== '') {
ctx.body = reply(ctx.body, formated.ToUserName, formated.FromUserName);
ctx.type = 'application/xml';
}
});
},
aesPost() {
// 取原始数据
return getRawBody(ctx.req, {
length: ctx.length,
limit: '1mb',
encoding: ctx.charset
}).then(xml => {
// 解析xml
ctx.state.wechatXml = xml;
return parseXML(xml);
}).then(parsed => {
// 解析xml
const formated = formatMessage(parsed.xml);
const encryptMessage = formated.Encrypt;
if (query.msg_signature !== _this.cryptor.getSignature(timestamp, nonce, encryptMessage)) {
return this._errorSign();
}
const decryptedXML = _this.cryptor.decrypt(encryptMessage);
const messageWrapXml = decryptedXML.message;
if (!messageWrapXml) {
return this._errorSign();
}
return parseXML(messageWrapXml).then(decodedXML => {
ctx.state.wechat = formatMessage(decodedXML.xml);
if (this.sessionStore) {
return this._session(handle, formated.FromUserName, ctx);
}
return Reflect.apply(handle, this, [ctx, next]);
}).then(() => {
const formatedData = ctx.state.wechat;
if (ctx.body !== '') {
const replyMessageXml = reply(ctx.body, formatedData.ToUserName, formatedData.FromUserName);
const wrap = {};
wrap.encrypt = _this.cryptor.encrypt(replyMessageXml);
wrap.nonce = parseInt(Math.random() * 100000000000, 10);
wrap.timestamp = new Date().getTime();
wrap.signature = _this.cryptor.getSignature(wrap.timestamp, wrap.nonce, wrap.encrypt);
ctx.body = encryptWrap(wrap);
this.type = 'application/xml';
}
});
});
},
_errorSign() {
this.status = 401;
this.body = 'Invalid signature';
},
_call() {
ctx.status = 501;
ctx.body = 'Not Implemented';
}
};
if (methodHandles[method]) {
return methodHandles[method]();
}
return methodHandles._call();
};
};
module.exports = Wechat;