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
|
@@ -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
|
;
|
|
@@ -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
|
* 获取请求默认配置
|