node-easywechat 2.8.3 → 2.9.2

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 (41) hide show
  1. package/README.md +51 -6
  2. package/dist/Core/Http/Request.js +2 -2
  3. package/dist/Core/ServerGuard.d.ts +2 -2
  4. package/dist/Core/Types.d.ts +34 -4
  5. package/dist/MiniProgram/Express/ExpressClient.d.ts +18 -0
  6. package/dist/MiniProgram/Express/ExpressClient.js +33 -0
  7. package/dist/MiniProgram/Shop/OrderClient.d.ts +7 -0
  8. package/dist/MiniProgram/Shop/OrderClient.js +9 -0
  9. package/dist/OfficialAccount/Base/OfficialAccountBase.d.ts +5 -0
  10. package/dist/OfficialAccount/Base/OfficialAccountBase.js +9 -0
  11. package/dist/OpenPlatform/Application.js +2 -2
  12. package/dist/OpenWork/Application.d.ts +37 -0
  13. package/dist/OpenWork/Application.js +128 -0
  14. package/dist/OpenWork/Auth/ProviderAccessToken.d.ts +8 -0
  15. package/dist/OpenWork/Auth/ProviderAccessToken.js +33 -0
  16. package/dist/OpenWork/Auth/SuiteAccessToken.d.ts +8 -0
  17. package/dist/OpenWork/Auth/SuiteAccessToken.js +34 -0
  18. package/dist/OpenWork/Auth/SuiteTicket.d.ts +8 -0
  19. package/dist/OpenWork/Auth/SuiteTicket.js +40 -0
  20. package/dist/OpenWork/Corp/CorpClient.d.ts +72 -0
  21. package/dist/OpenWork/Corp/CorpClient.js +150 -0
  22. package/dist/OpenWork/MiniProgram/MiniProgramClient.d.ts +5 -0
  23. package/dist/OpenWork/MiniProgram/MiniProgramClient.js +18 -0
  24. package/dist/OpenWork/Provider/ProviderClient.d.ts +81 -0
  25. package/dist/OpenWork/Provider/ProviderClient.js +172 -0
  26. package/dist/OpenWork/Server/OpenWorkGuard.d.ts +13 -0
  27. package/dist/OpenWork/Server/OpenWorkGuard.js +51 -0
  28. package/dist/OpenWork/Work/Application.d.ts +5 -0
  29. package/dist/OpenWork/Work/Application.js +22 -0
  30. package/dist/OpenWork/Work/Auth/AccessToken.d.ts +12 -0
  31. package/dist/OpenWork/Work/Auth/AccessToken.js +44 -0
  32. package/dist/Work/Application.d.ts +2 -0
  33. package/dist/Work/Application.js +5 -0
  34. package/dist/Work/ExternalContact/GroupChatWayClient.d.ts +32 -0
  35. package/dist/Work/ExternalContact/GroupChatWayClient.js +54 -0
  36. package/dist/Work/Server/Guard.d.ts +1 -1
  37. package/dist/index.d.ts +8 -2
  38. package/dist/index.js +8 -6
  39. package/package.json +2 -5
  40. package/node-easywechat.file_cache.easywechat.kernel.access_token.8a9684b45da7de42a06a70ba01a7005a.cache +0 -1
  41. package/node-easywechat.file_cache.easywechat.kernel.access_token.c3d06edaf326aad75d32f31348df787a.cache +0 -1
package/README.md CHANGED
@@ -3,9 +3,11 @@
3
3
 
4
4
  [![Build Status](https://app.travis-ci.com/hpyer/node-easywechat.svg?branch=develop)](https://app.travis-ci.com/hpyer/node-easywechat) [![npm](https://img.shields.io/npm/v/node-easywechat.svg)](https://www.npmjs.com/package/node-easywechat) [![License](https://img.shields.io/npm/l/node-easywechat.svg)](LICENSE)
5
5
 
6
- **注:2.x分支针对 EasyWechat 的 5.x版本(由于4.x与5.x的结构基本一致,就不升级大版本了)。若您需要 EasyWechat 的 3.x版本,请切换到 [1.x](https://github.com/hpyer/node-easywechat/tree/1.x) 分支。**
6
+ **注:2.x分支针对 EasyWechat 的 5.x版本(由于4.x与5.x的结构基本一致,就不升级大版本了)。**
7
7
 
8
- [EasyWechat](https://github.com/overtrue/wechat) 是一个由 `安正超` 大神用 PHP 开发的开源的微信非官方 SDK。其功能强大,使用方便,个人一直很喜欢,所以近日将其在 Node.js 上实现。本人会尽量还原其配置项以及接口的调用方式,但毕竟语言环境不同,具体的实现方式会有些许差别,还请各位开发者见谅。
8
+ ~~若您需要 EasyWechat 的 3.x版本,请切换到 [1.x](https://github.com/hpyer/node-easywechat/tree/1.x) 分支。~~ **1.0版本已废弃**
9
+
10
+ [EasyWechat](https://github.com/w7corp/easywechat) 是一个由 `安正超` 大神用 PHP 开发的开源的微信非官方 SDK(现由微擎团队团队维护)。其功能强大,使用方便,个人一直很喜欢,所以近日将其在 Node.js 上实现。本人会尽量还原其配置项以及接口的调用方式,但毕竟语言环境不同,具体的实现方式会有些许差别,还请各位开发者见谅。
9
11
 
10
12
  > 注:虽然也使用了 EasyWechat 这个名称,但是和 `安正超` 大神没有任何关系,请各位开发者不要因使用本包产生的疑惑而去打扰大神,如有疑问请在本项目中提 issue,谢谢~
11
13
 
@@ -18,8 +20,6 @@
18
20
 
19
21
  绝大部分API都可以根据 [EasyWechat 的文档](https://www.easywechat.com/5.x/) 来使用。小部分(如获取请求相关数据、返回响应数据、支付证书等)的操作,由于语言环境的不同,会有不同处理。具体可以查看 [node-easywechat-demo](https://github.com/hpyer/node-easywechat-demo/) 以及下方的[自定义模块说明](#自定义模块模块替换使用方法) 。如果仍有疑问,请提issue,谢谢~
20
22
 
21
- 更多信息详见:[API文档](docs/README.md)
22
-
23
23
  ```js
24
24
  // 公众号
25
25
  let officialAccount = new EasyWechat.Factory.OfficialAccount({
@@ -36,7 +36,7 @@ let payment = new EasyWechat.Factory.Payment({
36
36
  // ...
37
37
  });
38
38
 
39
- // 微信支付
39
+ // 开放平台
40
40
  let openPlatform = new EasyWechat.Factory.OpenPlatform({
41
41
  // ...
42
42
  });
@@ -51,6 +51,11 @@ let microMerchant = new EasyWechat.Factory.MicroMerchant({
51
51
  // ...
52
52
  });
53
53
 
54
+ // 企业微信开放平台
55
+ let openWork = new EasyWechat.Factory.OpenWork({
56
+ // ...
57
+ });
58
+
54
59
  // 通用方式
55
60
  let app = EasyWechat.Factory.getInstance('OficialAccount', {
56
61
  // ...
@@ -147,6 +152,46 @@ let app = EasyWechat.Factory.getInstance('OficialAccount', {
147
152
  }
148
153
  ```
149
154
 
155
+ ``` js
156
+ // 开放平台
157
+ {
158
+ // 开放平台APPID
159
+ app_id: 'your-app-id',
160
+ // 开放平台Secret
161
+ secret: 'your-secret',
162
+ // 开放平台Token
163
+ token: 'your-token',
164
+ // 开放平台AES KEY
165
+ aes_key: 'your-aes_key',
166
+ }
167
+ ```
168
+
169
+ ``` js
170
+ // 企业微信开放平台
171
+ {
172
+ // 服务商的corpid
173
+ corp_id: 'your-corp_id',
174
+ // 服务商的secret
175
+ secret: 'your-secret',
176
+ // 应用id
177
+ suite_id: 'your-suite_id',
178
+ // 应用secret
179
+ suite_secret: 'your-suite_secret',
180
+ // 应用Token
181
+ token: 'your-token',
182
+ // 应用AESKey
183
+ aes_key: 'your-aes_key',
184
+ // 注册定制化模板ID
185
+ reg_template_id: 'your-tpl_id',
186
+ // 安装应用的回调url(可选)
187
+ redirect_uri_install: 'your-uri_install',
188
+ // 单点登录回调url(可选)
189
+ redirect_uri_single: 'your-uri_single',
190
+ // 网页授权第三方回调url(可选)
191
+ redirect_uri_oauth: 'your-uri_oauth',
192
+ }
193
+ ```
194
+
150
195
  ### 模块支持情况
151
196
 
152
197
  - [x] 公众号模块
@@ -154,7 +199,7 @@ let app = EasyWechat.Factory.getInstance('OficialAccount', {
154
199
  - [x] 小程序
155
200
  - [x] 开放平台
156
201
  - [x] 企业微信
157
- - [ ] 企业微信开放平台
202
+ - [x] 企业微信开放平台
158
203
  - [x] 小微商户
159
204
  - [x] 自定义
160
205
 
@@ -110,7 +110,7 @@ class Request {
110
110
  get(key) {
111
111
  if (!this.isValid)
112
112
  throw new Error('Please set request first. app.rebind(\'request\', new EasyWechat.Request(ctx.req));');
113
- if (this._get[key])
113
+ if (this._get[key] != undefined)
114
114
  return this._get[key];
115
115
  return this.post(key);
116
116
  }
@@ -135,7 +135,7 @@ class Request {
135
135
  this._post = (0, Utils_1.parseQueryString)(this._content.toString());
136
136
  }
137
137
  }
138
- return this._post && this._post[key] ? this._post[key] : null;
138
+ return this._post && this._post[key] != undefined ? this._post[key] : null;
139
139
  });
140
140
  }
141
141
  getAllGet() {
@@ -35,8 +35,8 @@ export default class ServerGuard {
35
35
  * 处理消息
36
36
  */
37
37
  serve(): Promise<Response>;
38
- protected validate(): Promise<this>;
39
- protected forceValidate(): this;
38
+ validate(): Promise<this>;
39
+ forceValidate(): this;
40
40
  protected resolve(): Promise<Response>;
41
41
  protected shouldReturnRawResponse(): Promise<boolean>;
42
42
  protected buildResponse(to: string, from: string, message: any): Promise<string>;
@@ -49,7 +49,7 @@ export declare interface EasyWechatConfig {
49
49
  app_id?: string;
50
50
  /**
51
51
  * 应用 secret
52
- * @scope BaseService | OfficialAccount | MiniProgram | Work
52
+ * @scope BaseService | OfficialAccount | MiniProgram | Work | OpenWork
53
53
  */
54
54
  secret?: string;
55
55
  /**
@@ -64,12 +64,12 @@ export declare interface EasyWechatConfig {
64
64
  file_cache?: EasyWechatConfigCacheFile;
65
65
  /**
66
66
  * 服务端接口验证 token
67
- * @scope OfficialAccount | MiniProgram | OpenPlatform
67
+ * @scope OfficialAccount | MiniProgram | OpenPlatform | OpenWork
68
68
  */
69
69
  token?: string;
70
70
  /**
71
71
  * 服务端消息加解密密钥 aes_key
72
- * @scope OfficialAccount | MiniProgram | OpenPlatform
72
+ * @scope OfficialAccount | MiniProgram | OpenPlatform | OpenWork
73
73
  */
74
74
  aes_key?: string;
75
75
  /**
@@ -154,7 +154,7 @@ export declare interface EasyWechatConfig {
154
154
  device_type?: string;
155
155
  /**
156
156
  * 企业id
157
- * @scope Work
157
+ * @scope Work | OpenWork
158
158
  */
159
159
  corp_id?: string;
160
160
  /**
@@ -162,6 +162,36 @@ export declare interface EasyWechatConfig {
162
162
  * @scope Work
163
163
  */
164
164
  agent_id?: string;
165
+ /**
166
+ * 应用id
167
+ * @scope OpenWork
168
+ */
169
+ suite_id?: string;
170
+ /**
171
+ * 应用secret
172
+ * @scope OpenWork
173
+ */
174
+ suite_secret?: string;
175
+ /**
176
+ * 注册定制化模板ID
177
+ * @scope OpenWork
178
+ */
179
+ reg_template_id?: string;
180
+ /**
181
+ * 安装应用的回调url(可选)
182
+ * @scope OpenWork
183
+ */
184
+ redirect_uri_install?: string;
185
+ /**
186
+ * 单点登录回调url(可选)
187
+ * @scope OpenWork
188
+ */
189
+ redirect_uri_single?: string;
190
+ /**
191
+ * 网页授权第三方回调url(可选)
192
+ * @scope OpenWork
193
+ */
194
+ redirect_uri_oauth?: string;
165
195
  }
166
196
  /**
167
197
  * 服务端通知处理回调函数
@@ -1,5 +1,9 @@
1
1
  import BaseClient from '../../Core/BaseClient';
2
2
  export default class ExpressClient extends BaseClient {
3
+ /**
4
+ * 绑定、解绑物流账号
5
+ */
6
+ bind(params: object): Promise<any>;
3
7
  /**
4
8
  * 获取支持的快递公司列表
5
9
  */
@@ -44,4 +48,18 @@ export default class ExpressClient extends BaseClient {
44
48
  * @param openid 用户openid
45
49
  */
46
50
  unbindPrinter(openid?: string): Promise<any>;
51
+ /**
52
+ * 创建退货 ID
53
+ */
54
+ createReturn(params?: object): Promise<any>;
55
+ /**
56
+ * 查询退货 ID 状态
57
+ * @param returnId 退货id
58
+ */
59
+ getReturn(returnId?: string): Promise<any>;
60
+ /**
61
+ * 解绑退货 ID
62
+ * @param returnId 退货id
63
+ */
64
+ unbindReturn(returnId?: string): Promise<any>;
47
65
  }
@@ -5,6 +5,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const BaseClient_1 = __importDefault(require("../../Core/BaseClient"));
7
7
  class ExpressClient extends BaseClient_1.default {
8
+ /**
9
+ * 绑定、解绑物流账号
10
+ */
11
+ bind(params) {
12
+ if (!params['type'] || !params['biz_id'] || !params['delivery_id']) {
13
+ throw new Error('Missing parameter.');
14
+ }
15
+ return this.httpPostJson('cgi-bin/express/business/account/bind', params);
16
+ }
8
17
  /**
9
18
  * 获取支持的快递公司列表
10
19
  */
@@ -76,5 +85,29 @@ class ExpressClient extends BaseClient_1.default {
76
85
  openid: openid,
77
86
  });
78
87
  }
88
+ /**
89
+ * 创建退货 ID
90
+ */
91
+ createReturn(params = {}) {
92
+ return this.httpPostJson('cgi-bin/express/delivery/return/add', params);
93
+ }
94
+ /**
95
+ * 查询退货 ID 状态
96
+ * @param returnId 退货id
97
+ */
98
+ getReturn(returnId = '') {
99
+ return this.httpPostJson('cgi-bin/express/delivery/return/get', {
100
+ return_id: returnId,
101
+ });
102
+ }
103
+ /**
104
+ * 解绑退货 ID
105
+ * @param returnId 退货id
106
+ */
107
+ unbindReturn(returnId = '') {
108
+ return this.httpPostJson('cgi-bin/express/delivery/return/unbind', {
109
+ return_id: returnId,
110
+ });
111
+ }
79
112
  }
80
113
  exports.default = ExpressClient;
@@ -22,6 +22,13 @@ export default class OrderClient extends BaseClient {
22
22
  * @returns
23
23
  */
24
24
  get(openid: string, orderIds: object): Promise<any>;
25
+ /**
26
+ * 获取订单列表
27
+ * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/ministore/minishopopencomponent2/API/order/get_order_list.html
28
+ * @param params 查询参数
29
+ * @returns
30
+ */
31
+ getList(params: object): Promise<any>;
25
32
  /**
26
33
  * 同步订单支付结果
27
34
  * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/ministore/minishopopencomponent2/API/order/pay_order.html
@@ -38,6 +38,15 @@ class OrderClient extends BaseClient_1.default {
38
38
  openid,
39
39
  }));
40
40
  }
41
+ /**
42
+ * 获取订单列表
43
+ * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/ministore/minishopopencomponent2/API/order/get_order_list.html
44
+ * @param params 查询参数
45
+ * @returns
46
+ */
47
+ getList(params) {
48
+ return this.httpPostJson('shop/order/get_list', params);
49
+ }
41
50
  /**
42
51
  * 同步订单支付结果
43
52
  * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/ministore/minishopopencomponent2/API/order/pay_order.html
@@ -1,5 +1,10 @@
1
1
  import BaseClient from '../../Core/BaseClient';
2
2
  export default class OfficialAccountBase extends BaseClient {
3
+ /**
4
+ * 查询openAPI调用次数
5
+ * @param cgiPath api的请求地址,例如"/cgi-bin/message/custom/send";不要前缀“https://api.weixin.qq.com” ,也不要漏了"/",否则都会76003的报错
6
+ */
7
+ getQuota(cgiPath: string): Promise<any>;
3
8
  /**
4
9
  * 清理接口调用次数
5
10
  * 此接口官方有每月调用限制,不可随意调用
@@ -6,6 +6,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const BaseClient_1 = __importDefault(require("../../Core/BaseClient"));
7
7
  const Utils_1 = require("../../Core/Utils");
8
8
  class OfficialAccountBase extends BaseClient_1.default {
9
+ /**
10
+ * 查询openAPI调用次数
11
+ * @param cgiPath api的请求地址,例如"/cgi-bin/message/custom/send";不要前缀“https://api.weixin.qq.com” ,也不要漏了"/",否则都会76003的报错
12
+ */
13
+ getQuota(cgiPath) {
14
+ return this.httpPostJson('cgi-bin/openapi/quota/get', {
15
+ cgi_path: cgiPath,
16
+ });
17
+ }
9
18
  /**
10
19
  * 清理接口调用次数
11
20
  * 此接口官方有每月调用限制,不可随意调用
@@ -97,9 +97,9 @@ class OpenPlatform extends BaseApplication_1.default {
97
97
  };
98
98
  }
99
99
  else {
100
- optional['pre_auth_code'] = yield this.createPreAuthorizationCode()['pre_auth_code'];
100
+ optional['pre_auth_code'] = (yield this.createPreAuthorizationCode())['pre_auth_code'];
101
101
  }
102
- return 'https://mp.weixin.qq.com/safe/bindcomponent?' + (0, Utils_1.buildQueryString)((0, Utils_1.merge)(optional, {
102
+ return 'https://mp.weixin.qq.com/safe/bindcomponent?' + (0, Utils_1.buildQueryString)((0, Utils_1.merge)((0, Utils_1.merge)({}, optional), {
103
103
  component_appid: this.config['app_id'],
104
104
  redirect_uri: callbackUrl,
105
105
  action: 'bindcomponent',
@@ -0,0 +1,37 @@
1
+ import BaseApplication from '../Core/BaseApplication';
2
+ import MiniProgram from '../Work/MiniProgram/Application';
3
+ import Work from './Work/Application';
4
+ import ProviderAccessToken from './Auth/ProviderAccessToken';
5
+ import SuiteAccessToken from './Auth/SuiteAccessToken';
6
+ import SuiteTicket from './Auth/SuiteTicket';
7
+ import CorpClient from './Corp/CorpClient';
8
+ import MiniProgramClient from './MiniProgram/MiniProgramClient';
9
+ import ProviderClient from './Provider/ProviderClient';
10
+ import Encryptor from '../Core/Encryptor';
11
+ import OpenWorkGuard from './Server/OpenWorkGuard';
12
+ import { EasyWechatConfig } from '../Core/Types';
13
+ export default class OpenWork extends BaseApplication {
14
+ protected defaultConfig: EasyWechatConfig;
15
+ provider_access_token: ProviderAccessToken;
16
+ suite_access_token: SuiteAccessToken;
17
+ suite_ticket: SuiteTicket;
18
+ corp: CorpClient;
19
+ mini_program: MiniProgramClient;
20
+ provider: ProviderClient;
21
+ encryptor_corp: Encryptor;
22
+ encryptor: Encryptor;
23
+ server: OpenWorkGuard;
24
+ constructor(config?: EasyWechatConfig, prepends?: Object, id?: String);
25
+ registerProviders(): void;
26
+ /**
27
+ * 代理小程序实现业务,返回SDK实例
28
+ */
29
+ miniProgram(): MiniProgram;
30
+ /**
31
+ * 代理企业微信实现业务,返回SDK实例
32
+ * @param authCorpId 企业 corp_id
33
+ * @param permanentCode 企业永久授权码
34
+ * @returns
35
+ */
36
+ work(authCorpId: string, permanentCode: string): Work;
37
+ }
@@ -0,0 +1,128 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const BaseApplication_1 = __importDefault(require("../Core/BaseApplication"));
16
+ const Application_1 = __importDefault(require("../Work/MiniProgram/Application"));
17
+ const Application_2 = __importDefault(require("./Work/Application"));
18
+ const ProviderAccessToken_1 = __importDefault(require("./Auth/ProviderAccessToken"));
19
+ const SuiteAccessToken_1 = __importDefault(require("./Auth/SuiteAccessToken"));
20
+ const SuiteTicket_1 = __importDefault(require("./Auth/SuiteTicket"));
21
+ const CorpClient_1 = __importDefault(require("./Corp/CorpClient"));
22
+ const MiniProgramClient_1 = __importDefault(require("./MiniProgram/MiniProgramClient"));
23
+ const ProviderClient_1 = __importDefault(require("./Provider/ProviderClient"));
24
+ const Encryptor_1 = __importDefault(require("../Core/Encryptor"));
25
+ const OpenWorkGuard_1 = __importDefault(require("./Server/OpenWorkGuard"));
26
+ const FinallResult_1 = __importDefault(require("../Core/Decorators/FinallResult"));
27
+ class OpenWork extends BaseApplication_1.default {
28
+ constructor(config = {}, prepends = {}, id = null) {
29
+ super(config, prepends, id);
30
+ this.defaultConfig = {
31
+ // 必要配置
32
+ app_id: '',
33
+ key: '',
34
+ http: {
35
+ baseURL: 'https://qyapi.weixin.qq.com/',
36
+ },
37
+ };
38
+ this.provider_access_token = null;
39
+ this.suite_access_token = null;
40
+ this.suite_ticket = null;
41
+ this.corp = null;
42
+ this.mini_program = null;
43
+ this.provider = null;
44
+ this.encryptor_corp = null;
45
+ this.encryptor = null;
46
+ this.server = null;
47
+ this.registerProviders();
48
+ }
49
+ registerProviders() {
50
+ super.registerCommonProviders();
51
+ if (!this.provider_access_token) {
52
+ this.offsetSet('provider_access_token', function (app) {
53
+ return new ProviderAccessToken_1.default(app);
54
+ });
55
+ }
56
+ this.offsetSet('suite_ticket', function (app) {
57
+ return new SuiteTicket_1.default(app);
58
+ });
59
+ if (!this.suite_access_token) {
60
+ this.offsetSet('suite_access_token', function (app) {
61
+ return new SuiteAccessToken_1.default(app);
62
+ });
63
+ }
64
+ if (!this.corp) {
65
+ this.offsetSet('corp', function (app) {
66
+ return new CorpClient_1.default(app);
67
+ });
68
+ }
69
+ if (!this.mini_program) {
70
+ this.offsetSet('mini_program', function (app) {
71
+ return new MiniProgramClient_1.default(app);
72
+ });
73
+ }
74
+ if (!this.provider) {
75
+ this.offsetSet('provider', function (app) {
76
+ return new ProviderClient_1.default(app);
77
+ });
78
+ }
79
+ if (!this.encryptor_corp) {
80
+ this.offsetSet('encryptor_corp', function (app) {
81
+ return new Encryptor_1.default(app.config['corp_id'], app.config['token'], app.config['aes_key']);
82
+ });
83
+ }
84
+ if (!this.encryptor) {
85
+ this.offsetSet('encryptor', function (app) {
86
+ return new Encryptor_1.default(app.config['suite_id'], app.config['token'], app.config['aes_key']);
87
+ });
88
+ }
89
+ if (!this.server) {
90
+ this.offsetSet('server', function (app) {
91
+ let guard = new OpenWorkGuard_1.default(app);
92
+ guard.push(function (payload) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ let decrypted = yield this.app.request.get('echostr');
95
+ if (decrypted) {
96
+ let str = this.app.encryptor_corp.decrypt(decrypted, this.app.request.get('msg_signature'), this.app.request.get('nonce'), this.app.request.get('timestamp'));
97
+ return new FinallResult_1.default(str);
98
+ }
99
+ // 缓存SuiteTicket
100
+ if (typeof payload['SuiteTicket'] != 'undefined') {
101
+ yield this.app.suite_ticket.setTicket(payload['SuiteTicket']);
102
+ return new FinallResult_1.default('success');
103
+ }
104
+ return null;
105
+ });
106
+ });
107
+ return guard;
108
+ });
109
+ }
110
+ }
111
+ /**
112
+ * 代理小程序实现业务,返回SDK实例
113
+ */
114
+ miniProgram() {
115
+ return new Application_1.default(this.getConfig());
116
+ }
117
+ /**
118
+ * 代理企业微信实现业务,返回SDK实例
119
+ * @param authCorpId 企业 corp_id
120
+ * @param permanentCode 企业永久授权码
121
+ * @returns
122
+ */
123
+ work(authCorpId, permanentCode) {
124
+ return new Application_2.default(authCorpId, permanentCode, this);
125
+ }
126
+ }
127
+ exports.default = OpenWork;
128
+ ;
@@ -0,0 +1,8 @@
1
+ import BaseAccessToken from '../../Core/BaseAccessToken';
2
+ export default class ProviderAccessToken extends BaseAccessToken {
3
+ protected requestMethod: string;
4
+ protected tokenKey: string;
5
+ protected endpointToGetToken: string;
6
+ protected cachePrefix: string;
7
+ protected getCredentials(): Promise<object>;
8
+ }
@@ -0,0 +1,33 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const BaseAccessToken_1 = __importDefault(require("../../Core/BaseAccessToken"));
16
+ class ProviderAccessToken extends BaseAccessToken_1.default {
17
+ constructor() {
18
+ super(...arguments);
19
+ this.requestMethod = 'POST';
20
+ this.tokenKey = 'provider_access_token';
21
+ this.endpointToGetToken = 'cgi-bin/service/get_provider_token';
22
+ this.cachePrefix = 'easywechat.kernel.provider_access_token.';
23
+ }
24
+ getCredentials() {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ return {
27
+ corpid: this.app.config.corp_id,
28
+ provider_secret: this.app.config.secret,
29
+ };
30
+ });
31
+ }
32
+ }
33
+ exports.default = ProviderAccessToken;
@@ -0,0 +1,8 @@
1
+ import BaseAccessToken from '../../Core/BaseAccessToken';
2
+ export default class SuiteAccessToken extends BaseAccessToken {
3
+ protected requestMethod: string;
4
+ protected tokenKey: string;
5
+ protected endpointToGetToken: string;
6
+ protected cachePrefix: string;
7
+ protected getCredentials(): Promise<object>;
8
+ }
@@ -0,0 +1,34 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const BaseAccessToken_1 = __importDefault(require("../../Core/BaseAccessToken"));
16
+ class SuiteAccessToken extends BaseAccessToken_1.default {
17
+ constructor() {
18
+ super(...arguments);
19
+ this.requestMethod = 'POST';
20
+ this.tokenKey = 'suite_access_token';
21
+ this.endpointToGetToken = 'cgi-bin/service/get_suite_token';
22
+ this.cachePrefix = 'easywechat.kernel.suite_access_token.';
23
+ }
24
+ getCredentials() {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ return {
27
+ suite_id: this.app.config.suite_id,
28
+ suite_secret: this.app.config.suite_secret,
29
+ suite_ticket: yield this.app['suite_ticket'].getTicket(),
30
+ };
31
+ });
32
+ }
33
+ }
34
+ exports.default = SuiteAccessToken;
@@ -0,0 +1,8 @@
1
+ import BaseApplication from '../../Core/BaseApplication';
2
+ export default class SuiteTicket {
3
+ protected app: BaseApplication;
4
+ constructor(app: BaseApplication);
5
+ setTicket(ticket: string): Promise<this>;
6
+ getTicket(): Promise<string>;
7
+ protected getCacheKey(): string;
8
+ }
@@ -0,0 +1,40 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ class SuiteTicket {
13
+ constructor(app) {
14
+ this.app = null;
15
+ this.app = app;
16
+ }
17
+ setTicket(ticket) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ let cacher = this.app.getCache();
20
+ yield cacher.set(this.getCacheKey(), ticket, 3600);
21
+ if (!(yield cacher.has(this.getCacheKey()))) {
22
+ throw new Error('Failed to cache suite ticket.');
23
+ }
24
+ return this;
25
+ });
26
+ }
27
+ getTicket() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ let cached = yield this.app.getCache().get(this.getCacheKey());
30
+ if (cached) {
31
+ return cached;
32
+ }
33
+ throw new Error('Credential "suite_ticket" does not exist in cache.');
34
+ });
35
+ }
36
+ getCacheKey() {
37
+ return 'easywechat.open_work.suite_ticket.' + this.app.config.suite_id;
38
+ }
39
+ }
40
+ exports.default = SuiteTicket;