-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (41 loc) · 1.44 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
let token, expires_in;
let request = require('request');
const wxTemplate = {
sendMsg: async (params) => {
const {appId, appSecret, template_id, data, url, miniprogram, touser} = params;
token = await this.getAccessToken(appId, appSecret);
res = await this.pushService({data: params, token})
if (res.data && res.data.errcode === 40001 && res.data.errmsg.indexOf('access_token is invalid or not latest') > -1) {
token = await this.getAccessToken({appId, appSecret, refreshToken: true});
res = await this.pushService({data: params, token})
}
return res;
},
getAccessToken: async ({appId, appSecret, refreshToken}) => {
// token 未过期 或者 未强制刷新,则从内存中获取token
var now = new Date().getTime();
if (token && expires_in && now < expires_in && !refreshToken) {
return token;
}
const res = await request({
url: 'https://api.weixin.qq.com/cgi-bin/token',
method: 'GET',
params: {
grant_type: 'client_credential',
appid: appId,
secret: appSecret
}
});
token = res.data.access_token;
expires_in = new Date().getTime() + res.data.expires_in * 1000;
return res;
},
pushService: async ({data, token}) => {
res = await request({
url: `https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${token}`
method: 'POST',
body: JSON.stringify(data)
});
}
};
module.exports = wxTemplate;