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
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const merge_1 = __importDefault(require("merge"));
|
|
6
|
+
const Response_1 = __importDefault(require("../Http/Response"));
|
|
7
|
+
const Utils_1 = require("../Support/Utils");
|
|
8
|
+
class ResponseMessageMixin {
|
|
9
|
+
/**
|
|
10
|
+
* 转化为回复消息
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
async transformToReply(response, message, encryptor = null, isXml = true) {
|
|
14
|
+
if (!response || response === true) {
|
|
15
|
+
return new Response_1.default(200, {}, 'success');
|
|
16
|
+
}
|
|
17
|
+
let attributes = merge_1.default.recursive({
|
|
18
|
+
ToUserName: message['FromUserName'],
|
|
19
|
+
FromUserName: message['ToUserName'],
|
|
20
|
+
CreateTime: (0, Utils_1.getTimestamp)(),
|
|
21
|
+
}, await this.normalizeResponse(response));
|
|
22
|
+
if (isXml) {
|
|
23
|
+
return this.createXmlResponse(attributes, encryptor);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return this.createJsonResponse(attributes, encryptor);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async normalizeResponse(response) {
|
|
30
|
+
if (typeof response === 'function') {
|
|
31
|
+
response = await response();
|
|
32
|
+
}
|
|
33
|
+
if (typeof response === 'object') {
|
|
34
|
+
if (!response['MsgType']) {
|
|
35
|
+
throw new Error('`MsgType` cannot be empty.');
|
|
36
|
+
}
|
|
37
|
+
return response;
|
|
38
|
+
}
|
|
39
|
+
if (typeof response === 'string' || typeof response === 'number') {
|
|
40
|
+
return {
|
|
41
|
+
MsgType: 'text',
|
|
42
|
+
Content: response,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`Invalid Response "${response.toString()}".`);
|
|
46
|
+
}
|
|
47
|
+
createXmlResponse(attributes, encryptor = null) {
|
|
48
|
+
let xml = (0, Utils_1.buildXml)(attributes);
|
|
49
|
+
return new Response_1.default(200, {
|
|
50
|
+
'Content-Type': 'text/xml'
|
|
51
|
+
}, encryptor ? encryptor.encrypt(xml) : xml);
|
|
52
|
+
}
|
|
53
|
+
createJsonResponse(attributes, encryptor = null) {
|
|
54
|
+
let json = JSON.stringify(attributes);
|
|
55
|
+
return new Response_1.default(200, {
|
|
56
|
+
'Content-Type': 'application/json'
|
|
57
|
+
}, encryptor ? encryptor.encrypt(json) : json);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
;
|
|
61
|
+
module.exports = ResponseMessageMixin;
|
|
@@ -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
|
};
|
|
@@ -292,16 +283,14 @@ exports.singleItem = singleItem;
|
|
|
292
283
|
* @param xml
|
|
293
284
|
* @returns
|
|
294
285
|
*/
|
|
295
|
-
const parseXml = function (xml) {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
return res;
|
|
304
|
-
});
|
|
286
|
+
const parseXml = async function (xml) {
|
|
287
|
+
let res = await xml2js_1.default.parseStringPromise(xml);
|
|
288
|
+
// fix [Object: null prototype]
|
|
289
|
+
res = JSON.parse(JSON.stringify(res));
|
|
290
|
+
res = (0, exports.singleItem)(res);
|
|
291
|
+
if (res['xml'])
|
|
292
|
+
res = res['xml'];
|
|
293
|
+
return res;
|
|
305
294
|
};
|
|
306
295
|
exports.parseXml = parseXml;
|
|
307
296
|
/**
|
|
@@ -121,7 +121,7 @@ class Application {
|
|
|
121
121
|
return this.utils;
|
|
122
122
|
}
|
|
123
123
|
createClient() {
|
|
124
|
-
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) =>
|
|
124
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => (response.toObject()['errcode'] ?? 0) || (response.toObject()['error'] !== null && response.toObject()['error'] !== undefined), this.getConfig().get('http.throw', true)))
|
|
125
125
|
.setPresets(this.getConfig().all());
|
|
126
126
|
}
|
|
127
127
|
/**
|
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');
|
|
@@ -36,11 +36,35 @@ declare class Server extends ServerInterface {
|
|
|
36
36
|
protected decryptRequestMessage(query: Record<string, any>): ServerHandlerClosure<Message>;
|
|
37
37
|
}
|
|
38
38
|
interface Server {
|
|
39
|
+
/**
|
|
40
|
+
* 从后添加处理器
|
|
41
|
+
* @param handler
|
|
42
|
+
*/
|
|
39
43
|
with(next: ServerHandlerClosure<Message>): this;
|
|
44
|
+
/**
|
|
45
|
+
* 从后添加处理器
|
|
46
|
+
* @param handler
|
|
47
|
+
*/
|
|
40
48
|
withHandler(next: ServerHandlerClosure<Message>): this;
|
|
49
|
+
/**
|
|
50
|
+
* 从前添加处理器
|
|
51
|
+
* @param handler
|
|
52
|
+
*/
|
|
41
53
|
prepend(next: ServerHandlerClosure<Message>): this;
|
|
54
|
+
/**
|
|
55
|
+
* 从前添加处理器
|
|
56
|
+
* @param handler
|
|
57
|
+
*/
|
|
42
58
|
prependHandler(next: ServerHandlerClosure<Message>): this;
|
|
59
|
+
/**
|
|
60
|
+
* 删除处理器
|
|
61
|
+
* @param handler
|
|
62
|
+
*/
|
|
43
63
|
without(next: ServerHandlerClosure<Message>): this;
|
|
64
|
+
/**
|
|
65
|
+
* 删除处理器
|
|
66
|
+
* @param handler
|
|
67
|
+
*/
|
|
44
68
|
withoutHandler(next: ServerHandlerClosure<Message>): this;
|
|
45
69
|
}
|
|
46
70
|
export = Server;
|
|
@@ -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,23 +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
|
-
}
|
|
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;
|
|
44
36
|
}
|
|
45
37
|
/**
|
|
46
38
|
* 添加普通消息处理器
|
|
@@ -49,10 +41,8 @@ class Server extends ServerInterface_1.default {
|
|
|
49
41
|
* @returns
|
|
50
42
|
*/
|
|
51
43
|
addMessageListener(type, handler) {
|
|
52
|
-
return this.withHandler(function (message, next) {
|
|
53
|
-
return
|
|
54
|
-
return message.MsgType === type ? handler(message, next) : next(message);
|
|
55
|
-
});
|
|
44
|
+
return this.withHandler(async function (message, next) {
|
|
45
|
+
return message.MsgType === type ? handler(message, next) : next(message);
|
|
56
46
|
});
|
|
57
47
|
}
|
|
58
48
|
/**
|
|
@@ -62,10 +52,8 @@ class Server extends ServerInterface_1.default {
|
|
|
62
52
|
* @returns
|
|
63
53
|
*/
|
|
64
54
|
addEventListener(event, handler) {
|
|
65
|
-
return this.withHandler(function (message, next) {
|
|
66
|
-
return
|
|
67
|
-
return message.Event === event ? handler(message, next) : next(message);
|
|
68
|
-
});
|
|
55
|
+
return this.withHandler(async function (message, next) {
|
|
56
|
+
return message.Event === event ? handler(message, next) : next(message);
|
|
69
57
|
});
|
|
70
58
|
}
|
|
71
59
|
/**
|
|
@@ -77,12 +65,12 @@ class Server extends ServerInterface_1.default {
|
|
|
77
65
|
return Message_1.default.createFromRequest(request || this.request);
|
|
78
66
|
}
|
|
79
67
|
decryptRequestMessage(query) {
|
|
80
|
-
return (message, next) =>
|
|
68
|
+
return async (message, next) => {
|
|
81
69
|
if (!this.encryptor)
|
|
82
70
|
return null;
|
|
83
|
-
|
|
71
|
+
await this.decryptMessage(message, this.encryptor, query['msg_signature'] || '', query['timestamp'] || '', query['nonce'] || '');
|
|
84
72
|
return next(message);
|
|
85
|
-
}
|
|
73
|
+
};
|
|
86
74
|
}
|
|
87
75
|
}
|
|
88
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
|
;
|