node-easywechat 3.7.0 → 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 +4 -0
- package/dist/Channel/Application.js +1 -1
- 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.js +1 -12
- 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/DecryptMessageMixin.js +13 -24
- package/dist/Core/Mixins/HandlersMixin.js +18 -31
- package/dist/Core/Mixins/ResponseMessageMixin.js +31 -44
- 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.js +25 -40
- 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/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/package.json +1 -1
- package/tsconfig.json +2 -2
package/dist/MiniApp/Utils.js
CHANGED
|
@@ -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
|
};
|
|
@@ -23,22 +14,20 @@ class Utils {
|
|
|
23
14
|
* @param code
|
|
24
15
|
* @returns
|
|
25
16
|
*/
|
|
26
|
-
codeToSession(code) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
grant_type: 'authorization_code',
|
|
35
|
-
}
|
|
36
|
-
})).toObject();
|
|
37
|
-
if (!response['openid']) {
|
|
38
|
-
throw new Error(`code2Session error: ${JSON.stringify(response)}`);
|
|
17
|
+
async codeToSession(code) {
|
|
18
|
+
let client = this.app.getHttpClient();
|
|
19
|
+
let response = await (await client.request('GET', '/sns/jscode2session', {
|
|
20
|
+
params: {
|
|
21
|
+
appid: this.app.getAccount().getAppId(),
|
|
22
|
+
secret: this.app.getAccount().getSecret(),
|
|
23
|
+
js_code: code,
|
|
24
|
+
grant_type: 'authorization_code',
|
|
39
25
|
}
|
|
40
|
-
|
|
41
|
-
|
|
26
|
+
})).toObject();
|
|
27
|
+
if (!response['openid']) {
|
|
28
|
+
throw new Error(`code2Session error: ${JSON.stringify(response)}`);
|
|
29
|
+
}
|
|
30
|
+
return response;
|
|
42
31
|
}
|
|
43
32
|
/**
|
|
44
33
|
* 数据解密
|
|
@@ -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
|
};
|
|
@@ -49,24 +40,20 @@ class AccessToken {
|
|
|
49
40
|
this.key = key;
|
|
50
41
|
return this;
|
|
51
42
|
}
|
|
52
|
-
getToken() {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return this.refresh();
|
|
62
|
-
});
|
|
43
|
+
async getToken() {
|
|
44
|
+
let token = '';
|
|
45
|
+
if (this.cache) {
|
|
46
|
+
token = await this.cache.get(this.getKey());
|
|
47
|
+
}
|
|
48
|
+
if (!!token && typeof token === 'string') {
|
|
49
|
+
return token;
|
|
50
|
+
}
|
|
51
|
+
return this.refresh();
|
|
63
52
|
}
|
|
64
|
-
toQuery() {
|
|
65
|
-
return
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
};
|
|
69
|
-
});
|
|
53
|
+
async toQuery() {
|
|
54
|
+
return {
|
|
55
|
+
access_token: await this.getToken(),
|
|
56
|
+
};
|
|
70
57
|
}
|
|
71
58
|
refresh() {
|
|
72
59
|
return this.stable ? this.getStableAccessToken() : this.getAccessToken();
|
|
@@ -75,45 +62,41 @@ class AccessToken {
|
|
|
75
62
|
* 获取稳定版接口调用凭据
|
|
76
63
|
* @param forceRefresh 是否强制刷新,默认:false
|
|
77
64
|
*/
|
|
78
|
-
getStableAccessToken() {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
force_refresh: forceRefresh,
|
|
86
|
-
}
|
|
87
|
-
})).toObject();
|
|
88
|
-
if (!response['access_token']) {
|
|
89
|
-
throw new Error('Failed to get stable access_token: ' + JSON.stringify(response));
|
|
90
|
-
}
|
|
91
|
-
if (this.cache) {
|
|
92
|
-
yield this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
|
|
65
|
+
async getStableAccessToken(forceRefresh = false) {
|
|
66
|
+
let response = (await this.httpClient.request('post', 'cgi-bin/stable_token', {
|
|
67
|
+
json: {
|
|
68
|
+
grant_type: 'client_credential',
|
|
69
|
+
appid: this.appId,
|
|
70
|
+
secret: this.secret,
|
|
71
|
+
force_refresh: forceRefresh,
|
|
93
72
|
}
|
|
94
|
-
|
|
95
|
-
|
|
73
|
+
})).toObject();
|
|
74
|
+
if (!response['access_token']) {
|
|
75
|
+
throw new Error('Failed to get stable access_token: ' + JSON.stringify(response));
|
|
76
|
+
}
|
|
77
|
+
if (this.cache) {
|
|
78
|
+
await this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
|
|
79
|
+
}
|
|
80
|
+
return response['access_token'];
|
|
96
81
|
}
|
|
97
82
|
/**
|
|
98
83
|
* 获取接口调用凭据
|
|
99
84
|
*/
|
|
100
|
-
getAccessToken() {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
secret: this.secret,
|
|
107
|
-
}
|
|
108
|
-
})).toObject();
|
|
109
|
-
if (!response['access_token']) {
|
|
110
|
-
throw new Error('Failed to get access_token: ' + JSON.stringify(response));
|
|
111
|
-
}
|
|
112
|
-
if (this.cache) {
|
|
113
|
-
yield this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
|
|
85
|
+
async getAccessToken() {
|
|
86
|
+
let response = (await this.httpClient.request('get', 'cgi-bin/token', {
|
|
87
|
+
params: {
|
|
88
|
+
grant_type: 'client_credential',
|
|
89
|
+
appid: this.appId,
|
|
90
|
+
secret: this.secret,
|
|
114
91
|
}
|
|
115
|
-
|
|
116
|
-
|
|
92
|
+
})).toObject();
|
|
93
|
+
if (!response['access_token']) {
|
|
94
|
+
throw new Error('Failed to get access_token: ' + JSON.stringify(response));
|
|
95
|
+
}
|
|
96
|
+
if (this.cache) {
|
|
97
|
+
await this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
|
|
98
|
+
}
|
|
99
|
+
return response['access_token'];
|
|
117
100
|
}
|
|
118
101
|
}
|
|
119
102
|
module.exports = AccessToken;
|
|
@@ -156,7 +156,7 @@ class Application {
|
|
|
156
156
|
return this.utils;
|
|
157
157
|
}
|
|
158
158
|
createClient() {
|
|
159
|
-
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) =>
|
|
159
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => response.toObject()['errcode'] ?? 0, this.getConfig().get('http.throw', true)))
|
|
160
160
|
.setPresets(this.getConfig().all());
|
|
161
161
|
}
|
|
162
162
|
/**
|
|
@@ -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
|
};
|
|
@@ -28,29 +19,27 @@ class JsApiTicket extends AccessToken_1.default {
|
|
|
28
19
|
* 获取签名凭证jsapi_ticket
|
|
29
20
|
* @returns
|
|
30
21
|
*/
|
|
31
|
-
getTicket() {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
type: 'jsapi',
|
|
44
|
-
}
|
|
45
|
-
})).toObject();
|
|
46
|
-
if (!response['ticket']) {
|
|
47
|
-
throw new Error('Failed to get jssdk_ticket: ' + JSON.stringify(response));
|
|
48
|
-
}
|
|
49
|
-
if (this.cache) {
|
|
50
|
-
yield this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
|
|
22
|
+
async getTicket() {
|
|
23
|
+
let key = this.getKey();
|
|
24
|
+
let ticket = '';
|
|
25
|
+
if (this.cache) {
|
|
26
|
+
ticket = await this.cache.get(key);
|
|
27
|
+
}
|
|
28
|
+
if (!!ticket && typeof ticket === 'string') {
|
|
29
|
+
return ticket;
|
|
30
|
+
}
|
|
31
|
+
let response = (await this.httpClient.request('get', '/cgi-bin/ticket/getticket', {
|
|
32
|
+
params: {
|
|
33
|
+
type: 'jsapi',
|
|
51
34
|
}
|
|
52
|
-
|
|
53
|
-
|
|
35
|
+
})).toObject();
|
|
36
|
+
if (!response['ticket']) {
|
|
37
|
+
throw new Error('Failed to get jssdk_ticket: ' + JSON.stringify(response));
|
|
38
|
+
}
|
|
39
|
+
if (this.cache) {
|
|
40
|
+
await this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
|
|
41
|
+
}
|
|
42
|
+
return response['ticket'];
|
|
54
43
|
}
|
|
55
44
|
/**
|
|
56
45
|
* 获取签名配置
|
|
@@ -59,19 +48,17 @@ class JsApiTicket extends AccessToken_1.default {
|
|
|
59
48
|
* @param timestamp 时间长,默认:当前时间
|
|
60
49
|
* @returns
|
|
61
50
|
*/
|
|
62
|
-
configSignature(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
};
|
|
74
|
-
});
|
|
51
|
+
async configSignature(url, nonce = null, timestamp = null) {
|
|
52
|
+
nonce = nonce || (0, Utils_1.randomString)(10);
|
|
53
|
+
timestamp = timestamp || (0, Utils_1.getTimestamp)();
|
|
54
|
+
let ticket = await this.getTicket();
|
|
55
|
+
return {
|
|
56
|
+
url,
|
|
57
|
+
nonceStr: nonce,
|
|
58
|
+
timestamp,
|
|
59
|
+
appId: this.appId,
|
|
60
|
+
signature: this.getTicketSignature(ticket, nonce, timestamp, url),
|
|
61
|
+
};
|
|
75
62
|
}
|
|
76
63
|
getTicketSignature(ticket, nonce, timestamp, url) {
|
|
77
64
|
return (0, Utils_1.createHash)(`jsapi_ticket=${ticket}&noncestr=${nonce}×tamp=${timestamp}&url=${url}`, 'sha1');
|
|
@@ -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,26 +15,24 @@ class Server extends ServerInterface_1.default {
|
|
|
24
15
|
* 服务端消息处理
|
|
25
16
|
* @returns
|
|
26
17
|
*/
|
|
27
|
-
serve() {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return response;
|
|
46
|
-
});
|
|
18
|
+
async serve() {
|
|
19
|
+
let echostr = this.request.getQueryParams()['echostr'] || '';
|
|
20
|
+
if (!!echostr) {
|
|
21
|
+
return new Response_1.default(200, { 'Content-Type': 'text/html' }, echostr);
|
|
22
|
+
}
|
|
23
|
+
let message = await this.getRequestMessage(this.request);
|
|
24
|
+
let query = this.request.getQueryParams();
|
|
25
|
+
if (this.encryptor && query['msg_signature']) {
|
|
26
|
+
this.prepend(this.decryptRequestMessage(query));
|
|
27
|
+
}
|
|
28
|
+
let response = await this.handle(new Response_1.default(200, {}, 'success'), message);
|
|
29
|
+
if (!(response instanceof Response_1.default)) {
|
|
30
|
+
const contentType = this.request.getHeader('content-type');
|
|
31
|
+
const contentBody = this.request.getBody().toString();
|
|
32
|
+
const isXml = (contentType && contentType.indexOf('xml') > -1) || contentBody.substring(0, 1) === '<';
|
|
33
|
+
response = await this.transformToReply(response, message, this.encryptor, isXml);
|
|
34
|
+
}
|
|
35
|
+
return response;
|
|
47
36
|
}
|
|
48
37
|
/**
|
|
49
38
|
* 添加普通消息处理器
|
|
@@ -52,10 +41,8 @@ class Server extends ServerInterface_1.default {
|
|
|
52
41
|
* @returns
|
|
53
42
|
*/
|
|
54
43
|
addMessageListener(type, handler) {
|
|
55
|
-
return this.withHandler(function (message, next) {
|
|
56
|
-
return
|
|
57
|
-
return message.MsgType === type ? handler(message, next) : next(message);
|
|
58
|
-
});
|
|
44
|
+
return this.withHandler(async function (message, next) {
|
|
45
|
+
return message.MsgType === type ? handler(message, next) : next(message);
|
|
59
46
|
});
|
|
60
47
|
}
|
|
61
48
|
/**
|
|
@@ -65,10 +52,8 @@ class Server extends ServerInterface_1.default {
|
|
|
65
52
|
* @returns
|
|
66
53
|
*/
|
|
67
54
|
addEventListener(event, handler) {
|
|
68
|
-
return this.withHandler(function (message, next) {
|
|
69
|
-
return
|
|
70
|
-
return message.Event === event ? handler(message, next) : next(message);
|
|
71
|
-
});
|
|
55
|
+
return this.withHandler(async function (message, next) {
|
|
56
|
+
return message.Event === event ? handler(message, next) : next(message);
|
|
72
57
|
});
|
|
73
58
|
}
|
|
74
59
|
/**
|
|
@@ -80,12 +65,12 @@ class Server extends ServerInterface_1.default {
|
|
|
80
65
|
return Message_1.default.createFromRequest(request || this.request);
|
|
81
66
|
}
|
|
82
67
|
decryptRequestMessage(query) {
|
|
83
|
-
return (message, next) =>
|
|
68
|
+
return async (message, next) => {
|
|
84
69
|
if (!this.encryptor)
|
|
85
70
|
return null;
|
|
86
|
-
|
|
71
|
+
await this.decryptMessage(message, this.encryptor, query['msg_signature'] || '', query['timestamp'] || '', query['nonce'] || '');
|
|
87
72
|
return next(message);
|
|
88
|
-
}
|
|
73
|
+
};
|
|
89
74
|
}
|
|
90
75
|
}
|
|
91
76
|
;
|
|
@@ -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,14 +15,12 @@ class Utils {
|
|
|
24
15
|
* @param debug 是否开启调试模式,默认:false
|
|
25
16
|
* @returns
|
|
26
17
|
*/
|
|
27
|
-
buildJsSdkConfig(
|
|
28
|
-
return
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}, yield this.app.getTicket().configSignature(url));
|
|
34
|
-
});
|
|
18
|
+
async buildJsSdkConfig(url, jsApiList = [], openTagList = [], debug = false) {
|
|
19
|
+
return (0, merge_1.default)({
|
|
20
|
+
jsApiList,
|
|
21
|
+
openTagList,
|
|
22
|
+
debug,
|
|
23
|
+
}, await this.app.getTicket().configSignature(url));
|
|
35
24
|
}
|
|
36
25
|
}
|
|
37
26
|
;
|
|
@@ -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
|
/**
|