node-easywechat 2.12.5 → 2.13.0

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 (35) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +63 -0
  3. package/dist/Core/BaseAccessToken.js +2 -1
  4. package/dist/Core/BaseClient.d.ts +33 -1
  5. package/dist/Core/BaseClient.js +50 -3
  6. package/dist/Core/Http/Response.d.ts +4 -1
  7. package/dist/Core/Http/Response.js +9 -1
  8. package/dist/MicroMerchant/Application.d.ts +12 -0
  9. package/dist/MicroMerchant/Application.js +17 -0
  10. package/dist/MicroMerchant/Core/BaseClient.d.ts +3 -1
  11. package/dist/MicroMerchant/Core/BaseClient.js +3 -0
  12. package/dist/MiniProgram/Application.d.ts +12 -0
  13. package/dist/MiniProgram/Application.js +17 -0
  14. package/dist/OfficialAccount/Application.d.ts +12 -0
  15. package/dist/OfficialAccount/Application.js +17 -0
  16. package/dist/OfficialAccount/CustomerService/CustomerServiceClient.d.ts +3 -1
  17. package/dist/OfficialAccount/CustomerService/Messenger.d.ts +1 -1
  18. package/dist/OfficialAccount/CustomerService/Messenger.js +1 -2
  19. package/dist/OfficialAccount/TemplateMessage/TemplateMessageClient.d.ts +13 -1
  20. package/dist/OfficialAccount/TemplateMessage/TemplateMessageClient.js +14 -1
  21. package/dist/OpenPlatform/Application.d.ts +12 -0
  22. package/dist/OpenPlatform/Application.js +17 -0
  23. package/dist/OpenPlatform/Authorizer/MiniProgram/Application.d.ts +3 -0
  24. package/dist/OpenPlatform/Authorizer/MiniProgram/Application.js +8 -0
  25. package/dist/OpenPlatform/Authorizer/MiniProgram/Base/MiniProgramBaseClient.d.ts +8 -0
  26. package/dist/OpenPlatform/Authorizer/MiniProgram/Base/MiniProgramBaseClient.js +16 -0
  27. package/dist/OpenWork/Application.d.ts +12 -0
  28. package/dist/OpenWork/Application.js +17 -0
  29. package/dist/Payment/Application.d.ts +9 -0
  30. package/dist/Payment/Application.js +13 -0
  31. package/dist/Payment/Core/BaseClient.d.ts +4 -4
  32. package/dist/Payment/Core/BaseClient.js +16 -1
  33. package/dist/Work/Application.d.ts +12 -0
  34. package/dist/Work/Application.js +17 -0
  35. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,6 +1,20 @@
1
1
  # CHANGELOG
2
2
 
3
3
 
4
+ ## v2.13.0 (2023-09-15)
5
+
6
+ - Feat: 新增获取通用的请求客户端方法
7
+ - Feat: 优化客户端请求方法
8
+
9
+ ## v2.12.6 (2023-08-11)
10
+
11
+ - Feat: 开放平台增加获取小程序版本信息接口
12
+
13
+ - Fix: 公众号模版消息添加模版时增加关键字设置,同时优化各方法的注视
14
+ - Fix: 修复通过Messenger发送客服消息时,类型丢失的问题
15
+
16
+ ## v2.12.5 (2023-05-02)
17
+
4
18
  ## v2.12.4 (2023-04-23)
5
19
 
6
20
  - Fix: 更新依赖包node-socialite
package/README.md CHANGED
@@ -63,6 +63,69 @@ let app = EasyWechat.Factory.getInstance('OficialAccount', {
63
63
  });
64
64
  ```
65
65
 
66
+ ### 发起请求
67
+
68
+ 本工具包封装了大部分常用的微信api,您可以直接使用,而不用考虑具体的接口地址。
69
+
70
+ 以公众号获取用户信息为例:
71
+
72
+ ```js
73
+ // 1. 创建公众号应用
74
+ let officialAccount = new EasyWechat.Factory.OfficialAccount({
75
+ // ...
76
+ });
77
+ // 2. 获取用户信息
78
+ let user = await officialAccount.user.get('user_openid');
79
+ ```
80
+
81
+ 但是,微信的api实在太多,有时可能不能及时更新,因此,自`2.13.0`版本开始,本工具提供通用的发送请求的客户端(感谢`ValueLan`的建议),具体使用方法如下:
82
+
83
+ ```js
84
+ // 1. 创建公众号应用
85
+ let officialAccount = new EasyWechat.Factory.OfficialAccount({
86
+ // ...
87
+ });
88
+ // 2. 获取客户端实例
89
+ let client = officialAccount.getClient();
90
+ // 3. 发送请求
91
+ // get
92
+ let querystring = { foo: 'bar' };
93
+ let data = client.httpGet('/example-url', querystring);
94
+
95
+ // post
96
+ let data = { foo: 'bar' };
97
+ let data = client.httpPost('/example-url', data);
98
+
99
+ // 上传文件
100
+ let files = { file1: '/path/to/file1', file2: fs.createReadStream('/path/to/file2') };
101
+ let data = { foo: 'bar' };
102
+ let querystring = { foo: 'bar' };
103
+ let data = client.httpUpload('/example-url', files, data, querystring);
104
+
105
+ // 通用请求
106
+ let payload = { url: '/example-url', method: 'post', data: { foo: 'bar' } };
107
+ let data = client.request(payload); // 参数为 axios 的请求参数
108
+
109
+ // 通用请求(返回原始数据,可用于下载文件等)
110
+ let payload = { url: '/example-url', method: 'post', data: { foo: 'bar' } };
111
+ let data = client.requestRaw(payload); // 参数为 axios 的请求参数
112
+ ```
113
+
114
+ **注意**:如果是 `Payment` 支付应用,则只有 `request`、`requestRaw` 方法,以及 `safeRequest` 方法(该方法请求时会携带支付证书),切请求参数也不一样
115
+
116
+ ```js
117
+ // 1. 创建公众号应用
118
+ let payment = new EasyWechat.Factory.Payment({
119
+ // ...
120
+ });
121
+ // 2. 获取客户端实例
122
+ let client = payment.getClient();
123
+ // 3. 发送请求 request、safeRequest、requestRaw 三个方法参数一样
124
+ let data = { foo: 'bar' };
125
+ let payload = {}; // axios 的请求参数
126
+ let data = client.request('/example-url', data, 'post', payload);
127
+ ```
128
+
66
129
  ### 配置项示例
67
130
 
68
131
  ``` js
@@ -205,7 +205,8 @@ class BaseAccessToken {
205
205
  return __awaiter(this, void 0, void 0, function* () {
206
206
  payload.params = payload.params || {};
207
207
  if (!payload.params[this.queryName || this.tokenKey]) {
208
- payload.params[this.queryName || this.tokenKey] = (yield this.getToken())[this.tokenKey];
208
+ let token = yield this.getToken();
209
+ payload.params[this.queryName || this.tokenKey] = token[this.tokenKey];
209
210
  }
210
211
  return payload;
211
212
  });
@@ -9,11 +9,43 @@ declare abstract class BaseClient implements HttpMixin {
9
9
  constructor(app: BaseApplication, accessToken?: BaseAccessToken);
10
10
  setAccessToken(accessToken: BaseAccessToken): this;
11
11
  getAccessToken(): BaseAccessToken;
12
+ /**
13
+ * 发送请求
14
+ * @param payload axios请求参数
15
+ * @param returnResponse 是否返回axios响应对象,默认false表示直接返回数据
16
+ */
12
17
  request(payload: AxiosRequestConfig, returnResponse?: Boolean): Promise<AxiosResponse<any>>;
18
+ /**
19
+ * 上传文件请求
20
+ * @param url 地址
21
+ * @param files 文件键值对,键名为文件字段,键值为文件路径或可读流
22
+ * @param form 其它表单参数键值对
23
+ * @param query querystring参数键值对
24
+ */
13
25
  httpUpload(url: string, files?: object, form?: object, query?: object): Promise<any>;
26
+ /**
27
+ * Get请求
28
+ * @param url 地址
29
+ * @param query querystring参数键值对
30
+ */
14
31
  httpGet(url: string, query?: object): Promise<any>;
15
- httpPost(url: string, formData?: object): Promise<any>;
32
+ /**
33
+ * Post请求
34
+ * @param url 地址
35
+ * @param data body数据,详见 axios 请求配置的 data 参数
36
+ */
37
+ httpPost(url: string, data?: object): Promise<any>;
38
+ /**
39
+ * Post请求(JSON数据)
40
+ * @param url 地址
41
+ * @param data body数据,详见 axios 请求配置的 data 参数
42
+ * @param query querystring参数键值对
43
+ */
16
44
  httpPostJson(url: string, data?: object, query?: object): Promise<any>;
45
+ /**
46
+ * 发送请求并返回原始数据,主要用于下载文件等。返回封装过的Response对象
47
+ * @param payload axios请求参数
48
+ */
17
49
  requestRaw(payload: AxiosRequestConfig): Promise<Response>;
18
50
  doRequest(payload: AxiosRequestConfig): Promise<AxiosResponse<any>>;
19
51
  }
@@ -31,6 +31,11 @@ class BaseClient {
31
31
  getAccessToken() {
32
32
  return this.accessToken;
33
33
  }
34
+ /**
35
+ * 发送请求
36
+ * @param payload axios请求参数
37
+ * @param returnResponse 是否返回axios响应对象,默认false表示直接返回数据
38
+ */
34
39
  request(payload, returnResponse = false) {
35
40
  return __awaiter(this, void 0, void 0, function* () {
36
41
  payload = payload || {};
@@ -47,6 +52,13 @@ class BaseClient {
47
52
  return returnResponse ? response : response.data;
48
53
  });
49
54
  }
55
+ /**
56
+ * 上传文件请求
57
+ * @param url 地址
58
+ * @param files 文件键值对,键名为文件字段,键值为文件路径或可读流
59
+ * @param form 其它表单参数键值对
60
+ * @param query querystring参数键值对
61
+ */
50
62
  httpUpload(url, files = {}, form = {}, query = {}) {
51
63
  let formData = new form_data_1.default;
52
64
  for (let name in files) {
@@ -67,6 +79,11 @@ class BaseClient {
67
79
  params: query,
68
80
  });
69
81
  }
82
+ /**
83
+ * Get请求
84
+ * @param url 地址
85
+ * @param query querystring参数键值对
86
+ */
70
87
  httpGet(url, query = {}) {
71
88
  return this.request({
72
89
  url,
@@ -74,13 +91,24 @@ class BaseClient {
74
91
  params: query,
75
92
  });
76
93
  }
77
- httpPost(url, formData = {}) {
94
+ /**
95
+ * Post请求
96
+ * @param url 地址
97
+ * @param data body数据,详见 axios 请求配置的 data 参数
98
+ */
99
+ httpPost(url, data = {}) {
78
100
  return this.request({
79
101
  url,
80
102
  method: 'POST',
81
- data: formData,
103
+ data: data,
82
104
  });
83
105
  }
106
+ /**
107
+ * Post请求(JSON数据)
108
+ * @param url 地址
109
+ * @param data body数据,详见 axios 请求配置的 data 参数
110
+ * @param query querystring参数键值对
111
+ */
84
112
  httpPostJson(url, data = {}, query = {}) {
85
113
  return this.request({
86
114
  url,
@@ -89,12 +117,31 @@ class BaseClient {
89
117
  params: query,
90
118
  });
91
119
  }
120
+ /**
121
+ * 发送请求并返回原始数据,主要用于下载文件等。返回封装过的Response对象
122
+ * @param payload axios请求参数
123
+ */
92
124
  requestRaw(payload) {
93
125
  return __awaiter(this, void 0, void 0, function* () {
94
126
  payload = payload || {};
95
127
  payload.responseType = 'arraybuffer';
96
128
  let res = yield this.request(payload, true);
97
- return new Response_1.default(res.data, res.status, res.headers);
129
+ let config = {};
130
+ let keys = ['url', 'baseURL', 'method', 'params', 'headers'];
131
+ keys.map(key => {
132
+ if (typeof payload[key] === 'string' || typeof payload[key] === 'number') {
133
+ config[key] = payload[key];
134
+ }
135
+ else if (typeof payload[key] === 'object') {
136
+ if (payload[key] === null) {
137
+ config[key] = null;
138
+ }
139
+ else {
140
+ config[key] = JSON.parse(JSON.stringify(payload[key]));
141
+ }
142
+ }
143
+ });
144
+ return new Response_1.default(res.data, res.status, res.headers, config);
98
145
  });
99
146
  }
100
147
  // Rewrite by HttpMixin
@@ -3,7 +3,8 @@ export default class Response {
3
3
  protected content: Buffer;
4
4
  protected statusCode: number;
5
5
  protected headers: object;
6
- constructor(content: Buffer, statusCode?: number, headers?: object);
6
+ protected config: object;
7
+ constructor(content: Buffer, statusCode?: number, headers?: object, config?: object);
7
8
  setContent(content: Buffer): void;
8
9
  getContent(): Buffer;
9
10
  setStatusCode(statusCode: number): void;
@@ -12,4 +13,6 @@ export default class Response {
12
13
  setHeaders(headers: object): void;
13
14
  getHeaders(): object;
14
15
  getHeader(key: string): string;
16
+ setConfig(config: object): void;
17
+ getConfig(): object;
15
18
  }
@@ -1,13 +1,15 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class Response {
4
- constructor(content, statusCode = 200, headers = {}) {
4
+ constructor(content, statusCode = 200, headers = {}, config = {}) {
5
5
  this.content = null;
6
6
  this.statusCode = 200;
7
7
  this.headers = {};
8
+ this.config = {};
8
9
  this.content = content;
9
10
  this.statusCode = statusCode;
10
11
  this.headers = headers;
12
+ this.config = config;
11
13
  }
12
14
  setContent(content) {
13
15
  this.content = content;
@@ -33,6 +35,12 @@ class Response {
33
35
  getHeader(key) {
34
36
  return this.headers[key] || '';
35
37
  }
38
+ setConfig(config) {
39
+ this.config = config;
40
+ }
41
+ getConfig() {
42
+ return this.config;
43
+ }
36
44
  }
37
45
  exports.default = Response;
38
46
  ;
@@ -2,12 +2,19 @@ import BaseApplication from '../Core/BaseApplication';
2
2
  import { EasyWechatConfig } from '../Core/Types';
3
3
  import MicroMerchantBase from './Base/MicroMerchantBase';
4
4
  import CertficatesClient from './Certficates/CertficatesClient';
5
+ import BaseClient from './Core/BaseClient';
5
6
  import MediaClient from './Media/MediaClient';
6
7
  import MerchantConfigClient from './MerchantConfig/MerchantConfigClient';
7
8
  import MeterialClient from './Meterial/MeterialClient';
8
9
  import WithdrawClient from './Withdraw/WithdrawClient';
10
+ declare class Client extends BaseClient {
11
+ }
9
12
  export default class MicroMerchant extends BaseApplication {
10
13
  protected defaultConfig: EasyWechatConfig;
14
+ /**
15
+ * 客户端实例
16
+ */
17
+ client: Client;
11
18
  base: MicroMerchantBase;
12
19
  certficates: CertficatesClient;
13
20
  media: MediaClient;
@@ -24,4 +31,9 @@ export default class MicroMerchant extends BaseApplication {
24
31
  getStatus(): Promise<any>;
25
32
  upgrade(): Promise<any>;
26
33
  getUpgradeStatus(): Promise<any>;
34
+ /**
35
+ * 获取客户端实例
36
+ */
37
+ getClient(): Client;
27
38
  }
39
+ export {};
@@ -7,10 +7,14 @@ const BaseApplication_1 = __importDefault(require("../Core/BaseApplication"));
7
7
  const Utils_1 = require("../Core/Utils");
8
8
  const MicroMerchantBase_1 = __importDefault(require("./Base/MicroMerchantBase"));
9
9
  const CertficatesClient_1 = __importDefault(require("./Certficates/CertficatesClient"));
10
+ const BaseClient_1 = __importDefault(require("./Core/BaseClient"));
10
11
  const MediaClient_1 = __importDefault(require("./Media/MediaClient"));
11
12
  const MerchantConfigClient_1 = __importDefault(require("./MerchantConfig/MerchantConfigClient"));
12
13
  const MeterialClient_1 = __importDefault(require("./Meterial/MeterialClient"));
13
14
  const WithdrawClient_1 = __importDefault(require("./Withdraw/WithdrawClient"));
15
+ class Client extends BaseClient_1.default {
16
+ }
17
+ ;
14
18
  class MicroMerchant extends BaseApplication_1.default {
15
19
  constructor(config = {}, prepends = {}, id = null) {
16
20
  super(config, prepends, id);
@@ -23,6 +27,10 @@ class MicroMerchant extends BaseApplication_1.default {
23
27
  baseURL: 'https://api.mch.weixin.qq.com/',
24
28
  },
25
29
  };
30
+ /**
31
+ * 客户端实例
32
+ */
33
+ this.client = null;
26
34
  this.registerProviders();
27
35
  }
28
36
  registerProviders() {
@@ -94,6 +102,15 @@ class MicroMerchant extends BaseApplication_1.default {
94
102
  getUpgradeStatus() {
95
103
  return this.base.getUpgradeStatus.apply(this.base, arguments);
96
104
  }
105
+ /**
106
+ * 获取客户端实例
107
+ */
108
+ getClient() {
109
+ if (this.client) {
110
+ return this.client;
111
+ }
112
+ return this.client = new Client(this);
113
+ }
97
114
  }
98
115
  exports.default = MicroMerchant;
99
116
  ;
@@ -1,8 +1,10 @@
1
+ import BaseApplication from '../Application';
1
2
  import PaymentBaseClient from '../../Payment/Core/BaseClient';
2
3
  import { AxiosRequestConfig, AxiosResponse } from 'axios';
3
4
  declare class BaseClient extends PaymentBaseClient {
5
+ constructor(app: BaseApplication);
4
6
  httpUpload(url: string, files?: object, form?: object, query?: object, returnResponse?: boolean): Promise<any>;
5
- protected request(endpoint: string, params?: object, method?: string, options?: AxiosRequestConfig, returnResponse?: boolean): Promise<AxiosResponse<any>>;
7
+ request(endpoint: string, params?: object, method?: string, options?: AxiosRequestConfig, returnResponse?: boolean): Promise<AxiosResponse<any>>;
6
8
  protected processParams(params: object): object;
7
9
  protected getSensitiveFieldsName(): string[];
8
10
  }
@@ -18,6 +18,9 @@ const RSA_1 = __importDefault(require("../../Core/RSA"));
18
18
  const fs_1 = __importDefault(require("fs"));
19
19
  const form_data_1 = __importDefault(require("form-data"));
20
20
  class BaseClient extends BaseClient_1.default {
21
+ constructor(app) {
22
+ super(app);
23
+ }
21
24
  httpUpload(url, files = {}, form = {}, query = {}, returnResponse = false) {
22
25
  let formData = new form_data_1.default;
23
26
  for (let name in files) {
@@ -38,7 +38,14 @@ import RiskControlClient from './RiskControl/RiskControlClient';
38
38
  import LiveClient from './Live/LiveClient';
39
39
  import BroadcastClient from './Broadcast/BroadcastClient';
40
40
  import UnionClient from './Union/UnionClient';
41
+ import BaseClient from '../Core/BaseClient';
42
+ declare class Client extends BaseClient {
43
+ }
41
44
  export default class MiniProgram extends BaseApplication {
45
+ /**
46
+ * 客户端实例
47
+ */
48
+ client: Client;
42
49
  access_token: AccessToken;
43
50
  auth: AuthClient;
44
51
  encryptor: Encryptor;
@@ -80,4 +87,9 @@ export default class MiniProgram extends BaseApplication {
80
87
  constructor(config?: EasyWechatConfig, prepends?: Object, id?: String);
81
88
  registerProviders(): void;
82
89
  getPaidUnionid(): Promise<any>;
90
+ /**
91
+ * 获取客户端实例
92
+ */
93
+ getClient(): Client;
83
94
  }
95
+ export {};
@@ -51,9 +51,17 @@ const RiskControlClient_1 = __importDefault(require("./RiskControl/RiskControlCl
51
51
  const LiveClient_1 = __importDefault(require("./Live/LiveClient"));
52
52
  const BroadcastClient_1 = __importDefault(require("./Broadcast/BroadcastClient"));
53
53
  const UnionClient_1 = __importDefault(require("./Union/UnionClient"));
54
+ const BaseClient_1 = __importDefault(require("../Core/BaseClient"));
55
+ class Client extends BaseClient_1.default {
56
+ }
57
+ ;
54
58
  class MiniProgram extends BaseApplication_1.default {
55
59
  constructor(config = {}, prepends = {}, id = null) {
56
60
  super(config, prepends, id);
61
+ /**
62
+ * 客户端实例
63
+ */
64
+ this.client = null;
57
65
  this.registerProviders();
58
66
  }
59
67
  registerProviders() {
@@ -195,6 +203,15 @@ class MiniProgram extends BaseApplication_1.default {
195
203
  getPaidUnionid() {
196
204
  return this.base.getPaidUnionid.apply(this.base, arguments);
197
205
  }
206
+ /**
207
+ * 获取客户端实例
208
+ */
209
+ getClient() {
210
+ if (this.client) {
211
+ return this.client;
212
+ }
213
+ return this.client = new Client(this);
214
+ }
198
215
  }
199
216
  exports.default = MiniProgram;
200
217
  ;
@@ -29,8 +29,15 @@ import QrcodeClient from '../BaseService/Qrcode/QrcodeClient';
29
29
  import UrlClient from '../BaseService/Url/UrlClient';
30
30
  import { EasyWechatConfig } from '../Core/Types';
31
31
  import WeChat from 'node-socialite/dist/Providers/WeChat';
32
+ import BaseClient from '../Core/BaseClient';
33
+ declare class Client extends BaseClient {
34
+ }
32
35
  export default class OfficialAccount extends BaseApplication {
33
36
  protected defaultConfig: EasyWechatConfig;
37
+ /**
38
+ * 客户端实例
39
+ */
40
+ client: Client;
34
41
  access_token: AccessToken;
35
42
  encryptor: Encryptor;
36
43
  server: Guard;
@@ -65,4 +72,9 @@ export default class OfficialAccount extends BaseApplication {
65
72
  clearQuota(): Promise<any>;
66
73
  getValidIps(): Promise<any>;
67
74
  checkCallbackUrl(): Promise<any>;
75
+ /**
76
+ * 获取客户端实例
77
+ */
78
+ getClient(): Client;
68
79
  }
80
+ export {};
@@ -43,6 +43,10 @@ const MediaClient_1 = __importDefault(require("../BaseService/Media/MediaClient"
43
43
  const QrcodeClient_1 = __importDefault(require("../BaseService/Qrcode/QrcodeClient"));
44
44
  const UrlClient_1 = __importDefault(require("../BaseService/Url/UrlClient"));
45
45
  const node_socialite_1 = require("node-socialite");
46
+ const BaseClient_1 = __importDefault(require("../Core/BaseClient"));
47
+ class Client extends BaseClient_1.default {
48
+ }
49
+ ;
46
50
  class OfficialAccount extends BaseApplication_1.default {
47
51
  constructor(config = {}, prepends = {}, id = null) {
48
52
  super(config, prepends, id);
@@ -54,6 +58,10 @@ class OfficialAccount extends BaseApplication_1.default {
54
58
  callback: '',
55
59
  },
56
60
  };
61
+ /**
62
+ * 客户端实例
63
+ */
64
+ this.client = null;
57
65
  this.registerProviders();
58
66
  }
59
67
  registerProviders() {
@@ -205,6 +213,15 @@ class OfficialAccount extends BaseApplication_1.default {
205
213
  checkCallbackUrl() {
206
214
  return this.base.checkCallbackUrl.apply(this.base, arguments);
207
215
  }
216
+ /**
217
+ * 获取客户端实例
218
+ */
219
+ getClient() {
220
+ if (this.client) {
221
+ return this.client;
222
+ }
223
+ return this.client = new Client(this);
224
+ }
208
225
  }
209
226
  exports.default = OfficialAccount;
210
227
  ;
@@ -1,4 +1,6 @@
1
1
  import BaseClient from '../../Core/BaseClient';
2
+ import { Message } from '../../Core/Messages/Message';
3
+ import Messenger from './Messenger';
2
4
  export default class CustomerServiceClient extends BaseClient {
3
5
  list(): Promise<any>;
4
6
  online(): Promise<any>;
@@ -7,7 +9,7 @@ export default class CustomerServiceClient extends BaseClient {
7
9
  delete(account: string): Promise<any>;
8
10
  invite(account: string, wechatId: string): Promise<any>;
9
11
  setAvatar(account: string, path: string): Promise<any>;
10
- message(message: object): object;
12
+ message(message: string | Message): Messenger;
11
13
  send(message: object): Promise<any>;
12
14
  showTypingStatusToUser(openid: string): Promise<any>;
13
15
  hideTypingStatusToUser(openid: string): Promise<any>;
@@ -6,7 +6,7 @@ export default class Messenger {
6
6
  protected account: string;
7
7
  protected client: CustomerServiceClient;
8
8
  constructor(client: CustomerServiceClient);
9
- message(message: any): this;
9
+ message(message: string | Message): this;
10
10
  by(account: string): this;
11
11
  from(account: string): this;
12
12
  to(openid: string): this;
@@ -1,14 +1,13 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const Messages_1 = require("../../Core/Messages");
4
- const Utils_1 = require("../../Core/Utils");
5
4
  class Messenger {
6
5
  constructor(client) {
7
6
  this._message = null;
8
7
  this.client = client;
9
8
  }
10
9
  message(message) {
11
- if ((0, Utils_1.isString)(message)) {
10
+ if (typeof message === 'string') {
12
11
  message = new Messages_1.Text(message);
13
12
  }
14
13
  this._message = message;
@@ -5,35 +5,47 @@ export default class Client extends BaseClient {
5
5
  protected required: Array<string>;
6
6
  /**
7
7
  * 修改账号所属行业
8
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#设置所属行业
8
9
  * @param industry_id1 主行业id
9
10
  * @param industry_id2 副行业id
10
11
  */
11
12
  setIndustry(industry_id1: string, industry_id2: string): Promise<any>;
12
13
  /**
13
14
  * 获取支持的行业列表
15
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#获取设置的行业信息
14
16
  */
15
17
  getIndustry(): Promise<any>;
16
18
  /**
17
19
  * 添加模板
20
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#获得模板ID
18
21
  * @param template_id_short 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
22
+ * @param keyword_name_list 选用的类目模板的关键词,按顺序传入,如果为空,或者关键词不在模板库中,会返回40246错误码
19
23
  */
20
- addTemplate(template_id_short: string): Promise<any>;
24
+ addTemplate(template_id_short: string, keyword_name_list: string[]): Promise<any>;
21
25
  /**
22
26
  * 获取所有模板列表
27
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#获取模板列表
23
28
  */
24
29
  getPrivateTemplates(): Promise<any>;
25
30
  /**
26
31
  * 删除模板
32
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#删除模板
27
33
  * @param template_id 模版id
28
34
  */
29
35
  deletePrivateTemplate(template_id: string): Promise<any>;
30
36
  /**
31
37
  * 发送模板消息
38
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#发送模板消息
32
39
  * @param data 模版详情
33
40
  */
34
41
  send(data: object): Promise<any>;
35
42
  /**
36
43
  * 发送一次性订阅消息
44
+ *
45
+ * (注意与订阅通知的区别,若是订阅通知请使用`subscribe_message`对应的API)
46
+ *
47
+ * @see [一次性订阅消息](https://developers.weixin.qq.com/doc/offiaccount/Message_Management/One-time_subscription_info.html)
48
+ * @see [订阅通知](https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/intro.html)
37
49
  * @param data 消息详情
38
50
  */
39
51
  sendSubscription(data: object): Promise<any>;
@@ -23,6 +23,7 @@ class Client extends BaseClient_1.default {
23
23
  }
24
24
  /**
25
25
  * 修改账号所属行业
26
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#设置所属行业
26
27
  * @param industry_id1 主行业id
27
28
  * @param industry_id2 副行业id
28
29
  */
@@ -34,27 +35,33 @@ class Client extends BaseClient_1.default {
34
35
  }
35
36
  /**
36
37
  * 获取支持的行业列表
38
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#获取设置的行业信息
37
39
  */
38
40
  getIndustry() {
39
41
  return this.httpGet('cgi-bin/template/get_industry');
40
42
  }
41
43
  /**
42
44
  * 添加模板
45
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#获得模板ID
43
46
  * @param template_id_short 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
47
+ * @param keyword_name_list 选用的类目模板的关键词,按顺序传入,如果为空,或者关键词不在模板库中,会返回40246错误码
44
48
  */
45
- addTemplate(template_id_short) {
49
+ addTemplate(template_id_short, keyword_name_list) {
46
50
  return this.httpPostJson('cgi-bin/template/api_add_template', {
47
51
  template_id_short,
52
+ keyword_name_list,
48
53
  });
49
54
  }
50
55
  /**
51
56
  * 获取所有模板列表
57
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#获取模板列表
52
58
  */
53
59
  getPrivateTemplates() {
54
60
  return this.httpGet('cgi-bin/template/get_all_private_template');
55
61
  }
56
62
  /**
57
63
  * 删除模板
64
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#删除模板
58
65
  * @param template_id 模版id
59
66
  */
60
67
  deletePrivateTemplate(template_id) {
@@ -64,6 +71,7 @@ class Client extends BaseClient_1.default {
64
71
  }
65
72
  /**
66
73
  * 发送模板消息
74
+ * @see https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#发送模板消息
67
75
  * @param data 模版详情
68
76
  */
69
77
  send(data) {
@@ -72,6 +80,11 @@ class Client extends BaseClient_1.default {
72
80
  }
73
81
  /**
74
82
  * 发送一次性订阅消息
83
+ *
84
+ * (注意与订阅通知的区别,若是订阅通知请使用`subscribe_message`对应的API)
85
+ *
86
+ * @see [一次性订阅消息](https://developers.weixin.qq.com/doc/offiaccount/Message_Management/One-time_subscription_info.html)
87
+ * @see [订阅通知](https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/intro.html)
75
88
  * @param data 消息详情
76
89
  */
77
90
  sendSubscription(data) {
@@ -10,7 +10,14 @@ import OpenPlatformGuard from './Server/OpenPlatformGuard';
10
10
  import CodeTemplateClient from './CodeTemplate/CodeTemplateClient';
11
11
  import ComponentClient from './Component/ComponentClient';
12
12
  import { EasyWechatConfig } from '../Core/Types';
13
+ import BaseClient from '../Core/BaseClient';
14
+ declare class Client extends BaseClient {
15
+ }
13
16
  export default class OpenPlatform extends BaseApplication {
17
+ /**
18
+ * 客户端实例
19
+ */
20
+ client: Client;
14
21
  verify_ticket: VerifyTicket;
15
22
  access_token: AccessToken;
16
23
  base: OpenPlatformBase;
@@ -85,4 +92,9 @@ export default class OpenPlatform extends BaseApplication {
85
92
  * 清零调用次数
86
93
  */
87
94
  clearQuota(...arg: any[]): Promise<any>;
95
+ /**
96
+ * 获取客户端实例
97
+ */
98
+ getClient(): Client;
88
99
  }
100
+ export {};
@@ -27,9 +27,17 @@ const Encryptor_1 = __importDefault(require("../Core/Encryptor"));
27
27
  const OpenPlatformGuard_1 = __importDefault(require("./Server/OpenPlatformGuard"));
28
28
  const CodeTemplateClient_1 = __importDefault(require("./CodeTemplate/CodeTemplateClient"));
29
29
  const ComponentClient_1 = __importDefault(require("./Component/ComponentClient"));
30
+ const BaseClient_1 = __importDefault(require("../Core/BaseClient"));
31
+ class Client extends BaseClient_1.default {
32
+ }
33
+ ;
30
34
  class OpenPlatform extends BaseApplication_1.default {
31
35
  constructor(config = {}, prepends = {}, id = null) {
32
36
  super(config, prepends, id);
37
+ /**
38
+ * 客户端实例
39
+ */
40
+ this.client = null;
33
41
  this.registerProviders();
34
42
  }
35
43
  registerProviders() {
@@ -211,6 +219,15 @@ class OpenPlatform extends BaseApplication_1.default {
211
219
  clearQuota(...arg) {
212
220
  return this.base.clearQuota.apply(this.base, arg);
213
221
  }
222
+ /**
223
+ * 获取客户端实例
224
+ */
225
+ getClient() {
226
+ if (this.client) {
227
+ return this.client;
228
+ }
229
+ return this.client = new Client(this);
230
+ }
214
231
  }
215
232
  exports.default = OpenPlatform;
216
233
  ;
@@ -1,4 +1,5 @@
1
1
  import BaseApplication from '../../../MiniProgram/Application';
2
+ import MiniProgramBaseClient from './Base/MiniProgramBaseClient';
2
3
  import CodeClient from './Code/CodeClient';
3
4
  import DomainClient from './Domain/DomainClient';
4
5
  import MaterialClient from './Material/MaterialClient';
@@ -8,6 +9,7 @@ import AccountClient from './Account/AccountClient';
8
9
  import SettingClient from './Setting/SettingClient';
9
10
  import TesterClient from './Tester/TesterClient';
10
11
  export default class MiniProgram extends BaseApplication {
12
+ base: MiniProgramBaseClient;
11
13
  code: CodeClient;
12
14
  domain: DomainClient;
13
15
  material: MaterialClient;
@@ -18,4 +20,5 @@ export default class MiniProgram extends BaseApplication {
18
20
  tester: TesterClient;
19
21
  constructor(config?: Object, prepends?: Object, id?: String);
20
22
  registerExtraProviders(): void;
23
+ getVersionInfo(): Promise<any>;
21
24
  }
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const Application_1 = __importDefault(require("../../../MiniProgram/Application"));
7
+ const MiniProgramBaseClient_1 = __importDefault(require("./Base/MiniProgramBaseClient"));
7
8
  const CodeClient_1 = __importDefault(require("./Code/CodeClient"));
8
9
  const DomainClient_1 = __importDefault(require("./Domain/DomainClient"));
9
10
  const MaterialClient_1 = __importDefault(require("./Material/MaterialClient"));
@@ -18,6 +19,9 @@ class MiniProgram extends Application_1.default {
18
19
  this.registerExtraProviders();
19
20
  }
20
21
  registerExtraProviders() {
22
+ this.offsetSet('base', function (app) {
23
+ return new MiniProgramBaseClient_1.default(app);
24
+ });
21
25
  this.offsetSet('code', function (app) {
22
26
  return new CodeClient_1.default(app);
23
27
  });
@@ -43,6 +47,10 @@ class MiniProgram extends Application_1.default {
43
47
  return new TesterClient_1.default(app);
44
48
  });
45
49
  }
50
+ // map to `base` module
51
+ getVersionInfo() {
52
+ return this.base.getVersionInfo.apply(this.base, arguments);
53
+ }
46
54
  }
47
55
  exports.default = MiniProgram;
48
56
  ;
@@ -0,0 +1,8 @@
1
+ import BaseClient from '../../../../MiniProgram/Base/MiniProgramBase';
2
+ export default class MiniProgramBaseClient extends BaseClient {
3
+ /**
4
+ * 获取小程序版本信息
5
+ * @see https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/get_versioninfo.html
6
+ */
7
+ getVersionInfo(): Promise<any>;
8
+ }
@@ -0,0 +1,16 @@
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
+ const MiniProgramBase_1 = __importDefault(require("../../../../MiniProgram/Base/MiniProgramBase"));
7
+ class MiniProgramBaseClient extends MiniProgramBase_1.default {
8
+ /**
9
+ * 获取小程序版本信息
10
+ * @see https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/get_versioninfo.html
11
+ */
12
+ getVersionInfo() {
13
+ return this.httpPostJson('wxa/getversioninfo');
14
+ }
15
+ }
16
+ exports.default = MiniProgramBaseClient;
@@ -16,8 +16,15 @@ import LicenseAccountClient from './License/LicenseAccountClient';
16
16
  import LicenseAppClient from './License/LicenseAppClient';
17
17
  import LicenseAutoActiveClient from './License/LicenseAutoActiveClient';
18
18
  import MediaClient from './Media/MediaClient';
19
+ import BaseClient from '../Core/BaseClient';
20
+ declare class Client extends BaseClient {
21
+ }
19
22
  export default class OpenWork extends BaseApplication {
20
23
  protected defaultConfig: EasyWechatConfig;
24
+ /**
25
+ * 客户端实例
26
+ */
27
+ client: Client;
21
28
  provider_access_token: ProviderAccessToken;
22
29
  suite_access_token: SuiteAccessToken;
23
30
  suite_ticket: SuiteTicket;
@@ -46,4 +53,9 @@ export default class OpenWork extends BaseApplication {
46
53
  * @returns
47
54
  */
48
55
  work(authCorpId: string, permanentCode: string): Work;
56
+ /**
57
+ * 获取客户端实例
58
+ */
59
+ getClient(): Client;
49
60
  }
61
+ export {};
@@ -30,6 +30,10 @@ const LicenseAccountClient_1 = __importDefault(require("./License/LicenseAccount
30
30
  const LicenseAppClient_1 = __importDefault(require("./License/LicenseAppClient"));
31
31
  const LicenseAutoActiveClient_1 = __importDefault(require("./License/LicenseAutoActiveClient"));
32
32
  const MediaClient_1 = __importDefault(require("./Media/MediaClient"));
33
+ const BaseClient_1 = __importDefault(require("../Core/BaseClient"));
34
+ class Client extends BaseClient_1.default {
35
+ }
36
+ ;
33
37
  class OpenWork extends BaseApplication_1.default {
34
38
  constructor(config = {}, prepends = {}, id = null) {
35
39
  super(config, prepends, id);
@@ -41,6 +45,10 @@ class OpenWork extends BaseApplication_1.default {
41
45
  baseURL: 'https://qyapi.weixin.qq.com/',
42
46
  },
43
47
  };
48
+ /**
49
+ * 客户端实例
50
+ */
51
+ this.client = null;
44
52
  this.registerProviders();
45
53
  }
46
54
  registerProviders() {
@@ -150,6 +158,15 @@ class OpenWork extends BaseApplication_1.default {
150
158
  work(authCorpId, permanentCode) {
151
159
  return new Application_2.default(authCorpId, permanentCode, this);
152
160
  }
161
+ /**
162
+ * 获取客户端实例
163
+ */
164
+ getClient() {
165
+ if (this.client) {
166
+ return this.client;
167
+ }
168
+ return this.client = new Client(this);
169
+ }
153
170
  }
154
171
  exports.default = OpenWork;
155
172
  ;
@@ -16,8 +16,12 @@ import TransferClient from './Transfer/TransferClient';
16
16
  import SecurityClient from './Security/SecurityClient';
17
17
  import ProfitSharingClient from './ProfitSharing/ProfitSharingClient';
18
18
  import { EasyWechatConfig, PaymentPaidHandler, PaymentRefundedHandler, PaymentScannedHandler } from '../Core/Types';
19
+ import BaseClient from './Core/BaseClient';
20
+ declare class PaymentClient extends BaseClient {
21
+ }
19
22
  export default class Payment extends BaseApplication {
20
23
  protected defaultConfig: EasyWechatConfig;
24
+ client: PaymentClient;
21
25
  base: PaymentBase;
22
26
  bill: BillClient;
23
27
  coupon: CouponClient;
@@ -73,4 +77,9 @@ export default class Payment extends BaseApplication {
73
77
  * @param auth_code 扫码支付付款码
74
78
  */
75
79
  authCodeToOpenid(): Promise<any>;
80
+ /**
81
+ * 获取客户端实例
82
+ */
83
+ getClient(): PaymentClient;
76
84
  }
85
+ export {};
@@ -32,6 +32,10 @@ const SandboxClient_1 = __importDefault(require("./Sandbox/SandboxClient"));
32
32
  const TransferClient_1 = __importDefault(require("./Transfer/TransferClient"));
33
33
  const SecurityClient_1 = __importDefault(require("./Security/SecurityClient"));
34
34
  const ProfitSharingClient_1 = __importDefault(require("./ProfitSharing/ProfitSharingClient"));
35
+ const BaseClient_1 = __importDefault(require("./Core/BaseClient"));
36
+ class PaymentClient extends BaseClient_1.default {
37
+ }
38
+ ;
35
39
  class Payment extends BaseApplication_1.default {
36
40
  constructor(config = {}, prepends = {}, id = null) {
37
41
  super(config, prepends, id);
@@ -179,6 +183,15 @@ class Payment extends BaseApplication_1.default {
179
183
  authCodeToOpenid() {
180
184
  return this.base.authCodeToOpenid.apply(this.base, arguments);
181
185
  }
186
+ /**
187
+ * 获取客户端实例
188
+ */
189
+ getClient() {
190
+ if (this.client) {
191
+ return this.client;
192
+ }
193
+ return this.client = new PaymentClient(this);
194
+ }
182
195
  }
183
196
  exports.default = Payment;
184
197
  ;
@@ -6,10 +6,10 @@ declare class BaseClient implements HttpMixin {
6
6
  protected serverIp: String;
7
7
  constructor(app: BaseApplication);
8
8
  protected prepends(): {};
9
- protected request(endpoint: string, params?: object, method?: string, options?: AxiosRequestConfig, returnResponse?: boolean): Promise<AxiosResponse<any>>;
10
- protected safeRequest(endpoint: string, params?: object, method?: string, options?: AxiosRequestConfig): Promise<any>;
11
- protected requestRaw(endpoint: string, params?: object, method?: string, options?: AxiosRequestConfig): Promise<any>;
12
- protected wrap(endpoint: string): string;
9
+ request(endpoint: string, params?: object, method?: string, options?: AxiosRequestConfig, returnResponse?: boolean): Promise<AxiosResponse<any>>;
10
+ safeRequest(endpoint: string, params?: object, method?: string, options?: AxiosRequestConfig): Promise<any>;
11
+ requestRaw(endpoint: string, params?: object, method?: string, options?: AxiosRequestConfig): Promise<any>;
12
+ wrap(endpoint: string): string;
13
13
  getServerIp(): Promise<String>;
14
14
  getClientIp(): string;
15
15
  doRequest(payload: AxiosRequestConfig): Promise<AxiosResponse<any>>;
@@ -74,7 +74,22 @@ class BaseClient {
74
74
  return __awaiter(this, void 0, void 0, function* () {
75
75
  options.responseType = 'arraybuffer';
76
76
  let res = yield this.request(endpoint, params, method, options, true);
77
- return new Response_1.default(res.data, res.status, res.headers);
77
+ let config = {};
78
+ let keys = ['url', 'baseURL', 'method', 'params', 'headers'];
79
+ keys.map(key => {
80
+ if (typeof options[key] === 'string' || typeof options[key] === 'number') {
81
+ config[key] = options[key];
82
+ }
83
+ else if (typeof options[key] === 'object') {
84
+ if (options[key] === null) {
85
+ config[key] = null;
86
+ }
87
+ else {
88
+ config[key] = JSON.parse(JSON.stringify(options[key]));
89
+ }
90
+ }
91
+ });
92
+ return new Response_1.default(res.data, res.status, res.headers, config);
78
93
  });
79
94
  }
80
95
  wrap(endpoint) {
@@ -38,8 +38,15 @@ import ExternalSchoolClient from './ExternalContact/SchoolClient';
38
38
  import ExternalMomentClient from './ExternalContact/MomentClient';
39
39
  import CorpGroupClient from './CorpGroup/CorpGroupClient';
40
40
  import WeWork from 'node-socialite/dist/Providers/WeWork';
41
+ import BaseClient from '../Core/BaseClient';
42
+ declare class Client extends BaseClient {
43
+ }
41
44
  export default class Work extends BaseApplication {
42
45
  protected defaultConfig: EasyWechatConfig;
46
+ /**
47
+ * 客户端实例
48
+ */
49
+ client: Client;
43
50
  oa: OAClient;
44
51
  access_token: AccessToken;
45
52
  agent: AgentClient;
@@ -81,4 +88,9 @@ export default class Work extends BaseApplication {
81
88
  registerProviders(): void;
82
89
  miniProgram(): MiniProgram;
83
90
  getCallbackIp(): Promise<any>;
91
+ /**
92
+ * 获取客户端实例
93
+ */
94
+ getClient(): Client;
84
95
  }
96
+ export {};
@@ -52,6 +52,10 @@ const SchoolClient_1 = __importDefault(require("./ExternalContact/SchoolClient")
52
52
  const MomentClient_1 = __importDefault(require("./ExternalContact/MomentClient"));
53
53
  const CorpGroupClient_1 = __importDefault(require("./CorpGroup/CorpGroupClient"));
54
54
  const node_socialite_1 = require("node-socialite");
55
+ const BaseClient_1 = __importDefault(require("../Core/BaseClient"));
56
+ class Client extends BaseClient_1.default {
57
+ }
58
+ ;
55
59
  class Work extends BaseApplication_1.default {
56
60
  constructor(config = {}, prepends = {}, id = null) {
57
61
  super(config, prepends, id);
@@ -60,6 +64,10 @@ class Work extends BaseApplication_1.default {
60
64
  baseURL: 'https://qyapi.weixin.qq.com/',
61
65
  },
62
66
  };
67
+ /**
68
+ * 客户端实例
69
+ */
70
+ this.client = null;
63
71
  this.registerProviders();
64
72
  }
65
73
  registerProviders() {
@@ -231,6 +239,15 @@ class Work extends BaseApplication_1.default {
231
239
  getCallbackIp() {
232
240
  return this.base.getCallbackIp.apply(this.base, arguments);
233
241
  }
242
+ /**
243
+ * 获取客户端实例
244
+ */
245
+ getClient() {
246
+ if (this.client) {
247
+ return this.client;
248
+ }
249
+ return this.client = new Client(this);
250
+ }
234
251
  }
235
252
  exports.default = Work;
236
253
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-easywechat",
3
- "version": "2.12.5",
3
+ "version": "2.13.0",
4
4
  "description": "EasyWechat SDK for Node.js (NOT OFFICIAL)",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {