node-easywechat 3.7.0 → 3.7.2
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 +8 -0
- package/README.md +7 -3
- 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.d.ts +6 -0
- package/dist/OfficialAccount/Server.js +39 -42
- 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.d.ts +1 -1
- package/dist/OpenPlatform/Server.js +34 -56
- 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 +54 -95
- 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 +51 -87
- package/dist/Work/Utils.js +12 -25
- package/package.json +1 -1
- package/tsconfig.json +2 -2
|
@@ -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
|
};
|
|
@@ -110,12 +101,12 @@ class Application {
|
|
|
110
101
|
getServer() {
|
|
111
102
|
if (!this.server) {
|
|
112
103
|
this.server = new Server_1.default(this.getSuiteEncryptor(), this.getEncryptor(), this.getRequest());
|
|
113
|
-
this.server.withDefaultSuiteTicketHandler((message, next) =>
|
|
104
|
+
this.server.withDefaultSuiteTicketHandler(async (message, next) => {
|
|
114
105
|
if (message.SuiteId === this.getAccount().getSuiteId()) {
|
|
115
|
-
|
|
106
|
+
await this.getSuiteTicket().setTicket(message.SuiteTicket);
|
|
116
107
|
}
|
|
117
108
|
return next(message);
|
|
118
|
-
})
|
|
109
|
+
});
|
|
119
110
|
}
|
|
120
111
|
else {
|
|
121
112
|
this.server.setRequest(this.getRequest());
|
|
@@ -184,24 +175,22 @@ class Application {
|
|
|
184
175
|
* @param suiteAccessToken
|
|
185
176
|
* @returns
|
|
186
177
|
*/
|
|
187
|
-
getAuthorization(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
permanent_code: permanentCode,
|
|
198
|
-
}
|
|
199
|
-
})).toObject();
|
|
200
|
-
if (!response['auth_corp_info']) {
|
|
201
|
-
throw new Error('Failed to get auth_corp_info: ' + JSON.stringify(response));
|
|
178
|
+
async getAuthorization(corpId, permanentCode, suiteAccessToken = null) {
|
|
179
|
+
if (!suiteAccessToken)
|
|
180
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
181
|
+
let response = (await this.getClient().request('post', 'cgi-bin/service/get_auth_info', {
|
|
182
|
+
params: {
|
|
183
|
+
suite_access_token: await suiteAccessToken.getToken(),
|
|
184
|
+
},
|
|
185
|
+
json: {
|
|
186
|
+
auth_corpid: corpId,
|
|
187
|
+
permanent_code: permanentCode,
|
|
202
188
|
}
|
|
203
|
-
|
|
204
|
-
|
|
189
|
+
})).toObject();
|
|
190
|
+
if (!response['auth_corp_info']) {
|
|
191
|
+
throw new Error('Failed to get auth_corp_info: ' + JSON.stringify(response));
|
|
192
|
+
}
|
|
193
|
+
return new Authorization_1.default(response);
|
|
205
194
|
}
|
|
206
195
|
/**
|
|
207
196
|
* 获取预授权码
|
|
@@ -209,20 +198,18 @@ class Application {
|
|
|
209
198
|
* @param suiteAccessToken
|
|
210
199
|
* @returns
|
|
211
200
|
*/
|
|
212
|
-
createPreAuthorizationCode() {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
suite_access_token: yield suiteAccessToken.getToken(),
|
|
219
|
-
}
|
|
220
|
-
})).toObject();
|
|
221
|
-
if (!response['pre_auth_code']) {
|
|
222
|
-
throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
|
|
201
|
+
async createPreAuthorizationCode(suiteAccessToken = null) {
|
|
202
|
+
if (!suiteAccessToken)
|
|
203
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
204
|
+
let response = (await this.getClient().request('get', 'cgi-bin/service/get_pre_auth_code', {
|
|
205
|
+
params: {
|
|
206
|
+
suite_access_token: await suiteAccessToken.getToken(),
|
|
223
207
|
}
|
|
224
|
-
|
|
225
|
-
|
|
208
|
+
})).toObject();
|
|
209
|
+
if (!response['pre_auth_code']) {
|
|
210
|
+
throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
|
|
211
|
+
}
|
|
212
|
+
return response;
|
|
226
213
|
}
|
|
227
214
|
/**
|
|
228
215
|
* 获取企业授权令牌
|
|
@@ -231,55 +218,45 @@ class Application {
|
|
|
231
218
|
* @param suiteAccessToken
|
|
232
219
|
* @returns
|
|
233
220
|
*/
|
|
234
|
-
getAuthorizerAccessToken(
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
return new AuthorizerAccessToken_1.default(corpId, permanentCode, suiteAccessToken, null, this.getCache(), this.getHttpClient());
|
|
239
|
-
});
|
|
221
|
+
async getAuthorizerAccessToken(corpId, permanentCode, suiteAccessToken = null) {
|
|
222
|
+
if (!suiteAccessToken)
|
|
223
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
224
|
+
return new AuthorizerAccessToken_1.default(corpId, permanentCode, suiteAccessToken, null, this.getCache(), this.getHttpClient());
|
|
240
225
|
}
|
|
241
226
|
createClient() {
|
|
242
|
-
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getProviderAccessToken(), (response) =>
|
|
227
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getProviderAccessToken(), (response) => response.toObject()['errcode'] ?? 0, this.getConfig().get('http.throw', true)))
|
|
243
228
|
.setPresets(this.getConfig().all());
|
|
244
229
|
}
|
|
245
|
-
getAuthorizerClient(
|
|
246
|
-
return
|
|
247
|
-
|
|
248
|
-
.setPresets(this.getConfig().all());
|
|
249
|
-
});
|
|
230
|
+
async getAuthorizerClient(corpId, permanentCode, suiteAccessToken = null) {
|
|
231
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), await this.getAuthorizerAccessToken(corpId, permanentCode, suiteAccessToken), (response) => response.toObject()['errcode'] ?? 0, this.getConfig().get('http.throw', true)))
|
|
232
|
+
.setPresets(this.getConfig().all());
|
|
250
233
|
}
|
|
251
|
-
getJsApiTicket(
|
|
252
|
-
return
|
|
253
|
-
return new JsApiTicket_1.default(corpId, null, this.getCache(), yield this.getAuthorizerClient(corpId, permanentCode, suiteAccessToken));
|
|
254
|
-
});
|
|
234
|
+
async getJsApiTicket(corpId, permanentCode, suiteAccessToken = null) {
|
|
235
|
+
return new JsApiTicket_1.default(corpId, null, this.getCache(), await this.getAuthorizerClient(corpId, permanentCode, suiteAccessToken));
|
|
255
236
|
}
|
|
256
|
-
getOAuth(
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
.scopes(this.getConfig().get('oauth.scopes', 'snsapi_base'));
|
|
268
|
-
});
|
|
237
|
+
async getOAuth(suiteId, suiteAccessToken = null) {
|
|
238
|
+
if (!suiteAccessToken)
|
|
239
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
240
|
+
return (new OpenWeWork_1.default({
|
|
241
|
+
client_id: suiteId,
|
|
242
|
+
client_secret: '',
|
|
243
|
+
redirect: this.getConfig().get('oauth.redirect_url'),
|
|
244
|
+
}))
|
|
245
|
+
.withSuiteTicket(await this.getSuiteTicket().getTicket())
|
|
246
|
+
.withSuiteAccessToken(await suiteAccessToken.getToken())
|
|
247
|
+
.scopes(this.getConfig().get('oauth.scopes', 'snsapi_base'));
|
|
269
248
|
}
|
|
270
|
-
getCorpOAuth(
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
.scopes(this.getConfig().get('oauth.scopes', 'snsapi_base'));
|
|
282
|
-
});
|
|
249
|
+
async getCorpOAuth(corpId, suiteAccessToken = null) {
|
|
250
|
+
if (!suiteAccessToken)
|
|
251
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
252
|
+
return (new OpenWeWork_1.default({
|
|
253
|
+
client_id: corpId,
|
|
254
|
+
client_secret: '',
|
|
255
|
+
redirect: this.getConfig().get('oauth.redirect_url'),
|
|
256
|
+
}))
|
|
257
|
+
.withSuiteTicket(await this.getSuiteTicket().getTicket())
|
|
258
|
+
.withSuiteAccessToken(await suiteAccessToken.getToken())
|
|
259
|
+
.scopes(this.getConfig().get('oauth.scopes', 'snsapi_base'));
|
|
283
260
|
}
|
|
284
261
|
/**
|
|
285
262
|
* 获取请求默认配置
|
|
@@ -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
|
};
|
|
@@ -52,47 +43,41 @@ class AuthorizerAccessToken {
|
|
|
52
43
|
this.key = key;
|
|
53
44
|
return this;
|
|
54
45
|
}
|
|
55
|
-
getToken() {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return this.refresh();
|
|
68
|
-
});
|
|
46
|
+
async getToken() {
|
|
47
|
+
if (!this.suiteAccessToken) {
|
|
48
|
+
return this.permanentCodeOrAccessToken;
|
|
49
|
+
}
|
|
50
|
+
let token = '';
|
|
51
|
+
if (this.cache) {
|
|
52
|
+
token = await this.cache.get(this.getKey());
|
|
53
|
+
}
|
|
54
|
+
if (!!token && typeof token === 'string') {
|
|
55
|
+
return token;
|
|
56
|
+
}
|
|
57
|
+
return this.refresh();
|
|
69
58
|
}
|
|
70
|
-
toQuery() {
|
|
71
|
-
return
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
};
|
|
75
|
-
});
|
|
59
|
+
async toQuery() {
|
|
60
|
+
return {
|
|
61
|
+
access_token: await this.getToken(),
|
|
62
|
+
};
|
|
76
63
|
}
|
|
77
|
-
refresh() {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
permanent_code: this.permanentCodeOrAccessToken,
|
|
86
|
-
}
|
|
87
|
-
})).toObject();
|
|
88
|
-
if (!response['access_token']) {
|
|
89
|
-
throw new Error('Failed to get access_token: ' + JSON.stringify(response));
|
|
90
|
-
}
|
|
91
|
-
if (this.cache) {
|
|
92
|
-
yield this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']) - 100);
|
|
64
|
+
async refresh() {
|
|
65
|
+
let response = (await this.httpClient.request('post', 'cgi-bin/service/get_corp_token', {
|
|
66
|
+
params: {
|
|
67
|
+
suite_access_token: await this.suiteAccessToken.getToken(),
|
|
68
|
+
},
|
|
69
|
+
json: {
|
|
70
|
+
auth_corpid: this.corpId,
|
|
71
|
+
permanent_code: this.permanentCodeOrAccessToken,
|
|
93
72
|
}
|
|
94
|
-
|
|
95
|
-
|
|
73
|
+
})).toObject();
|
|
74
|
+
if (!response['access_token']) {
|
|
75
|
+
throw new Error('Failed to get access_token: ' + JSON.stringify(response));
|
|
76
|
+
}
|
|
77
|
+
if (this.cache) {
|
|
78
|
+
await this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']) - 100);
|
|
79
|
+
}
|
|
80
|
+
return response['access_token'];
|
|
96
81
|
}
|
|
97
82
|
/**
|
|
98
83
|
* 转为字符串
|
|
@@ -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
|
};
|
|
@@ -32,22 +23,20 @@ class JsApiTicket {
|
|
|
32
23
|
* @param timestamp 时间长,默认:当前时间
|
|
33
24
|
* @returns
|
|
34
25
|
*/
|
|
35
|
-
createConfigSignature(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
50
|
-
});
|
|
26
|
+
async createConfigSignature(url, nonce = null, timestamp = null, jsApiList = [], debug = false, beta = true) {
|
|
27
|
+
nonce = nonce || (0, Utils_1.randomString)(10);
|
|
28
|
+
timestamp = timestamp || (0, Utils_1.getTimestamp)();
|
|
29
|
+
let ticket = await this.getTicket();
|
|
30
|
+
return {
|
|
31
|
+
appId: this.corpId,
|
|
32
|
+
nonceStr: nonce,
|
|
33
|
+
timestamp,
|
|
34
|
+
url,
|
|
35
|
+
signature: this.getTicketSignature(ticket, nonce, timestamp, url),
|
|
36
|
+
jsApiList,
|
|
37
|
+
debug,
|
|
38
|
+
beta,
|
|
39
|
+
};
|
|
51
40
|
}
|
|
52
41
|
getTicketSignature(ticket, nonce, timestamp, url) {
|
|
53
42
|
return (0, Utils_1.createHash)(`jsapi_ticket=${ticket}&noncestr=${nonce}×tamp=${timestamp}&url=${url}`, 'sha1');
|
|
@@ -66,25 +55,23 @@ class JsApiTicket {
|
|
|
66
55
|
* 获取签名凭证jsapi_ticket
|
|
67
56
|
* @returns
|
|
68
57
|
*/
|
|
69
|
-
getTicket() {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return response['ticket'];
|
|
87
|
-
});
|
|
58
|
+
async getTicket() {
|
|
59
|
+
let key = this.getKey();
|
|
60
|
+
let ticket = '';
|
|
61
|
+
if (this.cache) {
|
|
62
|
+
ticket = await this.cache.get(key);
|
|
63
|
+
}
|
|
64
|
+
if (!!ticket && typeof ticket === 'string') {
|
|
65
|
+
return ticket;
|
|
66
|
+
}
|
|
67
|
+
let response = (await this.httpClient.request('get', '/cgi-bin/get_jsapi_ticket', {})).toObject();
|
|
68
|
+
if (!response['ticket']) {
|
|
69
|
+
throw new Error('Failed to get jssdk_ticket: ' + JSON.stringify(response));
|
|
70
|
+
}
|
|
71
|
+
if (this.cache) {
|
|
72
|
+
await this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
|
|
73
|
+
}
|
|
74
|
+
return response['ticket'];
|
|
88
75
|
}
|
|
89
76
|
/**
|
|
90
77
|
* 获取代理应用的签名配置
|
|
@@ -94,50 +81,46 @@ class JsApiTicket {
|
|
|
94
81
|
* @param timestamp 时间长,默认:当前时间
|
|
95
82
|
* @returns
|
|
96
83
|
*/
|
|
97
|
-
createAgentConfigSignature(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
};
|
|
111
|
-
});
|
|
84
|
+
async createAgentConfigSignature(agentId, url, nonce = null, timestamp = null, jsApiList = []) {
|
|
85
|
+
nonce = nonce || (0, Utils_1.randomString)(10);
|
|
86
|
+
timestamp = timestamp || (0, Utils_1.getTimestamp)();
|
|
87
|
+
let ticket = await this.getTicket();
|
|
88
|
+
return {
|
|
89
|
+
corpid: this.corpId,
|
|
90
|
+
agentid: agentId,
|
|
91
|
+
url,
|
|
92
|
+
nonceStr: nonce,
|
|
93
|
+
timestamp,
|
|
94
|
+
signature: this.getTicketSignature(ticket, nonce, timestamp, url),
|
|
95
|
+
jsApiList,
|
|
96
|
+
};
|
|
112
97
|
}
|
|
113
98
|
/**
|
|
114
99
|
* 获取代理应用的签名凭证jsapi_ticket
|
|
115
100
|
* @param agentId 代理应用的id
|
|
116
101
|
* @returns
|
|
117
102
|
*/
|
|
118
|
-
getAgentTicket(agentId) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
type: 'agent_config',
|
|
131
|
-
}
|
|
132
|
-
})).toObject();
|
|
133
|
-
if (!response['ticket']) {
|
|
134
|
-
throw new Error('Failed to get jssdk agentTicket: ' + JSON.stringify(response));
|
|
135
|
-
}
|
|
136
|
-
if (this.cache) {
|
|
137
|
-
yield this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
|
|
103
|
+
async getAgentTicket(agentId) {
|
|
104
|
+
let key = this.getAgentKey(agentId);
|
|
105
|
+
let ticket = '';
|
|
106
|
+
if (this.cache) {
|
|
107
|
+
ticket = await this.cache.get(key);
|
|
108
|
+
}
|
|
109
|
+
if (!!ticket && typeof ticket === 'string') {
|
|
110
|
+
return ticket;
|
|
111
|
+
}
|
|
112
|
+
let response = (await this.httpClient.request('get', '/cgi-bin/ticket/get', {
|
|
113
|
+
params: {
|
|
114
|
+
type: 'agent_config',
|
|
138
115
|
}
|
|
139
|
-
|
|
140
|
-
|
|
116
|
+
})).toObject();
|
|
117
|
+
if (!response['ticket']) {
|
|
118
|
+
throw new Error('Failed to get jssdk agentTicket: ' + JSON.stringify(response));
|
|
119
|
+
}
|
|
120
|
+
if (this.cache) {
|
|
121
|
+
await this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
|
|
122
|
+
}
|
|
123
|
+
return response['ticket'];
|
|
141
124
|
}
|
|
142
125
|
/**
|
|
143
126
|
* 获取代理应用的jsapi_ticket的缓存名称
|
|
@@ -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
|
};
|
|
@@ -44,41 +35,35 @@ class ProviderAccessToken {
|
|
|
44
35
|
this.key = key;
|
|
45
36
|
return this;
|
|
46
37
|
}
|
|
47
|
-
getToken() {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return this.refresh();
|
|
57
|
-
});
|
|
38
|
+
async getToken() {
|
|
39
|
+
let token = '';
|
|
40
|
+
if (this.cache) {
|
|
41
|
+
token = await this.cache.get(this.getKey());
|
|
42
|
+
}
|
|
43
|
+
if (!!token && typeof token === 'string') {
|
|
44
|
+
return token;
|
|
45
|
+
}
|
|
46
|
+
return this.refresh();
|
|
58
47
|
}
|
|
59
|
-
toQuery() {
|
|
60
|
-
return
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
};
|
|
64
|
-
});
|
|
48
|
+
async toQuery() {
|
|
49
|
+
return {
|
|
50
|
+
provider_access_token: await this.getToken(),
|
|
51
|
+
};
|
|
65
52
|
}
|
|
66
|
-
refresh() {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
provider_secret: this.providerSecret,
|
|
72
|
-
}
|
|
73
|
-
})).toObject();
|
|
74
|
-
if (!response['provider_access_token']) {
|
|
75
|
-
throw new Error('Failed to get provider_access_token: ' + JSON.stringify(response));
|
|
76
|
-
}
|
|
77
|
-
if (this.cache) {
|
|
78
|
-
yield this.cache.set(this.getKey(), response['provider_access_token'], parseInt(response['expires_in']) - 100);
|
|
53
|
+
async refresh() {
|
|
54
|
+
let response = (await this.httpClient.request('post', 'cgi-bin/service/get_provider_token', {
|
|
55
|
+
json: {
|
|
56
|
+
corpid: this.corpId,
|
|
57
|
+
provider_secret: this.providerSecret,
|
|
79
58
|
}
|
|
80
|
-
|
|
81
|
-
|
|
59
|
+
})).toObject();
|
|
60
|
+
if (!response['provider_access_token']) {
|
|
61
|
+
throw new Error('Failed to get provider_access_token: ' + JSON.stringify(response));
|
|
62
|
+
}
|
|
63
|
+
if (this.cache) {
|
|
64
|
+
await this.cache.set(this.getKey(), response['provider_access_token'], parseInt(response['expires_in']) - 100);
|
|
65
|
+
}
|
|
66
|
+
return response['provider_access_token'];
|
|
82
67
|
}
|
|
83
68
|
}
|
|
84
69
|
module.exports = ProviderAccessToken;
|