node-easywechat 3.0.0-beta.4 → 3.0.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.
Files changed (62) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +36 -1
  3. package/dist/Core/Contracts/RefreshableAccessTokenInterface.d.ts +2 -2
  4. package/dist/Core/Contracts/RefreshableAccessTokenInterface.js +2 -2
  5. package/dist/Core/Http/Minxins/MessageMixin.js +1 -2
  6. package/dist/Core/Http/ServerRequest.d.ts +1 -1
  7. package/dist/Core/Http/ServerRequest.js +16 -6
  8. package/dist/Core/HttpClient/HttpClient.js +3 -1
  9. package/dist/Core/HttpClient/HttpClientResponse.d.ts +16 -5
  10. package/dist/Core/HttpClient/HttpClientResponse.js +64 -22
  11. package/dist/Core/HttpClient/Mixins/PresetMixin.d.ts +0 -12
  12. package/dist/Core/HttpClient/Mixins/PresetMixin.js +0 -18
  13. package/dist/Core/Message.d.ts +0 -127
  14. package/dist/Core/Message.js +5 -1
  15. package/dist/Core/Mixins/DecryptXmlMessageMixin.js +1 -1
  16. package/dist/Core/Mixins/ResponseXmlMessageMixin.js +2 -2
  17. package/dist/Core/Support/PrivateKey.d.ts +20 -0
  18. package/dist/Core/Support/PrivateKey.js +38 -0
  19. package/dist/Core/Support/PublicKey.d.ts +14 -0
  20. package/dist/Core/Support/PublicKey.js +36 -0
  21. package/dist/Core/Support/RSA.d.ts +2 -2
  22. package/dist/Core/Support/RSA.js +4 -4
  23. package/dist/Core/Support/Utils.d.ts +6 -0
  24. package/dist/Core/Support/Utils.js +15 -1
  25. package/dist/MiniApp/Application.d.ts +4 -4
  26. package/dist/MiniApp/Contracts/ApplicationInterface.d.ts +2 -2
  27. package/dist/OfficialAccount/AccessToken.d.ts +2 -2
  28. package/dist/OfficialAccount/AccessToken.js +2 -2
  29. package/dist/OfficialAccount/Application.d.ts +4 -4
  30. package/dist/OfficialAccount/Application.js +0 -5
  31. package/dist/OfficialAccount/Contracts/ApplicationInterface.d.ts +2 -2
  32. package/dist/OfficialAccount/JsApiTicket.js +2 -2
  33. package/dist/OfficialAccount/Message.d.ts +139 -0
  34. package/dist/OfficialAccount/Message.js +1 -0
  35. package/dist/Pay/Application.d.ts +52 -0
  36. package/dist/Pay/Application.js +96 -0
  37. package/dist/Pay/Client.d.ts +54 -0
  38. package/dist/Pay/Client.js +146 -0
  39. package/dist/Pay/Config.d.ts +5 -0
  40. package/dist/Pay/Config.js +17 -0
  41. package/dist/Pay/Contracts/ApplicationInterface.d.ts +50 -0
  42. package/dist/Pay/Contracts/ApplicationInterface.js +45 -0
  43. package/dist/Pay/Contracts/MerchantInterface.d.ts +35 -0
  44. package/dist/Pay/Contracts/MerchantInterface.js +35 -0
  45. package/dist/Pay/Contracts/ResponseValidatorInterface.d.ts +9 -0
  46. package/dist/Pay/Contracts/ResponseValidatorInterface.js +10 -0
  47. package/dist/Pay/LegacySignature.d.ts +12 -0
  48. package/dist/Pay/LegacySignature.js +59 -0
  49. package/dist/Pay/Merchant.d.ts +25 -0
  50. package/dist/Pay/Merchant.js +53 -0
  51. package/dist/Pay/Message.d.ts +175 -0
  52. package/dist/Pay/Message.js +24 -0
  53. package/dist/Pay/Server.d.ts +41 -0
  54. package/dist/Pay/Server.js +122 -0
  55. package/dist/Pay/Signature.d.ts +15 -0
  56. package/dist/Pay/Signature.js +44 -0
  57. package/dist/Pay/Utils.d.ts +54 -0
  58. package/dist/Pay/Utils.js +142 -0
  59. package/dist/Types/global.d.ts +110 -1
  60. package/dist/index.d.ts +3 -3
  61. package/dist/index.js +3 -3
  62. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,10 +1,40 @@
1
1
  # CHANGELOG
2
2
 
3
3
 
4
+ ## v3.0.1 (2022-08-26)
5
+
6
+ - Fix: 修复with方法无效的问题
7
+
8
+ ## v3.0.0, origin/3.x (2022-07-28)
9
+
10
+ - Feat: 请求后自动解析响应结果,简化获取数据的方式
11
+ - Feat: 新增微信支付模块的公共调用方法
12
+ - Feat: 新增支付模块
13
+
14
+ - Fix: 响应内容解析时排除buffer对象
15
+ - Fix: 修复获取框架版本异常的问题
16
+ - Fix: 修复获取响应对象的字符串数据时异常的问题
17
+ - Fix: 修复测试用例中post等方式模拟请求时异常的问题
18
+ - Fix: 调整微信支付配置项
19
+ - Fix: 修复消息参数提示缺失问题
20
+ - Fix: 调整默认的RSA加密算法中的哈希方式为sha256
21
+ - Fix: 调整默认Message提示的作用域为公众号和小程序
22
+ - Fix: 修复AccessToken对象声明错误的问题
23
+
24
+ - Docs: 更新client调用方法说明
25
+ - Docs: 增加微信支付相关说明文档
26
+
27
+ ## v3.0.0-beta.5 (2022-06-29)
28
+
29
+ - Fix: 修复服务端获取消息异常的问题
30
+
4
31
  ## v3.0.0-beta.4 (2022-06-24)
5
32
 
33
+ - Feat: 增加链式添加上传文件的方法
6
34
  - Feat: 请求接口数据增加 FormData 实例的支持
7
35
 
36
+ - Fix: 修复接受微信回调接口验证请求时,返回对象无文件内容的问题
37
+
8
38
  ## v3.0.0-beta.3 (2022-06-22)
9
39
 
10
40
  - Fix: 修复获取js ticket时无法自动获取access_token的问题
package/README.md CHANGED
@@ -37,16 +37,31 @@ let app = new MiniApp({
37
37
  // 配置项
38
38
  });
39
39
 
40
+ // 微信支付
41
+ const { Pay } = require('node-easywechat');
42
+ // 实例化应用
43
+ let app = new Pay({
44
+ // 配置项
45
+ });
46
+
40
47
  // ----- 以下为通用的 api 调用方法 -----
41
48
 
42
49
  // 获取 api 调用客户端
43
50
  let client = app.getClient();
44
51
 
45
52
  // 请求 api
53
+ // 注意,这里返回的是 HttpClientResponse 对象
46
54
  let response = await client.post('/cgi-bin/user/info/updateremark', {
47
55
  openid: 'xxxx',
48
56
  remark: 'xxxx',
49
57
  });
58
+ // 获取具体数据
59
+ let data = response.toObject();
60
+ // 另外还有以下方法:
61
+ // toJson() 转json字符串
62
+ // toString() 转字符串
63
+ // saveAs(filename) 保存为文件
64
+ // toDataUrl() 转base64字符串
50
65
  ```
51
66
 
52
67
  ### 配置项示例
@@ -103,6 +118,26 @@ let response = await client.post('/cgi-bin/user/info/updateremark', {
103
118
  }
104
119
  ```
105
120
 
121
+ ``` js
122
+ // 微信支付配置
123
+ {
124
+ // 商户号
125
+ mch_id: '',
126
+ // 商户证书路径
127
+ certificate: '',
128
+ // 商户证书私钥路径
129
+ private_key: '',
130
+ // 平台证书(v3接口需要)
131
+ // 持路径列表或者PublicKey对象列表或者,以serial_no为key,证书内容或PublicKey对象为value的对象
132
+ // 下载工具:https://github.com/wechatpay-apiv3/CertificateDownloader
133
+ platform_certs: '',
134
+ // v3 API密钥
135
+ secret_key: '',
136
+ // v2 API密钥
137
+ v2_secret_key: '',
138
+ }
139
+ ```
140
+
106
141
  ### 自定义模块(模块替换)使用方法
107
142
 
108
143
  #### 日志模块
@@ -214,7 +249,7 @@ app.setRequest(request);
214
249
  ### 模块支持情况
215
250
 
216
251
  - [x] 公众号模块
217
- - [ ] 微信支付
252
+ - [x] 微信支付
218
253
  - [x] 小程序
219
254
  - [ ] 开放平台
220
255
  - [ ] 企业微信
@@ -1,9 +1,9 @@
1
1
  import AccessTokenInterface from "./AccessTokenInterface";
2
- declare abstract class RefreshableAccessToken extends AccessTokenInterface {
2
+ declare abstract class RefreshableAccessTokenInterface extends AccessTokenInterface {
3
3
  /**
4
4
  * 刷新token
5
5
  * @returns
6
6
  */
7
7
  refresh(): Promise<string>;
8
8
  }
9
- export = RefreshableAccessToken;
9
+ export = RefreshableAccessTokenInterface;
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  const AccessTokenInterface_1 = __importDefault(require("./AccessTokenInterface"));
15
- class RefreshableAccessToken extends AccessTokenInterface_1.default {
15
+ class RefreshableAccessTokenInterface extends AccessTokenInterface_1.default {
16
16
  /**
17
17
  * 刷新token
18
18
  * @returns
@@ -22,4 +22,4 @@ class RefreshableAccessToken extends AccessTokenInterface_1.default {
22
22
  }
23
23
  }
24
24
  ;
25
- module.exports = RefreshableAccessToken;
25
+ module.exports = RefreshableAccessTokenInterface;
@@ -63,8 +63,7 @@ class MessageMixin {
63
63
  }
64
64
  withBody(body) {
65
65
  if (Buffer.isBuffer(body)) {
66
- this.content = Buffer.from('');
67
- this.content.copy(body);
66
+ this.content = body;
68
67
  }
69
68
  else if (typeof body === 'string') {
70
69
  this.content = Buffer.from(body);
@@ -43,7 +43,7 @@ declare class ServerRequest implements ServerRequestInterface {
43
43
  * @param body 支持 Buffer、object对象、JSON字符串、XML字符串、QueryString格式的 body 内容字符串
44
44
  * @returns
45
45
  */
46
- static createFromIncomingMessage(req: IncomingMessage, body?: Buffer | Record<string, any> | string): ServerRequest;
46
+ static createFromIncomingMessage(req: IncomingMessage, body?: Buffer | Record<string, any> | string): Promise<ServerRequest>;
47
47
  }
48
48
  interface ServerRequest extends MessageMixin, RequestMixin {
49
49
  }
@@ -1,4 +1,13 @@
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
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
@@ -124,13 +133,14 @@ class ServerRequest {
124
133
  * @returns
125
134
  */
126
135
  static createFromIncomingMessage(req, body = null) {
127
- let request = new ServerRequest(req.method, req.url, req.headers || {}, body, req.httpVersion, (0, url_1.parse)(req.url, true).query || {});
128
- if (!body) {
129
- (0, raw_body_1.default)(req).then(res => {
136
+ return __awaiter(this, void 0, void 0, function* () {
137
+ let request = new ServerRequest(req.method, req.url, req.headers || {}, body, req.httpVersion);
138
+ if (!body) {
139
+ let res = yield (0, raw_body_1.default)(req);
130
140
  request.withBody(res);
131
- });
132
- }
133
- return request;
141
+ }
142
+ return request;
143
+ });
134
144
  }
135
145
  }
136
146
  ;
@@ -102,7 +102,9 @@ class HttpClient {
102
102
  let usedTime = Date.now() - starttime;
103
103
  yield this.logger('after', options, usedTime, response);
104
104
  }
105
- return new HttpClientResponse_1.default(response, this.failureJudge, this.throwError);
105
+ let resp = new HttpClientResponse_1.default(response, this.failureJudge, this.throwError);
106
+ yield resp.parseContent(this.throwError);
107
+ return resp;
106
108
  });
107
109
  }
108
110
  getInstance() {
@@ -5,7 +5,20 @@ declare class HttpClientResponse implements HttpClientResponseInterface {
5
5
  protected response: AxiosResponse;
6
6
  protected failureJudge: HttpClientFailureJudgeClosure;
7
7
  protected throwError: boolean;
8
+ protected parsedContent: Record<string, any>;
8
9
  constructor(response: AxiosResponse, failureJudge?: HttpClientFailureJudgeClosure, throwError?: boolean);
10
+ /**
11
+ * 设置解析后的body内容
12
+ * @param content
13
+ * @returns
14
+ */
15
+ setParsedContent(content: Record<string, any>): this;
16
+ /**
17
+ * 解析body内容
18
+ * @param throwError
19
+ * @returns
20
+ */
21
+ parseContent(throwError?: boolean): Promise<void>;
9
22
  withThrowError(throwError: boolean): this;
10
23
  throwOnFailure(): this;
11
24
  quietly(): this;
@@ -27,21 +40,19 @@ declare class HttpClientResponse implements HttpClientResponseInterface {
27
40
  isSuccessful(): boolean;
28
41
  /**
29
42
  * 返回对象格式
30
- * @param throwError
31
43
  * @returns
32
44
  */
33
- toObject<T = WeixinResponse>(throwError?: boolean): Promise<T>;
45
+ toObject<T = WeixinResponse>(): T;
34
46
  /**
35
47
  * 返回json字符串
36
- * @param throwError
37
48
  * @returns
38
49
  */
39
- toJson(throwError?: boolean): Promise<string>;
50
+ toJson(): string;
40
51
  /**
41
52
  * 返回字符串
42
53
  * @returns
43
54
  */
44
- toString(): Promise<string>;
55
+ toString(): string;
45
56
  /**
46
57
  * 另存为文件
47
58
  * @param filename 完整的文件名(含路径)
@@ -15,10 +15,65 @@ const fs_1 = __importDefault(require("fs"));
15
15
  const merge_1 = __importDefault(require("merge"));
16
16
  const Utils_1 = require("../Support/Utils");
17
17
  class HttpClientResponse {
18
- constructor(response, failureJudge = null, throwError = true) {
18
+ constructor(response, failureJudge = null, throwError = false) {
19
19
  this.response = response;
20
20
  this.failureJudge = failureJudge;
21
21
  this.throwError = throwError;
22
+ this.parsedContent = null;
23
+ }
24
+ /**
25
+ * 设置解析后的body内容
26
+ * @param content
27
+ * @returns
28
+ */
29
+ setParsedContent(content) {
30
+ this.parsedContent = content;
31
+ return this;
32
+ }
33
+ /**
34
+ * 解析body内容
35
+ * @param throwError
36
+ * @returns
37
+ */
38
+ parseContent(throwError = false) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ throwError = throwError !== null && throwError !== void 0 ? throwError : this.throwError;
41
+ let content = this.response.data;
42
+ if (!content) {
43
+ if (throwError) {
44
+ throw new Error('Response body is empty.');
45
+ }
46
+ return;
47
+ }
48
+ if (typeof content === 'string') {
49
+ if (this.is('xml') && content.indexOf('<xml>') > -1) {
50
+ this.parsedContent = yield (0, Utils_1.parseXml)(content);
51
+ }
52
+ else if (this.is('json')) {
53
+ try {
54
+ this.parsedContent = JSON.parse(content);
55
+ }
56
+ catch (e) {
57
+ if (throwError) {
58
+ throw new Error('Fail to parse JSON content.');
59
+ }
60
+ }
61
+ }
62
+ else if (this.is('text')) {
63
+ try {
64
+ this.parsedContent = (0, Utils_1.parseQueryString)(content);
65
+ }
66
+ catch (e) {
67
+ if (throwError) {
68
+ throw new Error('Fail to parse QueryString content.');
69
+ }
70
+ }
71
+ }
72
+ }
73
+ else if (!Buffer.isBuffer(content)) {
74
+ this.parsedContent = content;
75
+ }
76
+ });
22
77
  }
23
78
  withThrowError(throwError) {
24
79
  this.throwError = throwError;
@@ -63,40 +118,25 @@ class HttpClientResponse {
63
118
  }
64
119
  /**
65
120
  * 返回对象格式
66
- * @param throwError
67
121
  * @returns
68
122
  */
69
- toObject(throwError = null) {
70
- return __awaiter(this, void 0, void 0, function* () {
71
- throwError = throwError === null ? this.throwError : throwError;
72
- let content = this.response.data;
73
- if (!content) {
74
- throw new Error('Response body is empty.');
75
- }
76
- if (typeof content === 'string') {
77
- if (this.is('xml') && content.indexOf('<xml>') > -1) {
78
- return (0, Utils_1.parseXml)(content);
79
- }
80
- }
81
- return content;
82
- });
123
+ toObject() {
124
+ return this.parsedContent;
83
125
  }
84
126
  /**
85
127
  * 返回json字符串
86
- * @param throwError
87
128
  * @returns
88
129
  */
89
- toJson(throwError = null) {
90
- return __awaiter(this, void 0, void 0, function* () {
91
- return JSON.stringify(yield this.toObject(throwError));
92
- });
130
+ toJson() {
131
+ return JSON.stringify(this.toObject());
93
132
  }
94
133
  /**
95
134
  * 返回字符串
96
135
  * @returns
97
136
  */
98
137
  toString() {
99
- return this.toJson();
138
+ let obj = this.getContent();
139
+ return (typeof obj === 'object' && obj !== null) ? JSON.stringify(obj) : String(obj);
100
140
  }
101
141
  /**
102
142
  * 另存为文件
@@ -126,6 +166,8 @@ class HttpClientResponse {
126
166
  */
127
167
  is(type) {
128
168
  let contentType = this.getHeader('content-type');
169
+ if (!contentType)
170
+ return false;
129
171
  let res = false;
130
172
  switch (type.toLowerCase()) {
131
173
  case 'json':
@@ -62,18 +62,6 @@ declare class PresetMixin {
62
62
  * @returns
63
63
  */
64
64
  withSecret(new_secret?: string): this;
65
- /**
66
- * 预设置mch_id(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
67
- * @param new_mch_id
68
- * @returns
69
- */
70
- withMchId(new_mch_id?: string): this;
71
- /**
72
- * 预设置mch_id别名(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
73
- * @param new_alias
74
- * @returns
75
- */
76
- withMchIdAs(new_alias?: string): this;
77
65
  /**
78
66
  * 预设置文件
79
67
  * @param file 文件路径或可读文件流
@@ -109,24 +109,6 @@ class PresetMixin {
109
109
  this.with('secret', new_secret);
110
110
  return this;
111
111
  }
112
- /**
113
- * 预设置mch_id(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
114
- * @param new_mch_id
115
- * @returns
116
- */
117
- withMchId(new_mch_id = null) {
118
- this.with('mch_id', new_mch_id);
119
- return this;
120
- }
121
- /**
122
- * 预设置mch_id别名(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
123
- * @param new_alias
124
- * @returns
125
- */
126
- withMchIdAs(new_alias = null) {
127
- this.with(new_alias, this.presets['mch_id']);
128
- return this;
129
- }
130
112
  /**
131
113
  * 预设置文件
132
114
  * @param file 文件路径或可读文件流
@@ -27,132 +27,5 @@ declare class Message {
27
27
  toString(): string;
28
28
  }
29
29
  interface Message extends HasAttributesMixin {
30
- /**
31
- * 消息密文,兼容模式、安全模式才有
32
- */
33
- Encrypt?: string;
34
- /**
35
- * 开发者微信号
36
- */
37
- ToUserName?: string;
38
- /**
39
- * 发送方帐号 OpenId
40
- */
41
- FromUserName?: string;
42
- /**
43
- * 消息创建时间
44
- */
45
- CreateTime?: number;
46
- /**
47
- * 消息类型,'event':事件,'text':文本,'image':图片,'voice':语音,'video':视频,'shortvideo':小视频,'location':地理位置,'link':链接
48
- */
49
- MsgType?: string;
50
- /**
51
- * 消息id,64位整型
52
- * @scope MsgType='text' | 'image' | 'voice' | 'video' | 'shortvideo' | 'location'
53
- */
54
- MsgId?: string;
55
- /**
56
- * 文本消息内容
57
- * @scope MsgType='text'
58
- */
59
- Content?: string;
60
- /**
61
- * 图片链接
62
- * @scope MsgType='image'
63
- */
64
- PicUrl?: string;
65
- /**
66
- * 媒体id,可以调用获取临时素材接口拉取数据
67
- * @scope MsgType='image' | 'voice'
68
- */
69
- MediaId?: string;
70
- /**
71
- * 语音格式,如amr,speex等
72
- * @scope MsgType='voice'
73
- */
74
- Format?: string;
75
- /**
76
- * 语音识别结果,UTF8编码
77
- * @scope MsgType='voice'
78
- */
79
- Recognition?: string;
80
- /**
81
- * 缩略图的媒体id
82
- * @scope MsgType='video' | 'shortvideo'
83
- */
84
- ThumbMediaId?: string;
85
- /**
86
- * 纬度
87
- * @scope MsgType='location'
88
- */
89
- Location_X?: number;
90
- /**
91
- * 经度
92
- * @scope MsgType='location'
93
- */
94
- Location_Y?: number;
95
- /**
96
- * 地图缩放大小
97
- * @scope MsgType='location'
98
- */
99
- Scale?: number;
100
- /**
101
- * 地理位置信息
102
- * @scope MsgType='location'
103
- */
104
- Label?: number;
105
- /**
106
- * 消息标题
107
- * @scope MsgType='link'
108
- */
109
- Title?: string;
110
- /**
111
- * 消息描述
112
- * @scope MsgType='link'
113
- */
114
- Description?: string;
115
- /**
116
- * 消息链接
117
- * @scope MsgType='link'
118
- */
119
- Url?: string;
120
- /**
121
- * 事件类型,subscribe:关注,unsubscribe:取消关注,SCAN:扫描(用户已关注),LOCATION:地理位置;CLICK:自定义菜单事件,VIEW:点击菜单跳转链接
122
- * @scope MsgType='event'
123
- */
124
- Event?: string;
125
- /**
126
- * 事件KEY值
127
- *
128
- * Event='subscribe' | 'SCAN'时:qrscene_为前缀,后面为二维码的参数值
129
- *
130
- * Event='CLICK'时:自定义菜单接口中KEY值对应
131
- *
132
- * Event='VIEW'时:设置的跳转URL
133
- *
134
- * @scope MsgType='event' && Event='subscribe' | 'SCAN' | 'CLICK' | 'VIEW'
135
- */
136
- EventKey?: string;
137
- /**
138
- * 二维码的ticket,可用来换取二维码图片
139
- * @scope MsgType='event' && Event='subscribe' | 'SCAN'
140
- */
141
- Ticket?: string;
142
- /**
143
- * 纬度
144
- * @scope MsgType='event' && Event='LOCATION'
145
- */
146
- Latitude?: number;
147
- /**
148
- * 经度
149
- * @scope MsgType='event' && Event='LOCATION'
150
- */
151
- Longitude?: number;
152
- /**
153
- * 位置精度
154
- * @scope MsgType='event' && Event='LOCATION'
155
- */
156
- Precision?: number;
157
30
  }
158
31
  export = Message;
@@ -49,7 +49,11 @@ class Message {
49
49
  */
50
50
  static createFromRequest(request) {
51
51
  return __awaiter(this, void 0, void 0, function* () {
52
- let originContent = request.getBody().toString();
52
+ let originContent = '';
53
+ let body = request.getBody();
54
+ if (body) {
55
+ originContent = body.toString();
56
+ }
53
57
  let attributes = {};
54
58
  if ('<' === originContent.substring(0, 1)) {
55
59
  attributes = yield (0, Utils_1.parseXml)(originContent);
@@ -16,7 +16,7 @@ class DecryptXmlMessageMixin {
16
16
  */
17
17
  decryptMessage(message, encryptor, signature, timestamp, nonce) {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
- let ciphertext = message.Encrypt;
19
+ let ciphertext = message['Encrypt'];
20
20
  this.validateSignature(encryptor.getToken(), ciphertext, signature, timestamp, nonce);
21
21
  let attributes = yield (0, Utils_1.parseXml)(encryptor.decrypt(ciphertext, signature, nonce, timestamp));
22
22
  message.merge(attributes);
@@ -25,8 +25,8 @@ class ResponseXmlMessageMixin {
25
25
  return new Response_1.default(200, {}, 'success');
26
26
  }
27
27
  let attributes = merge_1.default.recursive({
28
- ToUserName: message.FromUserName,
29
- FromUserName: message.ToUserName,
28
+ ToUserName: message['FromUserName'],
29
+ FromUserName: message['ToUserName'],
30
30
  CreateTime: (0, Utils_1.getTimestamp)(),
31
31
  }, yield this.normalizeResponse(response));
32
32
  return this.createXmlResponse(attributes, encryptor);
@@ -0,0 +1,20 @@
1
+ export declare class PrivateKey {
2
+ protected key: string;
3
+ protected passphrase?: string;
4
+ constructor(key: string, passphrase?: string);
5
+ /**
6
+ * 获取私钥
7
+ * @returns
8
+ */
9
+ getKey(): string;
10
+ /**
11
+ * 获取密码
12
+ * @returns
13
+ */
14
+ getPassphrase(): string;
15
+ /**
16
+ * 转为字符串
17
+ * @returns
18
+ */
19
+ toString(): string;
20
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PrivateKey = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ class PrivateKey {
9
+ constructor(key, passphrase) {
10
+ this.key = key;
11
+ this.passphrase = passphrase;
12
+ if (fs_1.default.existsSync(key)) {
13
+ this.key = fs_1.default.readFileSync(key, 'utf8') || '';
14
+ }
15
+ }
16
+ /**
17
+ * 获取私钥
18
+ * @returns
19
+ */
20
+ getKey() {
21
+ return this.key;
22
+ }
23
+ /**
24
+ * 获取密码
25
+ * @returns
26
+ */
27
+ getPassphrase() {
28
+ return this.passphrase;
29
+ }
30
+ /**
31
+ * 转为字符串
32
+ * @returns
33
+ */
34
+ toString() {
35
+ return this.getKey();
36
+ }
37
+ }
38
+ exports.PrivateKey = PrivateKey;
@@ -0,0 +1,14 @@
1
+ export declare class PublicKey {
2
+ certificate: string;
3
+ constructor(certificate: string);
4
+ /**
5
+ * 获取公钥的序列号
6
+ * @returns
7
+ */
8
+ getSerialNo(): string;
9
+ /**
10
+ * 转为字符串
11
+ * @returns
12
+ */
13
+ toString(): string;
14
+ }