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
package/dist/OpenWork/Server.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
|
};
|
|
@@ -26,22 +17,19 @@ class Server extends ServerInterface_1.default {
|
|
|
26
17
|
* 服务端消息处理
|
|
27
18
|
* @returns
|
|
28
19
|
*/
|
|
29
|
-
serve() {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
let
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
return response;
|
|
44
|
-
});
|
|
20
|
+
async serve() {
|
|
21
|
+
let query = this.request.getQueryParams();
|
|
22
|
+
if (!!query['echostr']) {
|
|
23
|
+
let echostr = this.providerEncryptor.decrypt(query['echostr'], query['msg_signature'] ?? '', query['nonce'] ?? '', query['timestamp'] ?? '');
|
|
24
|
+
return new Response_1.default(200, { 'Content-Type': 'text/html' }, echostr);
|
|
25
|
+
}
|
|
26
|
+
let message = await this.getRequestMessage(this.request);
|
|
27
|
+
this.prepend(this.decryptRequestMessage(query));
|
|
28
|
+
let response = await this.handle(new Response_1.default(200, {}, 'success'), message);
|
|
29
|
+
if (!(response instanceof Response_1.default)) {
|
|
30
|
+
response = await this.transformToReply(response, message, this.encryptor);
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
45
33
|
}
|
|
46
34
|
withDefaultSuiteTicketHandler(handler) {
|
|
47
35
|
this.defaultSuiteTicketHandler = function () {
|
|
@@ -57,10 +45,8 @@ class Server extends ServerInterface_1.default {
|
|
|
57
45
|
if (this.defaultSuiteTicketHandler) {
|
|
58
46
|
this.withoutHandler(this.defaultSuiteTicketHandler);
|
|
59
47
|
}
|
|
60
|
-
return this.with(function (message, next) {
|
|
61
|
-
return
|
|
62
|
-
return message.InfoType === 'suite_ticket' ? handler(message, next) : next(message);
|
|
63
|
-
});
|
|
48
|
+
return this.with(async function (message, next) {
|
|
49
|
+
return message.InfoType === 'suite_ticket' ? handler(message, next) : next(message);
|
|
64
50
|
});
|
|
65
51
|
}
|
|
66
52
|
/**
|
|
@@ -68,10 +54,8 @@ class Server extends ServerInterface_1.default {
|
|
|
68
54
|
* @param handler
|
|
69
55
|
*/
|
|
70
56
|
handleAuthCreated(handler) {
|
|
71
|
-
return this.with(function (message, next) {
|
|
72
|
-
return
|
|
73
|
-
return message.InfoType === 'create_auth' ? handler(message, next) : next(message);
|
|
74
|
-
});
|
|
57
|
+
return this.with(async function (message, next) {
|
|
58
|
+
return message.InfoType === 'create_auth' ? handler(message, next) : next(message);
|
|
75
59
|
});
|
|
76
60
|
}
|
|
77
61
|
/**
|
|
@@ -79,10 +63,8 @@ class Server extends ServerInterface_1.default {
|
|
|
79
63
|
* @param handler
|
|
80
64
|
*/
|
|
81
65
|
handleAuthUpdated(handler) {
|
|
82
|
-
return this.with(function (message, next) {
|
|
83
|
-
return
|
|
84
|
-
return message.InfoType === 'change_auth' ? handler(message, next) : next(message);
|
|
85
|
-
});
|
|
66
|
+
return this.with(async function (message, next) {
|
|
67
|
+
return message.InfoType === 'change_auth' ? handler(message, next) : next(message);
|
|
86
68
|
});
|
|
87
69
|
}
|
|
88
70
|
/**
|
|
@@ -90,10 +72,8 @@ class Server extends ServerInterface_1.default {
|
|
|
90
72
|
* @param handler
|
|
91
73
|
*/
|
|
92
74
|
handleAuthCancelled(handler) {
|
|
93
|
-
return this.with(function (message, next) {
|
|
94
|
-
return
|
|
95
|
-
return message.InfoType === 'cancel_auth' ? handler(message, next) : next(message);
|
|
96
|
-
});
|
|
75
|
+
return this.with(async function (message, next) {
|
|
76
|
+
return message.InfoType === 'cancel_auth' ? handler(message, next) : next(message);
|
|
97
77
|
});
|
|
98
78
|
}
|
|
99
79
|
/**
|
|
@@ -101,10 +81,8 @@ class Server extends ServerInterface_1.default {
|
|
|
101
81
|
* @param handler
|
|
102
82
|
*/
|
|
103
83
|
handleUserCreated(handler) {
|
|
104
|
-
return this.with(function (message, next) {
|
|
105
|
-
return
|
|
106
|
-
return message.InfoType === 'change_contact' && message.ChangeType === 'create_user' ? handler(message, next) : next(message);
|
|
107
|
-
});
|
|
84
|
+
return this.with(async function (message, next) {
|
|
85
|
+
return message.InfoType === 'change_contact' && message.ChangeType === 'create_user' ? handler(message, next) : next(message);
|
|
108
86
|
});
|
|
109
87
|
}
|
|
110
88
|
/**
|
|
@@ -112,10 +90,8 @@ class Server extends ServerInterface_1.default {
|
|
|
112
90
|
* @param handler
|
|
113
91
|
*/
|
|
114
92
|
handleUserUpdated(handler) {
|
|
115
|
-
return this.with(function (message, next) {
|
|
116
|
-
return
|
|
117
|
-
return message.InfoType === 'change_contact' && message.ChangeType === 'update_user' ? handler(message, next) : next(message);
|
|
118
|
-
});
|
|
93
|
+
return this.with(async function (message, next) {
|
|
94
|
+
return message.InfoType === 'change_contact' && message.ChangeType === 'update_user' ? handler(message, next) : next(message);
|
|
119
95
|
});
|
|
120
96
|
}
|
|
121
97
|
/**
|
|
@@ -123,10 +99,8 @@ class Server extends ServerInterface_1.default {
|
|
|
123
99
|
* @param handler
|
|
124
100
|
*/
|
|
125
101
|
handleUserDeleted(handler) {
|
|
126
|
-
return this.with(function (message, next) {
|
|
127
|
-
return
|
|
128
|
-
return message.InfoType === 'change_contact' && message.ChangeType === 'delete_user' ? handler(message, next) : next(message);
|
|
129
|
-
});
|
|
102
|
+
return this.with(async function (message, next) {
|
|
103
|
+
return message.InfoType === 'change_contact' && message.ChangeType === 'delete_user' ? handler(message, next) : next(message);
|
|
130
104
|
});
|
|
131
105
|
}
|
|
132
106
|
/**
|
|
@@ -134,10 +108,8 @@ class Server extends ServerInterface_1.default {
|
|
|
134
108
|
* @param handler
|
|
135
109
|
*/
|
|
136
110
|
handlePartyCreated(handler) {
|
|
137
|
-
return this.with(function (message, next) {
|
|
138
|
-
return
|
|
139
|
-
return message.InfoType === 'change_contact' && message.ChangeType === 'create_party' ? handler(message, next) : next(message);
|
|
140
|
-
});
|
|
111
|
+
return this.with(async function (message, next) {
|
|
112
|
+
return message.InfoType === 'change_contact' && message.ChangeType === 'create_party' ? handler(message, next) : next(message);
|
|
141
113
|
});
|
|
142
114
|
}
|
|
143
115
|
/**
|
|
@@ -145,10 +117,8 @@ class Server extends ServerInterface_1.default {
|
|
|
145
117
|
* @param handler
|
|
146
118
|
*/
|
|
147
119
|
handlePartyUpdated(handler) {
|
|
148
|
-
return this.with(function (message, next) {
|
|
149
|
-
return
|
|
150
|
-
return message.InfoType === 'change_contact' && message.ChangeType === 'update_party' ? handler(message, next) : next(message);
|
|
151
|
-
});
|
|
120
|
+
return this.with(async function (message, next) {
|
|
121
|
+
return message.InfoType === 'change_contact' && message.ChangeType === 'update_party' ? handler(message, next) : next(message);
|
|
152
122
|
});
|
|
153
123
|
}
|
|
154
124
|
/**
|
|
@@ -156,10 +126,8 @@ class Server extends ServerInterface_1.default {
|
|
|
156
126
|
* @param handler
|
|
157
127
|
*/
|
|
158
128
|
handlePartyDeleted(handler) {
|
|
159
|
-
return this.with(function (message, next) {
|
|
160
|
-
return
|
|
161
|
-
return message.InfoType === 'change_contact' && message.ChangeType === 'delete_party' ? handler(message, next) : next(message);
|
|
162
|
-
});
|
|
129
|
+
return this.with(async function (message, next) {
|
|
130
|
+
return message.InfoType === 'change_contact' && message.ChangeType === 'delete_party' ? handler(message, next) : next(message);
|
|
163
131
|
});
|
|
164
132
|
}
|
|
165
133
|
/**
|
|
@@ -167,10 +135,8 @@ class Server extends ServerInterface_1.default {
|
|
|
167
135
|
* @param handler
|
|
168
136
|
*/
|
|
169
137
|
handleUserTagUpdated(handler) {
|
|
170
|
-
return this.with(function (message, next) {
|
|
171
|
-
return
|
|
172
|
-
return message.InfoType === 'change_contact' && message.ChangeType === 'update_tag' ? handler(message, next) : next(message);
|
|
173
|
-
});
|
|
138
|
+
return this.with(async function (message, next) {
|
|
139
|
+
return message.InfoType === 'change_contact' && message.ChangeType === 'update_tag' ? handler(message, next) : next(message);
|
|
174
140
|
});
|
|
175
141
|
}
|
|
176
142
|
/**
|
|
@@ -178,10 +144,8 @@ class Server extends ServerInterface_1.default {
|
|
|
178
144
|
* @param handler
|
|
179
145
|
*/
|
|
180
146
|
handleShareAgentChanged(handler) {
|
|
181
|
-
return this.with(function (message, next) {
|
|
182
|
-
return
|
|
183
|
-
return message.InfoType === 'share_agent_change' ? handler(message, next) : next(message);
|
|
184
|
-
});
|
|
147
|
+
return this.with(async function (message, next) {
|
|
148
|
+
return message.InfoType === 'share_agent_change' ? handler(message, next) : next(message);
|
|
185
149
|
});
|
|
186
150
|
}
|
|
187
151
|
/**
|
|
@@ -189,10 +153,8 @@ class Server extends ServerInterface_1.default {
|
|
|
189
153
|
* @param handler
|
|
190
154
|
*/
|
|
191
155
|
handleResetPermanentCode(handler) {
|
|
192
|
-
return this.with(function (message, next) {
|
|
193
|
-
return
|
|
194
|
-
return message.InfoType === 'reset_permanent_code' ? handler(message, next) : next(message);
|
|
195
|
-
});
|
|
156
|
+
return this.with(async function (message, next) {
|
|
157
|
+
return message.InfoType === 'reset_permanent_code' ? handler(message, next) : next(message);
|
|
196
158
|
});
|
|
197
159
|
}
|
|
198
160
|
/**
|
|
@@ -200,10 +162,8 @@ class Server extends ServerInterface_1.default {
|
|
|
200
162
|
* @param handler
|
|
201
163
|
*/
|
|
202
164
|
handleChangeAppAdmin(handler) {
|
|
203
|
-
return this.with(function (message, next) {
|
|
204
|
-
return
|
|
205
|
-
return message.MsgType === 'event' && message.Event === 'change_app_admin' ? handler(message, next) : next(message);
|
|
206
|
-
});
|
|
165
|
+
return this.with(async function (message, next) {
|
|
166
|
+
return message.MsgType === 'event' && message.Event === 'change_app_admin' ? handler(message, next) : next(message);
|
|
207
167
|
});
|
|
208
168
|
}
|
|
209
169
|
/**
|
|
@@ -212,30 +172,26 @@ class Server extends ServerInterface_1.default {
|
|
|
212
172
|
* @returns
|
|
213
173
|
*/
|
|
214
174
|
getRequestMessage(request = null) {
|
|
215
|
-
return Message_1.default.createFromRequest(request
|
|
175
|
+
return Message_1.default.createFromRequest(request ?? this.request);
|
|
216
176
|
}
|
|
217
177
|
decryptRequestMessage(query) {
|
|
218
|
-
return (message, next) =>
|
|
219
|
-
var _a, _b, _c;
|
|
178
|
+
return async (message, next) => {
|
|
220
179
|
if (!this.encryptor)
|
|
221
180
|
return null;
|
|
222
|
-
|
|
181
|
+
await this.decryptMessage(message, this.encryptor, query['msg_signature'] ?? '', query['timestamp'] ?? '', query['nonce'] ?? '');
|
|
223
182
|
return next(message);
|
|
224
|
-
}
|
|
183
|
+
};
|
|
225
184
|
}
|
|
226
185
|
/**
|
|
227
186
|
* 获取解密后的消息
|
|
228
187
|
* @param request 未设置该参数时,则从当前服务端收到的请求中获取
|
|
229
188
|
* @returns
|
|
230
189
|
*/
|
|
231
|
-
getDecryptedMessage() {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
let query = request.getQueryParams();
|
|
237
|
-
return yield this.decryptMessage(message, this.encryptor, (_a = query['msg_signature']) !== null && _a !== void 0 ? _a : '', (_b = query['timestamp']) !== null && _b !== void 0 ? _b : '', (_c = query['nonce']) !== null && _c !== void 0 ? _c : '');
|
|
238
|
-
});
|
|
190
|
+
async getDecryptedMessage(request = null) {
|
|
191
|
+
request = request ?? this.request;
|
|
192
|
+
let message = await this.getRequestMessage(request);
|
|
193
|
+
let query = request.getQueryParams();
|
|
194
|
+
return await this.decryptMessage(message, this.encryptor, query['msg_signature'] ?? '', query['timestamp'] ?? '', query['nonce'] ?? '');
|
|
239
195
|
}
|
|
240
196
|
}
|
|
241
197
|
;
|
|
@@ -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 SuiteAccessToken {
|
|
|
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
|
+
suite_access_token: await this.getToken(),
|
|
52
|
+
};
|
|
66
53
|
}
|
|
67
|
-
refresh() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
suite_ticket: yield this.suiteTicket.getTicket(),
|
|
74
|
-
}
|
|
75
|
-
})).toObject();
|
|
76
|
-
if (!response['suite_access_token']) {
|
|
77
|
-
throw new Error('Failed to get suite_access_token: ' + JSON.stringify(response));
|
|
78
|
-
}
|
|
79
|
-
if (this.cache) {
|
|
80
|
-
yield this.cache.set(this.getKey(), response['suite_access_token'], parseInt(response['expires_in']) - 100);
|
|
54
|
+
async refresh() {
|
|
55
|
+
let response = (await this.httpClient.request('post', 'cgi-bin/service/get_suite_token', {
|
|
56
|
+
json: {
|
|
57
|
+
suite_id: this.suiteId,
|
|
58
|
+
suite_secret: this.suiteSecret,
|
|
59
|
+
suite_ticket: await this.suiteTicket.getTicket(),
|
|
81
60
|
}
|
|
82
|
-
|
|
83
|
-
|
|
61
|
+
})).toObject();
|
|
62
|
+
if (!response['suite_access_token']) {
|
|
63
|
+
throw new Error('Failed to get suite_access_token: ' + JSON.stringify(response));
|
|
64
|
+
}
|
|
65
|
+
if (this.cache) {
|
|
66
|
+
await this.cache.set(this.getKey(), response['suite_access_token'], parseInt(response['expires_in']) - 100);
|
|
67
|
+
}
|
|
68
|
+
return response['suite_access_token'];
|
|
84
69
|
}
|
|
85
70
|
}
|
|
86
71
|
module.exports = SuiteAccessToken;
|
|
@@ -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 SuiteTicket {
|
|
12
3
|
constructor(suiteId, cache = null, key = null) {
|
|
13
4
|
this.suiteId = suiteId;
|
|
@@ -24,25 +15,21 @@ class SuiteTicket {
|
|
|
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 suite_ticket found.');
|
|
31
|
+
}
|
|
32
|
+
return ticket;
|
|
46
33
|
}
|
|
47
34
|
}
|
|
48
35
|
;
|
package/dist/Pay/Application.js
CHANGED
|
@@ -34,9 +34,8 @@ class Application {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
getMerchant() {
|
|
37
|
-
var _a;
|
|
38
37
|
if (!this.merchant) {
|
|
39
|
-
this.merchant = new Merchant_1.default(this.config.get('mch_id'), this.config.get('private_key'), this.config.get('certificate'), this.config.get('secret_key'), this.config.get('v2_secret_key'),
|
|
38
|
+
this.merchant = new Merchant_1.default(this.config.get('mch_id'), this.config.get('private_key'), this.config.get('certificate'), this.config.get('secret_key'), this.config.get('v2_secret_key'), this.config.get('platform_certs') ?? [], this);
|
|
40
39
|
}
|
|
41
40
|
return this.merchant;
|
|
42
41
|
}
|
package/dist/Pay/Client.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
|
};
|
|
@@ -22,7 +13,6 @@ const LegacySignature_1 = __importDefault(require("./LegacySignature"));
|
|
|
22
13
|
const form_data_1 = __importDefault(require("form-data"));
|
|
23
14
|
class Client {
|
|
24
15
|
constructor(merchant, client, defaultOptions = {}) {
|
|
25
|
-
var _a;
|
|
26
16
|
this.merchant = merchant;
|
|
27
17
|
this.throw = true;
|
|
28
18
|
this.client = null;
|
|
@@ -39,7 +29,7 @@ class Client {
|
|
|
39
29
|
'/hk/v3/',
|
|
40
30
|
'/global/v3/',
|
|
41
31
|
];
|
|
42
|
-
this.throw = !!(
|
|
32
|
+
this.throw = !!(defaultOptions['throw'] ?? true);
|
|
43
33
|
this.defaultOptions = merge_1.default.recursive(true, this.defaultOptions, defaultOptions);
|
|
44
34
|
this.client = client || HttpClient_1.default.create();
|
|
45
35
|
}
|
|
@@ -54,51 +44,49 @@ class Client {
|
|
|
54
44
|
this.client.setLogger(logger);
|
|
55
45
|
return this;
|
|
56
46
|
}
|
|
57
|
-
request(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (
|
|
65
|
-
if (typeof payload.json
|
|
66
|
-
|
|
67
|
-
payload.data = JSON.stringify(payload.json);
|
|
68
|
-
}
|
|
69
|
-
else if (typeof payload.json === 'string') {
|
|
70
|
-
payload.data = payload.json;
|
|
71
|
-
}
|
|
72
|
-
delete payload.json;
|
|
47
|
+
async request(method, url, payload = {}) {
|
|
48
|
+
if (!payload.headers)
|
|
49
|
+
payload.headers = {};
|
|
50
|
+
if (!payload.headers['user-agent'] && !payload.headers['User-Agent']) {
|
|
51
|
+
payload.headers['user-agent'] = (0, Utils_1.createUserAgent)();
|
|
52
|
+
}
|
|
53
|
+
if (this.isV3Request(url) && !payload.headers['authorization']) {
|
|
54
|
+
if (typeof payload.json !== 'undefined') {
|
|
55
|
+
if (typeof payload.json === 'object') {
|
|
56
|
+
payload.data = JSON.stringify(payload.json);
|
|
73
57
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
else {
|
|
77
|
-
if (typeof payload.xml !== 'undefined') {
|
|
78
|
-
if (typeof payload.xml === 'object') {
|
|
79
|
-
payload.xml = (0, Utils_1.buildXml)(this.attachLegacySignature(payload.xml));
|
|
80
|
-
}
|
|
81
|
-
if (typeof payload.xml !== 'string') {
|
|
82
|
-
throw new Error('The `xml` option must be a string or object.');
|
|
83
|
-
}
|
|
84
|
-
payload.data = payload.xml;
|
|
85
|
-
delete payload.xml;
|
|
58
|
+
else if (typeof payload.json === 'string') {
|
|
59
|
+
payload.data = payload.json;
|
|
86
60
|
}
|
|
87
|
-
|
|
88
|
-
|
|
61
|
+
delete payload.json;
|
|
62
|
+
}
|
|
63
|
+
payload.headers['authorization'] = this.createSignature(method, url, payload);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
if (typeof payload.xml !== 'undefined') {
|
|
67
|
+
if (typeof payload.xml === 'object') {
|
|
68
|
+
payload.xml = (0, Utils_1.buildXml)(this.attachLegacySignature(payload.xml));
|
|
89
69
|
}
|
|
90
|
-
if (
|
|
91
|
-
|
|
70
|
+
if (typeof payload.xml !== 'string') {
|
|
71
|
+
throw new Error('The `xml` option must be a string or object.');
|
|
92
72
|
}
|
|
73
|
+
payload.data = payload.xml;
|
|
74
|
+
delete payload.xml;
|
|
93
75
|
}
|
|
94
|
-
if (
|
|
95
|
-
payload.data =
|
|
76
|
+
if (payload.data && typeof payload.data === 'object') {
|
|
77
|
+
payload.data = (0, Utils_1.buildXml)(this.attachLegacySignature(payload.data));
|
|
96
78
|
}
|
|
97
|
-
if (
|
|
98
|
-
payload.headers =
|
|
79
|
+
if (!payload.headers['content-type'] && !payload.headers['Content-Type']) {
|
|
80
|
+
payload.headers['content-type'] = 'text/xml';
|
|
99
81
|
}
|
|
100
|
-
|
|
101
|
-
|
|
82
|
+
}
|
|
83
|
+
if (this.prependData && Object.keys(this.prependData).length > 0) {
|
|
84
|
+
payload.data = { ...this.prependData, ...payload.data };
|
|
85
|
+
}
|
|
86
|
+
if (this.prependHeaders && Object.keys(this.prependHeaders).length > 0) {
|
|
87
|
+
payload.headers = { ...this.prependHeaders, ...payload.headers };
|
|
88
|
+
}
|
|
89
|
+
return this.client.request(method, (0, Utils_1.ltrim)(url, '\\/+'), payload);
|
|
102
90
|
}
|
|
103
91
|
/**
|
|
104
92
|
* 文件上传
|
|
@@ -109,35 +97,33 @@ class Client {
|
|
|
109
97
|
* @param filename 文件名,必须以 .jpg、.bmp、.png 为后缀
|
|
110
98
|
* @returns
|
|
111
99
|
*/
|
|
112
|
-
uploadMedia(
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
100
|
+
async uploadMedia(uri, file, meta = null, filename = null) {
|
|
101
|
+
if (typeof file === 'string') {
|
|
102
|
+
file = fs_1.default.readFileSync(file);
|
|
103
|
+
}
|
|
104
|
+
else if (typeof file !== 'string' && !Buffer.isBuffer(file)) {
|
|
105
|
+
file = await (0, Utils_1.streamToBuffer)(file);
|
|
106
|
+
}
|
|
107
|
+
filename = filename ?? 'file.jpg';
|
|
108
|
+
if (!meta) {
|
|
109
|
+
meta = {
|
|
110
|
+
filename: filename,
|
|
111
|
+
sha256: await (0, Utils_1.createHash)(file, 'sha256'),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
let metaJson = JSON.stringify(meta);
|
|
115
|
+
let formData = new form_data_1.default();
|
|
116
|
+
formData.append('file', file, filename);
|
|
117
|
+
formData.append('meta', metaJson, {
|
|
118
|
+
contentType: 'application/json',
|
|
119
|
+
});
|
|
120
|
+
return this.client.request('POST', (0, Utils_1.ltrim)(uri, '\\/+'), {
|
|
121
|
+
formData,
|
|
122
|
+
headers: {
|
|
123
|
+
'authorization': this.createSignature('POST', uri, {
|
|
124
|
+
data: metaJson,
|
|
125
|
+
}),
|
|
126
126
|
}
|
|
127
|
-
let metaJson = JSON.stringify(meta);
|
|
128
|
-
let formData = new form_data_1.default();
|
|
129
|
-
formData.append('file', file, filename);
|
|
130
|
-
formData.append('meta', metaJson, {
|
|
131
|
-
contentType: 'application/json',
|
|
132
|
-
});
|
|
133
|
-
return this.client.request('POST', (0, Utils_1.ltrim)(uri, '\\/+'), {
|
|
134
|
-
formData,
|
|
135
|
-
headers: {
|
|
136
|
-
'authorization': this.createSignature('POST', uri, {
|
|
137
|
-
data: metaJson,
|
|
138
|
-
}),
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
127
|
});
|
|
142
128
|
}
|
|
143
129
|
/**
|
|
@@ -14,12 +14,11 @@ class LegacySignature {
|
|
|
14
14
|
* @returns
|
|
15
15
|
*/
|
|
16
16
|
sign(params) {
|
|
17
|
-
var _a, _b;
|
|
18
17
|
let nonce = (0, Utils_1.randomString)();
|
|
19
18
|
let attributes = (0, merge_1.default)(true, {
|
|
20
19
|
nonce_str: nonce,
|
|
21
|
-
sub_mch_id:
|
|
22
|
-
sub_appid:
|
|
20
|
+
sub_mch_id: params['sub_mch_id'] ?? null,
|
|
21
|
+
sub_appid: params['sub_appid'] ?? null,
|
|
23
22
|
}, params);
|
|
24
23
|
let signString = '';
|
|
25
24
|
let sparator = '';
|