|
@@ -22,7 +22,7 @@ const sha1 = (str) => { |
|
@@ -22,7 +22,7 @@ const sha1 = (str) => { |
22
|
|
22
|
|
23
|
// 微信 JS 接口签名校验工具 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
|
23
|
// 微信 JS 接口签名校验工具 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
|
24
|
|
24
|
|
25
|
-let getAccessToken = function(req, next, appId, secret, prefix) { // 获取access_token
|
25
|
+let getAccessToken = function(appId, secret, prefix) { // 获取access_token
|
26
|
return request({
|
26
|
return request({
|
27
|
url: prefix + 'token',
|
27
|
url: prefix + 'token',
|
28
|
qs: {
|
28
|
qs: {
|
|
@@ -33,11 +33,11 @@ let getAccessToken = function(req, next, appId, secret, prefix) { // 获取acces |
|
@@ -33,11 +33,11 @@ let getAccessToken = function(req, next, appId, secret, prefix) { // 获取acces |
33
|
json: true
|
33
|
json: true
|
34
|
}).then(res => {
|
34
|
}).then(res => {
|
35
|
return res;
|
35
|
return res;
|
36
|
- }).catch(next);
|
36
|
+ });
|
37
|
|
37
|
|
38
|
};
|
38
|
};
|
39
|
|
39
|
|
40
|
-let getTicket = function(req, accessToken, next, appId, secret, prefix) { // 获取ticket
|
40
|
+let getTicket = function(accessToken, appId, secret, prefix) { // 获取ticket
|
41
|
return request({
|
41
|
return request({
|
42
|
url: prefix + 'ticket/getticket',
|
42
|
url: prefix + 'ticket/getticket',
|
43
|
qs: {
|
43
|
qs: {
|
|
@@ -50,7 +50,7 @@ let getTicket = function(req, accessToken, next, appId, secret, prefix) { // 获 |
|
@@ -50,7 +50,7 @@ let getTicket = function(req, accessToken, next, appId, secret, prefix) { // 获 |
50
|
});
|
50
|
});
|
51
|
};
|
51
|
};
|
52
|
|
52
|
|
53
|
-let calcSigature = function(ticket_res, res) { // 计算signature
|
53
|
+let calcSigature = function(ticket_res) { // 计算signature
|
54
|
let appID = ticket_res.appID;
|
54
|
let appID = ticket_res.appID;
|
55
|
let ticket = ticket_res.access_ticket;
|
55
|
let ticket = ticket_res.access_ticket;
|
56
|
let nonceStr = Math.random().toString(36).substr(2, 15);
|
56
|
let nonceStr = Math.random().toString(36).substr(2, 15);
|
|
@@ -59,63 +59,32 @@ let calcSigature = function(ticket_res, res) { // 计算signature |
|
@@ -59,63 +59,32 @@ let calcSigature = function(ticket_res, res) { // 计算signature |
59
|
let str = `jsapi_ticket=${ticket}&noncestr=${nonceStr}×tamp=${timestamp}&url=${url}`;
|
59
|
let str = `jsapi_ticket=${ticket}&noncestr=${nonceStr}×tamp=${timestamp}&url=${url}`;
|
60
|
let signature = sha1(str);
|
60
|
let signature = sha1(str);
|
61
|
|
61
|
|
62
|
- console.log(str + '&signature=' + signature);
|
|
|
63
|
-
|
|
|
64
|
- let result = {
|
62
|
+ return {
|
65
|
appId: appID,
|
63
|
appId: appID,
|
66
|
nonceStr,
|
64
|
nonceStr,
|
67
|
timestamp,
|
65
|
timestamp,
|
68
|
url,
|
66
|
url,
|
69
|
signature
|
67
|
signature
|
70
|
};
|
68
|
};
|
71
|
-
|
|
|
72
|
- // console.log('result', result);
|
|
|
73
|
- return res.jsonp(result);
|
|
|
74
|
};
|
69
|
};
|
75
|
|
70
|
|
76
|
-let getResult = function(wechat_res, req, res, next) { // 向微信接口获取access_token及ticket并计算signature
|
71
|
+let getResult = function(wechat_res) { // 向微信接口获取access_token及ticket并计算signature
|
77
|
let appID = wechat_res.appID;
|
72
|
let appID = wechat_res.appID;
|
78
|
let appSecret = wechat_res.appSecret;
|
73
|
let appSecret = wechat_res.appSecret;
|
79
|
let prefix = wechat_res.prefix;
|
74
|
let prefix = wechat_res.prefix;
|
80
|
let mpPrefix = wechat_res.mpPrefix;
|
75
|
let mpPrefix = wechat_res.mpPrefix;
|
81
|
- let url = wechat_res.url;
|
|
|
82
|
- let type = wechat_res.type;
|
|
|
83
|
|
76
|
|
84
|
- return getAccessToken(req, next, appID, appSecret, prefix)
|
77
|
+ return getAccessToken(appID, appSecret, prefix)
|
85
|
.then(result => { // 先获取accessToken
|
78
|
.then(result => { // 先获取accessToken
|
86
|
let access_token = result.access_token;
|
79
|
let access_token = result.access_token;
|
87
|
|
80
|
|
88
|
- console.log('token:', access_token);
|
|
|
89
|
- return getTicket(req, access_token, next, appID, appSecret, mpPrefix)
|
81
|
+ return getTicket(access_token, appID, appSecret, mpPrefix)
|
90
|
.then(ticket_res => { // 通过accessToken获取ticket
|
82
|
.then(ticket_res => { // 通过accessToken获取ticket
|
91
|
- if (ticket_res && ticket_res.ticket) {
|
|
|
92
|
- let access_ticket = ticket_res.ticket;
|
|
|
93
|
- let expired = parseInt(ticket_res.expires_in, 10) - 100;
|
|
|
94
|
- let create_time = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
95
|
- let expired_time = moment().add(expired, 'seconds').format('YYYY-MM-DD HH:mm:ss');
|
|
|
96
|
-
|
|
|
97
|
- // console.log('ticket_res:', ticket_res);
|
|
|
98
|
- req.ctx(WechatModel).
|
|
|
99
|
- saveAccessKeys(access_token, access_ticket, create_time, expired_time, type)
|
|
|
100
|
- .then(() => {
|
|
|
101
|
- let access_res = {
|
|
|
102
|
- appID: appID,
|
|
|
103
|
- access_ticket: access_ticket,
|
|
|
104
|
- url: url
|
|
|
105
|
- };
|
|
|
106
|
-
|
|
|
107
|
- calcSigature(access_res, res);
|
|
|
108
|
- });
|
|
|
109
|
- } else {
|
|
|
110
|
- res.jsonp({
|
|
|
111
|
- appID: appID,
|
|
|
112
|
- nonceStr: '',
|
|
|
113
|
- timestamp: '',
|
|
|
114
|
- url: url,
|
|
|
115
|
- signature: 'get failed'
|
|
|
116
|
- });
|
|
|
117
|
- }
|
|
|
118
|
- }).catch(next);
|
83
|
+ return Object.assign({
|
|
|
84
|
+ access_token: access_token,
|
|
|
85
|
+ appID: appID
|
|
|
86
|
+ }, ticket_res);
|
|
|
87
|
+ });
|
119
|
});
|
88
|
});
|
120
|
};
|
89
|
};
|
121
|
|
90
|
|
|
@@ -132,51 +101,52 @@ const wechatShare = { |
|
@@ -132,51 +101,52 @@ const wechatShare = { |
132
|
|
101
|
|
133
|
req.ctx(WechatModel).getAccessKeys()
|
102
|
req.ctx(WechatModel).getAccessKeys()
|
134
|
.then(keys_res => { // 先查询数据库中是否有access_token并且未过期
|
103
|
.then(keys_res => { // 先查询数据库中是否有access_token并且未过期
|
135
|
- console.log(keys_res);
|
|
|
136
|
- if (keys_res.length > 0) {
|
|
|
137
|
- let access_token = keys_res[0].access_token;
|
|
|
138
|
- let access_ticket = keys_res[0].access_ticket;
|
|
|
139
|
- let expired = new Date(keys_res[0].access_expired).getTime();
|
|
|
140
|
- let now = Date.now();
|
|
|
141
|
-
|
|
|
142
|
- console.log('data_time!', expired, now, keys_res[0].access_expired);
|
|
|
143
|
- if (now > expired) { // 过期则去 取新的token与ticket 并且更新数据库
|
|
|
144
|
- return Promise.reject({
|
|
|
145
|
- type: 'update'
|
|
|
146
|
- });
|
|
|
147
|
- } else {
|
|
|
148
|
- return Promise.resolve({ // 没过期则直接返回数据库中保存的access_ticket
|
|
|
149
|
- access_token,
|
|
|
150
|
- access_ticket
|
|
|
151
|
- });
|
|
|
152
|
- }
|
|
|
153
|
-
|
|
|
154
|
- } else {
|
|
|
155
|
- return Promise.reject({ // 数据库中没有token数据则新增
|
|
|
156
|
- type: 'save'
|
104
|
+
|
|
|
105
|
+ if (keys_res.code === 200) { // code=200说明数据库中查找到数据并且未过期
|
|
|
106
|
+
|
|
|
107
|
+ delete keys_res.code;
|
|
|
108
|
+ return Object.assign({
|
|
|
109
|
+ appID,
|
|
|
110
|
+ url
|
|
|
111
|
+ }, keys_res);
|
|
|
112
|
+ } else if (keys_res.code === 201) {
|
|
|
113
|
+ // code=201说明数据库中没有数据,或者数据已过期,根据返回的type判断是添加还是更新
|
|
|
114
|
+
|
|
|
115
|
+ delete keys_res.code;
|
|
|
116
|
+ keys_res = Object.assign({
|
|
|
117
|
+ appID,
|
|
|
118
|
+ appSecret,
|
|
|
119
|
+ prefix,
|
|
|
120
|
+ mpPrefix,
|
|
|
121
|
+ url
|
|
|
122
|
+ }, keys_res);
|
|
|
123
|
+
|
|
|
124
|
+ return getResult(keys_res).then(ticket_res => {
|
|
|
125
|
+ let appId = ticket_res.appID;
|
|
|
126
|
+ let access_token = ticket_res.access_token || '';
|
|
|
127
|
+ let access_ticket = ticket_res.ticket || '';
|
|
|
128
|
+ let expired = parseInt(ticket_res.expires_in, 10) - 100 || 0;
|
|
|
129
|
+ let create_time = moment().format('YYYY-MM-DD HH:mm:ss') || '';
|
|
|
130
|
+ let expired_time = moment().add(expired, 'seconds').format('YYYY-MM-DD HH:mm:ss') || '';
|
|
|
131
|
+
|
|
|
132
|
+ // 获取到ticket信息之后把相关字段保存到数据库中
|
|
|
133
|
+ return req.ctx(WechatModel).
|
|
|
134
|
+ saveAccessKeys(access_token, access_ticket, create_time, expired_time, keys_res.type)
|
|
|
135
|
+ .then(() => {
|
|
|
136
|
+ return {
|
|
|
137
|
+ appID: appId,
|
|
|
138
|
+ access_ticket: access_ticket,
|
|
|
139
|
+ url: url
|
|
|
140
|
+ };
|
|
|
141
|
+ });
|
157
|
});
|
142
|
});
|
158
|
}
|
143
|
}
|
159
|
})
|
144
|
})
|
160
|
- .then(access_res => {
|
|
|
161
|
- access_res = Object.assign({
|
|
|
162
|
- appID,
|
|
|
163
|
- url
|
|
|
164
|
- }, access_res);
|
|
|
165
|
- calcSigature(access_res, res); // 组合参数, 计算signature
|
|
|
166
|
- },
|
|
|
167
|
- fail_res => {
|
|
|
168
|
- fail_res = Object.assign({
|
|
|
169
|
- appID: appID,
|
|
|
170
|
- appSecret: appSecret,
|
|
|
171
|
- prefix: prefix,
|
|
|
172
|
- mpPrefix: mpPrefix,
|
|
|
173
|
- url: url
|
|
|
174
|
- }, fail_res);
|
|
|
175
|
-
|
|
|
176
|
- getResult(fail_res, req, res, next); // 向微信接口获取access_token及ticket并计算signature
|
|
|
177
|
- });
|
|
|
178
|
-
|
145
|
+ .then(result => {
|
|
|
146
|
+ let calc_res = calcSigature(result);
|
179
|
|
147
|
|
|
|
148
|
+ res.jsonp(calc_res);
|
|
|
149
|
+ }).catch(next);
|
180
|
}
|
150
|
}
|
181
|
};
|
151
|
};
|
182
|
|
152
|
|