-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathjscode2session.js
39 lines (33 loc) · 1.15 KB
/
jscode2session.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
const url = require('url');
const promisify = require('es6-promisify');
const co = require('co');
const config = require('../config');
const send = promisify(require('request').get, { multiArgs: true });
module.exports = {
apiUrl: 'https://api.weixin.qq.com/sns/jscode2session',
exchange: co.wrap(function *(jscode) {
try {
const requestUrl = this._buildUrl(jscode);
const [response, body] = yield send({ 'url': requestUrl, 'json': true });
// body: { session_key, expires_in, openid }
if ('session_key' in body) {
return { sessionKey: body.session_key, openId: body.openid };
}
let error = new Error('jscode failed to exchange session_key');
error.detail = body;
throw error;
} catch (error) {
throw error;
}
}),
_buildUrl(jscode) {
return `${this.apiUrl}${url.format({
query: {
'appid': config.appId,
'secret': config.appSecret,
'js_code': jscode,
'grant_type': 'authorization_code',
},
})}`;
},
};