node-easywechat 3.6.3 → 3.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +25 -0
- package/dist/Channel/AccessToken.d.ts +8 -0
- package/dist/Channel/AccessToken.js +15 -0
- package/dist/Channel/Account.d.ts +4 -0
- package/dist/Channel/Account.js +8 -0
- package/dist/Channel/Application.d.ts +60 -0
- package/dist/Channel/Application.js +125 -0
- package/dist/Channel/Contracts/AccountInterface.d.ts +23 -0
- package/dist/Channel/Contracts/AccountInterface.js +25 -0
- package/dist/Channel/Contracts/ApplicationInterface.d.ts +62 -0
- package/dist/Channel/Contracts/ApplicationInterface.js +55 -0
- package/dist/Channel/Message.d.ts +542 -0
- package/dist/Channel/Message.js +9 -0
- package/dist/Channel/Server.d.ts +91 -0
- package/dist/Channel/Server.js +16 -0
- package/dist/Core/Cache/FileCache.js +46 -63
- package/dist/Core/Contracts/AccessTokenInterface.js +2 -15
- package/dist/Core/Contracts/CacheInterface.js +4 -21
- package/dist/Core/Contracts/RefreshableAccessTokenInterface.js +1 -12
- package/dist/Core/Contracts/ServerInterface.d.ts +3 -3
- package/dist/Core/Contracts/ServerInterface.js +4 -15
- package/dist/Core/Http/ServerRequest.js +34 -49
- package/dist/Core/HttpClient/AccessTokenAwareClient.js +6 -17
- package/dist/Core/HttpClient/Contracts/HttpClientInterface.js +1 -12
- package/dist/Core/HttpClient/HttpClient.js +74 -85
- package/dist/Core/HttpClient/HttpClientResponse.js +35 -50
- package/dist/Core/HttpClient/Mixins/HttpClientMethodsMixin.js +34 -59
- package/dist/Core/HttpClient/Mixins/PresetMixin.js +8 -11
- package/dist/Core/Message.js +20 -31
- package/dist/Core/Mixins/{DecryptXmlMessageMixin.d.ts → DecryptMessageMixin.d.ts} +2 -2
- package/dist/Core/Mixins/DecryptMessageMixin.js +34 -0
- package/dist/Core/Mixins/HandlersMixin.d.ts +8 -8
- package/dist/Core/Mixins/HandlersMixin.js +26 -39
- package/dist/Core/Mixins/{ResponseXmlMessageMixin.d.ts → ResponseMessageMixin.d.ts} +4 -3
- package/dist/Core/Mixins/ResponseMessageMixin.js +61 -0
- package/dist/Core/Support/Utils.js +8 -19
- package/dist/MiniApp/Application.js +1 -1
- package/dist/MiniApp/Utils.js +13 -24
- package/dist/OfficialAccount/AccessToken.js +42 -59
- package/dist/OfficialAccount/Application.js +1 -1
- package/dist/OfficialAccount/JsApiTicket.js +31 -44
- package/dist/OfficialAccount/Server.d.ts +24 -0
- package/dist/OfficialAccount/Server.js +25 -37
- package/dist/OfficialAccount/Utils.js +6 -17
- package/dist/OpenPlatform/Application.js +62 -86
- package/dist/OpenPlatform/Authorizer/MiniApp/Utils.js +14 -25
- package/dist/OpenPlatform/AuthorizerAccessToken.js +6 -19
- package/dist/OpenPlatform/ComponentAccessToken.js +27 -42
- package/dist/OpenPlatform/Server.js +31 -54
- package/dist/OpenPlatform/VerifyTicket.js +14 -27
- package/dist/OpenWork/Application.js +61 -84
- package/dist/OpenWork/AuthorizerAccessToken.js +32 -47
- package/dist/OpenWork/JsApiTicket.js +64 -81
- package/dist/OpenWork/ProviderAccessToken.js +26 -41
- package/dist/OpenWork/Server.js +50 -94
- package/dist/OpenWork/SuiteAccessToken.js +27 -42
- package/dist/OpenWork/SuiteTicket.js +14 -27
- package/dist/Pay/Application.js +1 -2
- package/dist/Pay/Client.js +63 -77
- package/dist/Pay/LegacySignature.js +2 -3
- package/dist/Pay/Merchant.js +36 -52
- package/dist/Pay/Server.js +55 -70
- package/dist/Pay/Utils.js +13 -26
- package/dist/Pay/Validator.js +28 -39
- package/dist/Types/global.d.ts +31 -0
- package/dist/Work/AccessToken.js +26 -41
- package/dist/Work/Application.js +21 -32
- package/dist/Work/JsApiTicket.js +60 -77
- package/dist/Work/Server.js +47 -86
- package/dist/Work/Utils.js +12 -25
- package/dist/index.d.ts +14 -2
- package/dist/index.js +19 -1
- package/package.json +1 -1
- package/tsconfig.json +2 -2
- package/dist/Core/Mixins/DecryptXmlMessageMixin.js +0 -38
- package/dist/Core/Mixins/ResponseXmlMessageMixin.js +0 -63
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -145,19 +136,17 @@ class Application {
|
|
|
145
136
|
* @param authorizationCode
|
|
146
137
|
* @returns
|
|
147
138
|
*/
|
|
148
|
-
getAuthorization(authorizationCode) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
authorization_code: authorizationCode,
|
|
154
|
-
}
|
|
155
|
-
})).toObject();
|
|
156
|
-
if (!response['authorization_info']) {
|
|
157
|
-
throw new Error('Failed to get authorization_info: ' + JSON.stringify(response));
|
|
139
|
+
async getAuthorization(authorizationCode) {
|
|
140
|
+
let response = (await this.getClient().request('post', 'cgi-bin/component/api_query_auth', {
|
|
141
|
+
json: {
|
|
142
|
+
component_appid: this.getAccount().getAppId(),
|
|
143
|
+
authorization_code: authorizationCode,
|
|
158
144
|
}
|
|
159
|
-
|
|
160
|
-
|
|
145
|
+
})).toObject();
|
|
146
|
+
if (!response['authorization_info']) {
|
|
147
|
+
throw new Error('Failed to get authorization_info: ' + JSON.stringify(response));
|
|
148
|
+
}
|
|
149
|
+
return new Authorization_1.default(response);
|
|
161
150
|
}
|
|
162
151
|
/**
|
|
163
152
|
* 获取/刷新接口调用令牌
|
|
@@ -166,38 +155,34 @@ class Application {
|
|
|
166
155
|
* @param authorizerRefreshToken
|
|
167
156
|
* @returns
|
|
168
157
|
*/
|
|
169
|
-
refreshAuthorizerToken(authorizerAppId, authorizerRefreshToken) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
authorizer_refresh_token: authorizerRefreshToken,
|
|
176
|
-
}
|
|
177
|
-
})).toObject();
|
|
178
|
-
if (!response['authorizer_access_token']) {
|
|
179
|
-
throw new Error('Failed to get authorizer_access_token: ' + JSON.stringify(response));
|
|
158
|
+
async refreshAuthorizerToken(authorizerAppId, authorizerRefreshToken) {
|
|
159
|
+
let response = (await this.getClient().request('post', 'cgi-bin/component/api_authorizer_token', {
|
|
160
|
+
json: {
|
|
161
|
+
component_appid: this.getAccount().getAppId(),
|
|
162
|
+
authorizer_appid: authorizerAppId,
|
|
163
|
+
authorizer_refresh_token: authorizerRefreshToken,
|
|
180
164
|
}
|
|
181
|
-
|
|
182
|
-
|
|
165
|
+
})).toObject();
|
|
166
|
+
if (!response['authorizer_access_token']) {
|
|
167
|
+
throw new Error('Failed to get authorizer_access_token: ' + JSON.stringify(response));
|
|
168
|
+
}
|
|
169
|
+
return response;
|
|
183
170
|
}
|
|
184
171
|
/**
|
|
185
172
|
* 获取预授权码
|
|
186
173
|
* @see https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/pre_auth_code.html
|
|
187
174
|
* @returns
|
|
188
175
|
*/
|
|
189
|
-
createPreAuthorizationCode() {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
component_appid: this.getAccount().getAppId(),
|
|
194
|
-
}
|
|
195
|
-
})).toObject();
|
|
196
|
-
if (!response['pre_auth_code']) {
|
|
197
|
-
throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
|
|
176
|
+
async createPreAuthorizationCode() {
|
|
177
|
+
let response = (await this.getClient().request('post', 'cgi-bin/component/api_create_preauthcode', {
|
|
178
|
+
json: {
|
|
179
|
+
component_appid: this.getAccount().getAppId(),
|
|
198
180
|
}
|
|
199
|
-
|
|
200
|
-
|
|
181
|
+
})).toObject();
|
|
182
|
+
if (!response['pre_auth_code']) {
|
|
183
|
+
throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
|
|
184
|
+
}
|
|
185
|
+
return response;
|
|
201
186
|
}
|
|
202
187
|
/**
|
|
203
188
|
* 生成授权页地址
|
|
@@ -205,24 +190,22 @@ class Application {
|
|
|
205
190
|
* @param optional 预授权码,不传
|
|
206
191
|
* @returns
|
|
207
192
|
*/
|
|
208
|
-
createPreAuthorizationUrl(callbackUrl, optional) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
optional
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return `https://mp.weixin.qq.com/cgi-bin/componentloginpage?${(0, Utils_1.buildQueryString)(queries)}`;
|
|
225
|
-
});
|
|
193
|
+
async createPreAuthorizationUrl(callbackUrl, optional) {
|
|
194
|
+
if (typeof optional === 'string') {
|
|
195
|
+
optional = {
|
|
196
|
+
pre_auth_code: optional,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
else if (!optional || !optional['pre_auth_code']) {
|
|
200
|
+
optional = {
|
|
201
|
+
pre_auth_code: (await this.createPreAuthorizationCode()).pre_auth_code,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
let queries = (0, merge_1.default)({
|
|
205
|
+
component_appid: this.getAccount().getAppId(),
|
|
206
|
+
redirect_uri: callbackUrl,
|
|
207
|
+
}, optional);
|
|
208
|
+
return `https://mp.weixin.qq.com/cgi-bin/componentloginpage?${(0, Utils_1.buildQueryString)(queries)}`;
|
|
226
209
|
}
|
|
227
210
|
getOAuth() {
|
|
228
211
|
let oauthFactory = ((app) => {
|
|
@@ -245,10 +228,8 @@ class Application {
|
|
|
245
228
|
* @param config
|
|
246
229
|
* @returns
|
|
247
230
|
*/
|
|
248
|
-
getOfficialAccountWithRefreshToken(
|
|
249
|
-
return
|
|
250
|
-
return this.getOfficialAccountWithAccessToken(appId, yield this.getAuthorizerAccessToken(appId, refreshToken), config);
|
|
251
|
-
});
|
|
231
|
+
async getOfficialAccountWithRefreshToken(appId, refreshToken, config = {}) {
|
|
232
|
+
return this.getOfficialAccountWithAccessToken(appId, await this.getAuthorizerAccessToken(appId, refreshToken), config);
|
|
252
233
|
}
|
|
253
234
|
/**
|
|
254
235
|
* 根据访问令牌获取公众号实例
|
|
@@ -313,10 +294,8 @@ class Application {
|
|
|
313
294
|
* @param config
|
|
314
295
|
* @returns
|
|
315
296
|
*/
|
|
316
|
-
getMiniAppWithRefreshToken(
|
|
317
|
-
return
|
|
318
|
-
return this.getMiniAppWithAccessToken(appId, yield this.getAuthorizerAccessToken(appId, refreshToken), config);
|
|
319
|
-
});
|
|
297
|
+
async getMiniAppWithRefreshToken(appId, refreshToken, config = {}) {
|
|
298
|
+
return this.getMiniAppWithAccessToken(appId, await this.getAuthorizerAccessToken(appId, refreshToken), config);
|
|
320
299
|
}
|
|
321
300
|
/**
|
|
322
301
|
* 根据访问令牌获取小程序实例
|
|
@@ -361,23 +340,20 @@ class Application {
|
|
|
361
340
|
* @param refreshToken
|
|
362
341
|
* @returns
|
|
363
342
|
*/
|
|
364
|
-
getAuthorizerAccessToken(appId, refreshToken) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
let
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
376
|
-
return authorizerAccessToken;
|
|
377
|
-
});
|
|
343
|
+
async getAuthorizerAccessToken(appId, refreshToken) {
|
|
344
|
+
let md5RefreshToken = (0, Utils_1.createHash)(refreshToken, 'md5');
|
|
345
|
+
let cacheKey = `open-platform.authorizer_access_token.${appId}.${md5RefreshToken}`;
|
|
346
|
+
let cache = this.getCache();
|
|
347
|
+
let authorizerAccessToken = await cache.get(cacheKey);
|
|
348
|
+
if (!authorizerAccessToken) {
|
|
349
|
+
let response = await this.refreshAuthorizerToken(appId, refreshToken);
|
|
350
|
+
authorizerAccessToken = response['authorizer_access_token'];
|
|
351
|
+
await cache.set(cacheKey, authorizerAccessToken, (parseInt(response['expires_in']) ?? 7200) - 500);
|
|
352
|
+
}
|
|
353
|
+
return authorizerAccessToken;
|
|
378
354
|
}
|
|
379
355
|
createClient() {
|
|
380
|
-
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) =>
|
|
356
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => response.toObject()['errcode'] ?? 0, this.getConfig().get('http.throw', true)))
|
|
381
357
|
.setPresets(this.getConfig().all());
|
|
382
358
|
}
|
|
383
359
|
/**
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -24,24 +15,22 @@ class Utils extends Utils_1.default {
|
|
|
24
15
|
* @param code
|
|
25
16
|
* @returns
|
|
26
17
|
*/
|
|
27
|
-
code2Session(code) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
grant_type: 'authorization_code',
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
let data = response.toObject();
|
|
40
|
-
if (!data['openid']) {
|
|
41
|
-
throw new Error(`code2Session error: ${JSON.stringify(data)}`);
|
|
18
|
+
async code2Session(code) {
|
|
19
|
+
let client = this.app.getHttpClient();
|
|
20
|
+
let response = await client.request('GET', '/sns/component/jscode2session', {
|
|
21
|
+
params: {
|
|
22
|
+
component_access_token: await this.app.getComponentApp().getAccessToken().getToken(),
|
|
23
|
+
component_appid: this.app.getComponentApp().getAccount().getAppId(),
|
|
24
|
+
appid: this.app.getAccount().getAppId(),
|
|
25
|
+
js_code: code,
|
|
26
|
+
grant_type: 'authorization_code',
|
|
42
27
|
}
|
|
43
|
-
return data;
|
|
44
28
|
});
|
|
29
|
+
let data = response.toObject();
|
|
30
|
+
if (!data['openid']) {
|
|
31
|
+
throw new Error(`code2Session error: ${JSON.stringify(data)}`);
|
|
32
|
+
}
|
|
33
|
+
return data;
|
|
45
34
|
}
|
|
46
35
|
}
|
|
47
36
|
;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
class AuthorizerAccessToken {
|
|
12
3
|
constructor(appId, accessToken) {
|
|
13
4
|
this.appId = appId;
|
|
@@ -20,17 +11,13 @@ class AuthorizerAccessToken {
|
|
|
20
11
|
getAppId() {
|
|
21
12
|
return this.appId;
|
|
22
13
|
}
|
|
23
|
-
getToken() {
|
|
24
|
-
return
|
|
25
|
-
return this.accessToken;
|
|
26
|
-
});
|
|
14
|
+
async getToken() {
|
|
15
|
+
return this.accessToken;
|
|
27
16
|
}
|
|
28
|
-
toQuery() {
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
33
|
-
});
|
|
17
|
+
async toQuery() {
|
|
18
|
+
return {
|
|
19
|
+
'access_token': await this.getToken(),
|
|
20
|
+
};
|
|
34
21
|
}
|
|
35
22
|
/**
|
|
36
23
|
* 转为字符串
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -45,42 +36,36 @@ class ComponentAccessToken {
|
|
|
45
36
|
this.key = key;
|
|
46
37
|
return this;
|
|
47
38
|
}
|
|
48
|
-
getToken() {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return this.refresh();
|
|
58
|
-
});
|
|
39
|
+
async getToken() {
|
|
40
|
+
let token = '';
|
|
41
|
+
if (this.cache) {
|
|
42
|
+
token = await this.cache.get(this.getKey());
|
|
43
|
+
}
|
|
44
|
+
if (!!token && typeof token === 'string') {
|
|
45
|
+
return token;
|
|
46
|
+
}
|
|
47
|
+
return this.refresh();
|
|
59
48
|
}
|
|
60
|
-
toQuery() {
|
|
61
|
-
return
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
};
|
|
65
|
-
});
|
|
49
|
+
async toQuery() {
|
|
50
|
+
return {
|
|
51
|
+
component_access_token: await this.getToken(),
|
|
52
|
+
};
|
|
66
53
|
}
|
|
67
|
-
refresh() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
component_verify_ticket: yield this.verifyTicket.getTicket(),
|
|
74
|
-
}
|
|
75
|
-
})).toObject();
|
|
76
|
-
if (!response['component_access_token']) {
|
|
77
|
-
throw new Error('Failed to get component_access_token: ' + JSON.stringify(response));
|
|
78
|
-
}
|
|
79
|
-
if (this.cache) {
|
|
80
|
-
yield this.cache.set(this.getKey(), response['component_access_token'], parseInt(response['expires_in']) - 100);
|
|
54
|
+
async refresh() {
|
|
55
|
+
let response = (await this.httpClient.request('post', 'cgi-bin/component/api_component_token', {
|
|
56
|
+
data: {
|
|
57
|
+
component_appid: this.appId,
|
|
58
|
+
component_appsecret: this.secret,
|
|
59
|
+
component_verify_ticket: await this.verifyTicket.getTicket(),
|
|
81
60
|
}
|
|
82
|
-
|
|
83
|
-
|
|
61
|
+
})).toObject();
|
|
62
|
+
if (!response['component_access_token']) {
|
|
63
|
+
throw new Error('Failed to get component_access_token: ' + JSON.stringify(response));
|
|
64
|
+
}
|
|
65
|
+
if (this.cache) {
|
|
66
|
+
await this.cache.set(this.getKey(), response['component_access_token'], parseInt(response['expires_in']) - 100);
|
|
67
|
+
}
|
|
68
|
+
return response['component_access_token'];
|
|
84
69
|
}
|
|
85
70
|
}
|
|
86
71
|
module.exports = ComponentAccessToken;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -25,20 +16,18 @@ class Server extends ServerInterface_1.default {
|
|
|
25
16
|
* 服务端消息处理
|
|
26
17
|
* @returns
|
|
27
18
|
*/
|
|
28
|
-
serve() {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return response;
|
|
41
|
-
});
|
|
19
|
+
async serve() {
|
|
20
|
+
let echostr = this.request.getQueryParams()['echostr'] || '';
|
|
21
|
+
if (!!echostr) {
|
|
22
|
+
return new Response_1.default(200, { 'Content-Type': 'text/html' }, echostr);
|
|
23
|
+
}
|
|
24
|
+
let message = await this.getRequestMessage(this.request);
|
|
25
|
+
this.prepend(this.decryptRequestMessage());
|
|
26
|
+
let response = await this.handle(new Response_1.default(200, {}, 'success'), message);
|
|
27
|
+
if (!(response instanceof Response_1.default)) {
|
|
28
|
+
response = await this.transformToReply(response, message, this.encryptor);
|
|
29
|
+
}
|
|
30
|
+
return response;
|
|
42
31
|
}
|
|
43
32
|
/**
|
|
44
33
|
* 处理授权成功通知
|
|
@@ -46,10 +35,8 @@ class Server extends ServerInterface_1.default {
|
|
|
46
35
|
* @returns
|
|
47
36
|
*/
|
|
48
37
|
handleAuthorized(handler) {
|
|
49
|
-
return this.with(function (message, next) {
|
|
50
|
-
return
|
|
51
|
-
return message.InfoType === 'authorized' ? handler(message, next) : next(message);
|
|
52
|
-
});
|
|
38
|
+
return this.with(async function (message, next) {
|
|
39
|
+
return message.InfoType === 'authorized' ? handler(message, next) : next(message);
|
|
53
40
|
});
|
|
54
41
|
}
|
|
55
42
|
/**
|
|
@@ -58,10 +45,8 @@ class Server extends ServerInterface_1.default {
|
|
|
58
45
|
* @returns
|
|
59
46
|
*/
|
|
60
47
|
handleUnauthorized(handler) {
|
|
61
|
-
return this.with(function (message, next) {
|
|
62
|
-
return
|
|
63
|
-
return message.InfoType === 'unauthorized' ? handler(message, next) : next(message);
|
|
64
|
-
});
|
|
48
|
+
return this.with(async function (message, next) {
|
|
49
|
+
return message.InfoType === 'unauthorized' ? handler(message, next) : next(message);
|
|
65
50
|
});
|
|
66
51
|
}
|
|
67
52
|
/**
|
|
@@ -70,10 +55,8 @@ class Server extends ServerInterface_1.default {
|
|
|
70
55
|
* @returns
|
|
71
56
|
*/
|
|
72
57
|
handleAuthorizeUpdated(handler) {
|
|
73
|
-
return this.with(function (message, next) {
|
|
74
|
-
return
|
|
75
|
-
return message.InfoType === 'updateauthorized' ? handler(message, next) : next(message);
|
|
76
|
-
});
|
|
58
|
+
return this.with(async function (message, next) {
|
|
59
|
+
return message.InfoType === 'updateauthorized' ? handler(message, next) : next(message);
|
|
77
60
|
});
|
|
78
61
|
}
|
|
79
62
|
/**
|
|
@@ -96,10 +79,8 @@ class Server extends ServerInterface_1.default {
|
|
|
96
79
|
if (this.defaultVerifyTicketHandler) {
|
|
97
80
|
this.withoutHandler(this.defaultVerifyTicketHandler);
|
|
98
81
|
}
|
|
99
|
-
return this.with(function (message, next) {
|
|
100
|
-
return
|
|
101
|
-
return message.InfoType === 'component_verify_ticket' ? handler(message, next) : next(message);
|
|
102
|
-
});
|
|
82
|
+
return this.with(async function (message, next) {
|
|
83
|
+
return message.InfoType === 'component_verify_ticket' ? handler(message, next) : next(message);
|
|
103
84
|
});
|
|
104
85
|
}
|
|
105
86
|
/**
|
|
@@ -108,18 +89,16 @@ class Server extends ServerInterface_1.default {
|
|
|
108
89
|
* @returns
|
|
109
90
|
*/
|
|
110
91
|
handleThirdFastRegister(handler) {
|
|
111
|
-
return this.with(function (message, next) {
|
|
112
|
-
return
|
|
113
|
-
return message.InfoType === 'notify_third_fasteregister' ? handler(message, next) : next(message);
|
|
114
|
-
});
|
|
92
|
+
return this.with(async function (message, next) {
|
|
93
|
+
return message.InfoType === 'notify_third_fasteregister' ? handler(message, next) : next(message);
|
|
115
94
|
});
|
|
116
95
|
}
|
|
117
96
|
decryptRequestMessage() {
|
|
118
97
|
let query = this.request.getQueryParams();
|
|
119
|
-
return (message, next) =>
|
|
120
|
-
|
|
98
|
+
return async (message, next) => {
|
|
99
|
+
await this.decryptMessage(message, this.encryptor, query['msg_signature'] || '', query['timestamp'] || '', query['nonce'] || '');
|
|
121
100
|
return next(message);
|
|
122
|
-
}
|
|
101
|
+
};
|
|
123
102
|
}
|
|
124
103
|
/**
|
|
125
104
|
* 获取来自微信服务器的推送消息
|
|
@@ -134,14 +113,12 @@ class Server extends ServerInterface_1.default {
|
|
|
134
113
|
* @param request 未设置该参数时,则从当前服务端收到的请求中获取
|
|
135
114
|
* @returns
|
|
136
115
|
*/
|
|
137
|
-
getDecryptedMessage() {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return this.decryptMessage(message, this.encryptor, query['msg_signature'] || '', query['timestamp'] || '', query['nonce'] || '');
|
|
144
|
-
});
|
|
116
|
+
async getDecryptedMessage(request = null) {
|
|
117
|
+
if (!request)
|
|
118
|
+
request = this.request;
|
|
119
|
+
let message = await Message_1.default.createFromRequest(request);
|
|
120
|
+
let query = request.getQueryParams();
|
|
121
|
+
return this.decryptMessage(message, this.encryptor, query['msg_signature'] || '', query['timestamp'] || '', query['nonce'] || '');
|
|
145
122
|
}
|
|
146
123
|
}
|
|
147
124
|
;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
class VerifyTicket {
|
|
12
3
|
constructor(appId, key = null, cache = null) {
|
|
13
4
|
this.appId = appId;
|
|
@@ -24,25 +15,21 @@ class VerifyTicket {
|
|
|
24
15
|
this.key = key;
|
|
25
16
|
return this;
|
|
26
17
|
}
|
|
27
|
-
setTicket(ticket) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return this;
|
|
33
|
-
});
|
|
18
|
+
async setTicket(ticket) {
|
|
19
|
+
if (this.cache) {
|
|
20
|
+
await this.cache.set(this.getKey(), ticket, 6000);
|
|
21
|
+
}
|
|
22
|
+
return this;
|
|
34
23
|
}
|
|
35
|
-
getTicket() {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return ticket;
|
|
45
|
-
});
|
|
24
|
+
async getTicket() {
|
|
25
|
+
let ticket = '';
|
|
26
|
+
if (this.cache) {
|
|
27
|
+
ticket = await this.cache.get(this.getKey());
|
|
28
|
+
}
|
|
29
|
+
if (!ticket || typeof ticket != 'string') {
|
|
30
|
+
throw new Error('No component_verify_ticket found.');
|
|
31
|
+
}
|
|
32
|
+
return ticket;
|
|
46
33
|
}
|
|
47
34
|
}
|
|
48
35
|
;
|