node-easywechat 3.1.4 → 3.3.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 (69) hide show
  1. package/.github/workflows/build-2.yml +21 -0
  2. package/.github/workflows/build-3.yml +21 -0
  3. package/.github/workflows/publish-npm-2.yml +24 -0
  4. package/.github/workflows/publish-npm-3.yml +24 -0
  5. package/CHANGELOG.md +15 -0
  6. package/README.md +23 -2
  7. package/dist/MiniApp/Application.d.ts +4 -4
  8. package/dist/OfficialAccount/Application.d.ts +4 -4
  9. package/dist/OpenPlatform/Application.d.ts +5 -5
  10. package/dist/OpenPlatform/Application.js +1 -1
  11. package/dist/OpenWork/Account.d.ts +17 -0
  12. package/dist/OpenWork/Account.js +30 -0
  13. package/dist/OpenWork/Application.d.ts +120 -0
  14. package/dist/OpenWork/Application.js +294 -0
  15. package/dist/OpenWork/Authorization.d.ts +12 -0
  16. package/dist/OpenWork/Authorization.js +49 -0
  17. package/dist/OpenWork/AuthorizerAccessToken.d.ts +38 -0
  18. package/dist/OpenWork/AuthorizerAccessToken.js +105 -0
  19. package/dist/OpenWork/Config.d.ts +5 -0
  20. package/dist/OpenWork/Config.js +19 -0
  21. package/dist/OpenWork/Contracts/AccountInterface.d.ts +33 -0
  22. package/dist/OpenWork/Contracts/AccountInterface.js +35 -0
  23. package/dist/OpenWork/Contracts/ApplicationInterface.d.ts +108 -0
  24. package/dist/OpenWork/Contracts/ApplicationInterface.js +96 -0
  25. package/dist/OpenWork/Contracts/SuiteTicketInterface.d.ts +23 -0
  26. package/dist/OpenWork/Contracts/SuiteTicketInterface.js +25 -0
  27. package/dist/OpenWork/Encryptor.d.ts +9 -0
  28. package/dist/OpenWork/Encryptor.js +17 -0
  29. package/dist/OpenWork/JsApiTicket.d.ts +50 -0
  30. package/dist/OpenWork/JsApiTicket.js +152 -0
  31. package/dist/OpenWork/Message.d.ts +78 -0
  32. package/dist/OpenWork/Message.js +9 -0
  33. package/dist/OpenWork/ProviderAccessToken.d.ts +26 -0
  34. package/dist/OpenWork/ProviderAccessToken.js +84 -0
  35. package/dist/OpenWork/Server.d.ts +103 -0
  36. package/dist/OpenWork/Server.js +242 -0
  37. package/dist/OpenWork/SuiteAccessToken.d.ts +28 -0
  38. package/dist/OpenWork/SuiteAccessToken.js +86 -0
  39. package/dist/OpenWork/SuiteEncryptor.d.ts +9 -0
  40. package/dist/OpenWork/SuiteEncryptor.js +17 -0
  41. package/dist/OpenWork/SuiteTicket.d.ts +13 -0
  42. package/dist/OpenWork/SuiteTicket.js +49 -0
  43. package/dist/Pay/Application.d.ts +4 -4
  44. package/dist/Types/global.d.ts +19 -1
  45. package/dist/Work/AccessToken.d.ts +26 -0
  46. package/dist/Work/AccessToken.js +84 -0
  47. package/dist/Work/Account.d.ts +13 -0
  48. package/dist/Work/Account.js +25 -0
  49. package/dist/Work/Application.d.ts +75 -0
  50. package/dist/Work/Application.js +177 -0
  51. package/dist/Work/Config.d.ts +5 -0
  52. package/dist/Work/Config.js +17 -0
  53. package/dist/Work/Contracts/AccountInterface.d.ts +23 -0
  54. package/dist/Work/Contracts/AccountInterface.js +25 -0
  55. package/dist/Work/Contracts/ApplicationInterface.d.ts +87 -0
  56. package/dist/Work/Contracts/ApplicationInterface.js +76 -0
  57. package/dist/Work/Encryptor.d.ts +9 -0
  58. package/dist/Work/Encryptor.js +17 -0
  59. package/dist/Work/JsApiTicket.d.ts +45 -0
  60. package/dist/Work/JsApiTicket.js +141 -0
  61. package/dist/Work/Message.d.ts +142 -0
  62. package/dist/Work/Message.js +9 -0
  63. package/dist/Work/Server.d.ts +90 -0
  64. package/dist/Work/Server.js +212 -0
  65. package/dist/Work/Utils.d.ts +25 -0
  66. package/dist/Work/Utils.js +56 -0
  67. package/dist/index.d.ts +4 -2
  68. package/dist/index.js +5 -1
  69. package/package.json +2 -2
@@ -0,0 +1,294 @@
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
+ const merge_1 = __importDefault(require("merge"));
15
+ const OpenWeWork_1 = __importDefault(require("node-socialite/dist/Providers/OpenWeWork"));
16
+ const Config_1 = __importDefault(require("./Config"));
17
+ const ConfigInterface_1 = __importDefault(require("../Core/Contracts/ConfigInterface"));
18
+ const Encryptor_1 = __importDefault(require("./Encryptor"));
19
+ const AccessTokenAwareClient_1 = __importDefault(require("../Core/HttpClient/AccessTokenAwareClient"));
20
+ const CacheMixin_1 = __importDefault(require("../Core/Mixins/CacheMixin"));
21
+ const ClientMixin_1 = __importDefault(require("../Core/Mixins/ClientMixin"));
22
+ const ConfigMixin_1 = __importDefault(require("../Core/Mixins/ConfigMixin"));
23
+ const HttpClientMixin_1 = __importDefault(require("../Core/Mixins/HttpClientMixin"));
24
+ const ServerRequestMixin_1 = __importDefault(require("../Core/Mixins/ServerRequestMixin"));
25
+ const Utils_1 = require("../Core/Support/Utils");
26
+ const Account_1 = __importDefault(require("./Account"));
27
+ const Server_1 = __importDefault(require("./Server"));
28
+ const SuiteTicket_1 = __importDefault(require("./SuiteTicket"));
29
+ const SuiteEncryptor_1 = __importDefault(require("./SuiteEncryptor"));
30
+ const SuiteAccessToken_1 = __importDefault(require("./SuiteAccessToken"));
31
+ const Authorization_1 = __importDefault(require("./Authorization"));
32
+ const AuthorizerAccessToken_1 = __importDefault(require("./AuthorizerAccessToken"));
33
+ const ProviderAccessToken_1 = __importDefault(require("./ProviderAccessToken"));
34
+ const JsApiTicket_1 = __importDefault(require("./JsApiTicket"));
35
+ /**
36
+ * 企业微信开放平台应用
37
+ */
38
+ class Application {
39
+ constructor(config) {
40
+ this.account = null;
41
+ this.encryptor = null;
42
+ this.server = null;
43
+ this.accessToken = null;
44
+ this.suiteEncryptor = null;
45
+ this.suiteAccessToken = null;
46
+ this.suiteTicket = null;
47
+ this.authorizerAccessToken = null;
48
+ if (config instanceof ConfigInterface_1.default) {
49
+ this.setConfig(config);
50
+ }
51
+ else {
52
+ this.setConfig(new Config_1.default(config));
53
+ }
54
+ }
55
+ getAccount() {
56
+ if (!this.account) {
57
+ this.account = new Account_1.default(this.config.get('corp_id'), this.config.get('provider_secret'), this.config.get('suite_id'), this.config.get('suite_secret'), this.config.get('token'), this.config.get('aes_key'));
58
+ }
59
+ return this.account;
60
+ }
61
+ /**
62
+ * 设置当前账户实例
63
+ * @param account
64
+ * @returns
65
+ */
66
+ setAccount(account) {
67
+ this.account = account;
68
+ return this;
69
+ }
70
+ getEncryptor() {
71
+ if (!this.encryptor) {
72
+ let token = this.getAccount().getToken();
73
+ let aesKey = this.getAccount().getAesKey();
74
+ if (!token || !aesKey) {
75
+ throw new Error('token or aes_key cannot be empty.');
76
+ }
77
+ this.encryptor = new Encryptor_1.default(this.getAccount().getCorpId(), token, aesKey);
78
+ }
79
+ return this.encryptor;
80
+ }
81
+ /**
82
+ * 设置加密机实例
83
+ * @param encryptor
84
+ * @returns
85
+ */
86
+ setEncryptor(encryptor) {
87
+ this.encryptor = encryptor;
88
+ return this;
89
+ }
90
+ getSuiteEncryptor() {
91
+ if (!this.suiteEncryptor) {
92
+ let token = this.getAccount().getToken();
93
+ let aesKey = this.getAccount().getAesKey();
94
+ if (!token || !aesKey) {
95
+ throw new Error('token or aes_key cannot be empty.');
96
+ }
97
+ this.suiteEncryptor = new SuiteEncryptor_1.default(this.getAccount().getSuiteId(), token, aesKey);
98
+ }
99
+ return this.suiteEncryptor;
100
+ }
101
+ /**
102
+ * 设置授权应用的加密机实例
103
+ * @param suiteEncryptor
104
+ * @returns
105
+ */
106
+ setSuiteEncryptor(suiteEncryptor) {
107
+ this.suiteEncryptor = suiteEncryptor;
108
+ return this;
109
+ }
110
+ getServer() {
111
+ if (!this.server) {
112
+ this.server = new Server_1.default(this.getSuiteEncryptor(), this.getEncryptor(), this.getRequest());
113
+ this.server.withDefaultSuiteTicketHandler((message, next) => __awaiter(this, void 0, void 0, function* () {
114
+ if (message.SuiteId === this.getAccount().getSuiteId()) {
115
+ yield this.getSuiteTicket().setTicket(message.SuiteTicket);
116
+ }
117
+ return next(message);
118
+ }));
119
+ }
120
+ return this.server;
121
+ }
122
+ /**
123
+ * 设置服务端实例
124
+ * @param server
125
+ * @returns
126
+ */
127
+ setServer(server) {
128
+ this.server = server;
129
+ return this;
130
+ }
131
+ getProviderAccessToken() {
132
+ if (!this.accessToken) {
133
+ this.accessToken = new ProviderAccessToken_1.default(this.getAccount().getCorpId(), this.getAccount().getProviderSecret(), null, this.getCache(), this.getHttpClient());
134
+ }
135
+ return this.accessToken;
136
+ }
137
+ /**
138
+ * 设置开放平台应用的AccessToken实例
139
+ * @param accessToken
140
+ * @returns
141
+ */
142
+ setProviderAccessToken(accessToken) {
143
+ this.accessToken = accessToken;
144
+ return this;
145
+ }
146
+ getSuiteAccessToken() {
147
+ if (!this.suiteAccessToken) {
148
+ this.suiteAccessToken = new SuiteAccessToken_1.default(this.getAccount().getSuiteId(), this.getAccount().getSuiteSecret(), this.getSuiteTicket(), null, this.getCache(), this.getHttpClient());
149
+ }
150
+ return this.suiteAccessToken;
151
+ }
152
+ /**
153
+ * 设置授权应用的AccessToken实例
154
+ * @param suiteAccessToken
155
+ * @returns
156
+ */
157
+ setSuiteAccessToken(suiteAccessToken) {
158
+ this.suiteAccessToken = suiteAccessToken;
159
+ return this;
160
+ }
161
+ getSuiteTicket() {
162
+ if (!this.suiteTicket) {
163
+ this.suiteTicket = new SuiteTicket_1.default(this.getAccount().getSuiteId(), this.getCache());
164
+ }
165
+ return this.suiteTicket;
166
+ }
167
+ /**
168
+ * 设置授权应用的Ticket实例
169
+ * @param suiteTicket
170
+ * @returns
171
+ */
172
+ setSuiteTicket(suiteTicket) {
173
+ this.suiteTicket = suiteTicket;
174
+ return this;
175
+ }
176
+ /**
177
+ * 获取企业授权信息
178
+ * @see https://developer.work.weixin.qq.com/document/10975#获取企业授权信息
179
+ * @param corpId 授权方corpid
180
+ * @param permanentCode 永久授权码,通过get_permanent_code获取
181
+ * @param suiteAccessToken
182
+ * @returns
183
+ */
184
+ getAuthorization(corpId, permanentCode, suiteAccessToken = null) {
185
+ return __awaiter(this, void 0, void 0, function* () {
186
+ if (!suiteAccessToken)
187
+ suiteAccessToken = this.getSuiteAccessToken();
188
+ let response = (yield this.getClient().request('post', 'cgi-bin/service/get_auth_info', {
189
+ params: {
190
+ suite_access_token: yield suiteAccessToken.getToken(),
191
+ },
192
+ json: {
193
+ auth_corpid: corpId,
194
+ permanent_code: permanentCode,
195
+ }
196
+ })).toObject();
197
+ if (!response['auth_corp_info']) {
198
+ throw new Error('Failed to get auth_corp_info: ' + JSON.stringify(response));
199
+ }
200
+ return new Authorization_1.default(response);
201
+ });
202
+ }
203
+ /**
204
+ * 获取预授权码
205
+ * @see https://developer.work.weixin.qq.com/document/10975#获取预授权码
206
+ * @param suiteAccessToken
207
+ * @returns
208
+ */
209
+ createPreAuthorizationCode(suiteAccessToken = null) {
210
+ return __awaiter(this, void 0, void 0, function* () {
211
+ if (!suiteAccessToken)
212
+ suiteAccessToken = this.getSuiteAccessToken();
213
+ let response = (yield this.getClient().request('post', 'cgi-bin/service/get_pre_auth_code', {
214
+ params: {
215
+ suite_access_token: yield suiteAccessToken.getToken(),
216
+ }
217
+ })).toObject();
218
+ if (!response['pre_auth_code']) {
219
+ throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
220
+ }
221
+ return response;
222
+ });
223
+ }
224
+ /**
225
+ * 获取企业授权令牌
226
+ * @param corpId
227
+ * @param permanentCode
228
+ * @param suiteAccessToken
229
+ * @returns
230
+ */
231
+ getAuthorizerAccessToken(corpId, permanentCode, suiteAccessToken = null) {
232
+ return __awaiter(this, void 0, void 0, function* () {
233
+ if (!suiteAccessToken)
234
+ suiteAccessToken = this.getSuiteAccessToken();
235
+ return new AuthorizerAccessToken_1.default(corpId, permanentCode, suiteAccessToken, null, this.getCache(), this.getHttpClient());
236
+ });
237
+ }
238
+ createClient() {
239
+ return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getProviderAccessToken(), (response) => { var _a; return (_a = response.toObject()['errcode']) !== null && _a !== void 0 ? _a : 0; }, this.getConfig().get('http.throw', true)))
240
+ .setPresets(this.getConfig().all());
241
+ }
242
+ getAuthorizerClient(corpId, permanentCode, suiteAccessToken = null) {
243
+ return __awaiter(this, void 0, void 0, function* () {
244
+ return (new AccessTokenAwareClient_1.default(this.getHttpClient(), yield this.getAuthorizerAccessToken(corpId, permanentCode, suiteAccessToken), (response) => { var _a; return (_a = response.toObject()['errcode']) !== null && _a !== void 0 ? _a : 0; }, this.getConfig().get('http.throw', true)))
245
+ .setPresets(this.getConfig().all());
246
+ });
247
+ }
248
+ getJsApiTicket(corpId, permanentCode, suiteAccessToken = null) {
249
+ return __awaiter(this, void 0, void 0, function* () {
250
+ return new JsApiTicket_1.default(corpId, null, this.getCache(), yield this.getAuthorizerClient(corpId, permanentCode, suiteAccessToken));
251
+ });
252
+ }
253
+ getOAuth(suiteId, suiteAccessToken = null) {
254
+ return __awaiter(this, void 0, void 0, function* () {
255
+ if (!suiteAccessToken)
256
+ suiteAccessToken = this.getSuiteAccessToken();
257
+ return (new OpenWeWork_1.default({
258
+ client_id: suiteId,
259
+ client_secret: '',
260
+ redirect: this.getConfig().get('oauth.redirect_url'),
261
+ }))
262
+ .withSuiteTicket(yield this.getSuiteTicket().getTicket())
263
+ .withSuiteAccessToken(yield suiteAccessToken.getToken())
264
+ .scopes(this.getConfig().get('oauth.scopes', 'snsapi_base'));
265
+ });
266
+ }
267
+ getCorpOAuth(corpId, suiteAccessToken = null) {
268
+ return __awaiter(this, void 0, void 0, function* () {
269
+ if (!suiteAccessToken)
270
+ suiteAccessToken = this.getSuiteAccessToken();
271
+ return (new OpenWeWork_1.default({
272
+ client_id: corpId,
273
+ client_secret: '',
274
+ redirect: this.getConfig().get('oauth.redirect_url'),
275
+ }))
276
+ .withSuiteTicket(yield this.getSuiteTicket().getTicket())
277
+ .withSuiteAccessToken(yield suiteAccessToken.getToken())
278
+ .scopes(this.getConfig().get('oauth.scopes', 'snsapi_base'));
279
+ });
280
+ }
281
+ /**
282
+ * 获取请求默认配置
283
+ * @returns
284
+ */
285
+ getHttpClientDefaultOptions() {
286
+ return (0, merge_1.default)(true, {
287
+ baseURL: 'https://api.weixin.qq.com/',
288
+ }, this.getConfig().get('http', {}));
289
+ }
290
+ }
291
+ ;
292
+ ;
293
+ (0, Utils_1.applyMixins)(Application, [ConfigMixin_1.default, CacheMixin_1.default, ClientMixin_1.default, ServerRequestMixin_1.default, HttpClientMixin_1.default]);
294
+ module.exports = Application;
@@ -0,0 +1,12 @@
1
+ import HasAttributesMixin from "../Core/Mixins/HasAttributesMixin";
2
+ declare class Authorization {
3
+ constructor(attributes?: Record<string, any>);
4
+ /**
5
+ * 获取corpid
6
+ * @returns
7
+ */
8
+ getCorpId(): string;
9
+ }
10
+ interface Authorization extends HasAttributesMixin {
11
+ }
12
+ export = Authorization;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ const merge = __importStar(require("merge"));
29
+ const HasAttributesMixin_1 = __importDefault(require("../Core/Mixins/HasAttributesMixin"));
30
+ const Utils_1 = require("../Core/Support/Utils");
31
+ class Authorization {
32
+ constructor(attributes = null) {
33
+ if (attributes) {
34
+ this.attributes = merge.recursive({}, attributes);
35
+ }
36
+ }
37
+ /**
38
+ * 获取corpid
39
+ * @returns
40
+ */
41
+ getCorpId() {
42
+ if (!this.attributes['auth_corp_info'])
43
+ return '';
44
+ return this.attributes['auth_corp_info']['corpid'] || '';
45
+ }
46
+ }
47
+ ;
48
+ (0, Utils_1.applyMixins)(Authorization, [HasAttributesMixin_1.default]);
49
+ module.exports = Authorization;
@@ -0,0 +1,38 @@
1
+ import AccessTokenInterface from "../Core/Contracts/AccessTokenInterface";
2
+ import CacheInterface from "../Core/Contracts/CacheInterface";
3
+ import RefreshableAccessTokenInterface from "../Core/Contracts/RefreshableAccessTokenInterface";
4
+ import HttpClientInterface from "../Core/HttpClient/Contracts/HttpClientInterface";
5
+ declare class AuthorizerAccessToken implements RefreshableAccessTokenInterface {
6
+ protected corpId: string;
7
+ protected permanentCodeOrAccessToken: string;
8
+ protected suiteAccessToken: AccessTokenInterface;
9
+ protected key: string;
10
+ protected cache: CacheInterface;
11
+ protected httpClient: HttpClientInterface;
12
+ constructor(corpId: string, permanentCodeOrAccessToken: string, suiteAccessToken?: AccessTokenInterface, key?: string, cache?: CacheInterface, httpClient?: HttpClientInterface);
13
+ /**
14
+ * 获取appId
15
+ * @returns
16
+ */
17
+ getCorpId(): string;
18
+ /**
19
+ * 获取access_token的缓存名称
20
+ * @returns
21
+ */
22
+ getKey(): string;
23
+ /**
24
+ * 设置access_token的缓存名称
25
+ * @param key
26
+ * @returns
27
+ */
28
+ setKey(key: string): this;
29
+ getToken(): Promise<string>;
30
+ toQuery(): Promise<Record<string, any>>;
31
+ refresh(): Promise<string>;
32
+ /**
33
+ * 转为字符串
34
+ * @returns
35
+ */
36
+ toString(): Promise<string>;
37
+ }
38
+ export = AuthorizerAccessToken;
@@ -0,0 +1,105 @@
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
+ const HttpClient_1 = __importDefault(require("../Core/HttpClient/HttpClient"));
15
+ class AuthorizerAccessToken {
16
+ constructor(corpId, permanentCodeOrAccessToken, suiteAccessToken = null, key = null, cache = null, httpClient = null) {
17
+ this.corpId = corpId;
18
+ this.permanentCodeOrAccessToken = permanentCodeOrAccessToken;
19
+ this.suiteAccessToken = suiteAccessToken;
20
+ this.key = key;
21
+ this.cache = cache;
22
+ this.httpClient = httpClient;
23
+ if (!this.httpClient) {
24
+ this.httpClient = HttpClient_1.default.create({
25
+ baseURL: 'https://qyapi.weixin.qq.com/',
26
+ });
27
+ }
28
+ }
29
+ /**
30
+ * 获取appId
31
+ * @returns
32
+ */
33
+ getCorpId() {
34
+ return this.corpId;
35
+ }
36
+ /**
37
+ * 获取access_token的缓存名称
38
+ * @returns
39
+ */
40
+ getKey() {
41
+ if (!this.key) {
42
+ this.key = `open_work.authorizer.access_token.${this.corpId}.${this.permanentCodeOrAccessToken}`;
43
+ }
44
+ return this.key;
45
+ }
46
+ /**
47
+ * 设置access_token的缓存名称
48
+ * @param key
49
+ * @returns
50
+ */
51
+ setKey(key) {
52
+ this.key = key;
53
+ return this;
54
+ }
55
+ getToken() {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ if (!this.suiteAccessToken) {
58
+ return this.permanentCodeOrAccessToken;
59
+ }
60
+ let token = '';
61
+ if (this.cache) {
62
+ token = yield this.cache.get(this.getKey());
63
+ }
64
+ if (!!token && typeof token === 'string') {
65
+ return token;
66
+ }
67
+ return this.refresh();
68
+ });
69
+ }
70
+ toQuery() {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ return {
73
+ access_token: yield this.getToken(),
74
+ };
75
+ });
76
+ }
77
+ refresh() {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ let response = (yield this.httpClient.request('post', 'cgi-bin/service/get_corp_token', {
80
+ params: {
81
+ suite_access_token: yield this.suiteAccessToken.getToken(),
82
+ },
83
+ json: {
84
+ auth_corpid: this.corpId,
85
+ permanent_code: this.permanentCodeOrAccessToken,
86
+ }
87
+ })).toObject();
88
+ if (!response['access_token']) {
89
+ throw new Error('Failed to get access_token: ' + JSON.stringify(response));
90
+ }
91
+ if (this.cache) {
92
+ yield this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']) - 100);
93
+ }
94
+ return response['access_token'];
95
+ });
96
+ }
97
+ /**
98
+ * 转为字符串
99
+ * @returns
100
+ */
101
+ toString() {
102
+ return this.getToken();
103
+ }
104
+ }
105
+ module.exports = AuthorizerAccessToken;
@@ -0,0 +1,5 @@
1
+ import BaseConfig from '../Core/Config';
2
+ declare class Config extends BaseConfig {
3
+ protected requiredKeys: string[];
4
+ }
5
+ export = Config;
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const Config_1 = __importDefault(require("../Core/Config"));
6
+ class Config extends Config_1.default {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.requiredKeys = [
10
+ 'corp_id',
11
+ 'suite_id',
12
+ 'provider_secret',
13
+ 'suite_secret',
14
+ 'token',
15
+ 'aes_key',
16
+ ];
17
+ }
18
+ }
19
+ module.exports = Config;
@@ -0,0 +1,33 @@
1
+ declare abstract class AccountInterface {
2
+ /**
3
+ * 获取corpid
4
+ * @returns
5
+ */
6
+ getCorpId(): string;
7
+ /**
8
+ * 获取provider secret
9
+ * @returns
10
+ */
11
+ getProviderSecret(): string;
12
+ /**
13
+ * 获取suite id
14
+ * @returns
15
+ */
16
+ getSuiteId(): string;
17
+ /**
18
+ * 获取suite secret
19
+ * @returns
20
+ */
21
+ getSuiteSecret(): string;
22
+ /**
23
+ * 获取token
24
+ * @returns
25
+ */
26
+ getToken(): string;
27
+ /**
28
+ * 获取aesKey
29
+ * @returns
30
+ */
31
+ getAesKey(): string;
32
+ }
33
+ export = AccountInterface;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+ class AccountInterface {
3
+ /**
4
+ * 获取corpid
5
+ * @returns
6
+ */
7
+ getCorpId() { return null; }
8
+ /**
9
+ * 获取provider secret
10
+ * @returns
11
+ */
12
+ getProviderSecret() { return null; }
13
+ /**
14
+ * 获取suite id
15
+ * @returns
16
+ */
17
+ getSuiteId() { return null; }
18
+ /**
19
+ * 获取suite secret
20
+ * @returns
21
+ */
22
+ getSuiteSecret() { return null; }
23
+ /**
24
+ * 获取token
25
+ * @returns
26
+ */
27
+ getToken() { return null; }
28
+ /**
29
+ * 获取aesKey
30
+ * @returns
31
+ */
32
+ getAesKey() { return null; }
33
+ }
34
+ ;
35
+ module.exports = AccountInterface;