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,108 @@
1
+ import ProviderInterface from "node-socialite/dist/Core/ProviderInterface";
2
+ import CacheInterface from "../../Core/Contracts/CacheInterface";
3
+ import ConfigInterface from "../../Core/Contracts/ConfigInterface";
4
+ import AccessTokenAwareClient from "../../Core/HttpClient/AccessTokenAwareClient";
5
+ import HttpClientInterface from "../../Core/HttpClient/Contracts/HttpClientInterface";
6
+ import ServerInterface from "../../Core/Contracts/ServerInterface";
7
+ import ServerRequestInterface from "../../Core/Http/Contracts/ServerRequestInterface";
8
+ import Encryptor from "../../Core/Encryptor";
9
+ import SuiteEncryptor from "../SuiteEncryptor";
10
+ import AccountInterface from "./AccountInterface";
11
+ import AccessTokenInterface from "../../Core/Contracts/AccessTokenInterface";
12
+ import SuiteTicketInterface from "./SuiteTicketInterface";
13
+ import SuiteAccessToken from "../SuiteAccessToken";
14
+ import JsApiTicket from "../JsApiTicket";
15
+ declare abstract class ApplicationInterface {
16
+ /**
17
+ * 获取当前账户实例
18
+ * @returns
19
+ */
20
+ getAccount(): AccountInterface;
21
+ /**
22
+ * 获取加密机实例
23
+ * @returns
24
+ */
25
+ getEncryptor(): Encryptor;
26
+ /**
27
+ * 获取授权应用的加密机实例
28
+ * @returns
29
+ */
30
+ getSuiteEncryptor(): SuiteEncryptor;
31
+ /**
32
+ * 获取服务端实例
33
+ * @returns
34
+ */
35
+ getServer(): ServerInterface;
36
+ /**
37
+ * 获取当前请求实例
38
+ * @returns
39
+ */
40
+ getRequest(): ServerRequestInterface;
41
+ /**
42
+ * 获取客户端实例
43
+ * @returns
44
+ */
45
+ getClient(): AccessTokenAwareClient;
46
+ /**
47
+ * 创建客户端实例
48
+ * @returns
49
+ */
50
+ createClient(): AccessTokenAwareClient;
51
+ /**
52
+ * 获取企业的客户端实例
53
+ * @returns
54
+ */
55
+ getAuthorizerClient(corpId: string, permanentCode: string, suiteAccessToken?: SuiteAccessToken): Promise<AccessTokenAwareClient>;
56
+ /**
57
+ * 获取jsapi ticket
58
+ * @returns
59
+ */
60
+ getJsApiTicket(corpId: string, permanentCode: string, suiteAccessToken?: SuiteAccessToken): Promise<JsApiTicket>;
61
+ /**
62
+ * 获取网络请求客户端实例
63
+ * @returns
64
+ */
65
+ getHttpClient(): HttpClientInterface;
66
+ /**
67
+ * 获取配置信息实例
68
+ * @returns
69
+ */
70
+ getConfig(): ConfigInterface;
71
+ /**
72
+ * 获取开放平台应用的AccessToken实例
73
+ * @returns
74
+ */
75
+ getProviderAccessToken(): AccessTokenInterface;
76
+ /**
77
+ * 获取授权应用的AccessToken实例
78
+ * @returns
79
+ */
80
+ getSuiteAccessToken(): AccessTokenInterface;
81
+ /**
82
+ * 获取授权应用的Ticket实例
83
+ * @returns
84
+ */
85
+ getSuiteTicket(): SuiteTicketInterface;
86
+ /**
87
+ * 获取缓存实例
88
+ * @returns
89
+ */
90
+ getCache(): CacheInterface;
91
+ /**
92
+ * 获取授权应用的OAuth实例
93
+ * @param suiteId
94
+ * @param suiteAccessToken
95
+ * @https://developer.work.weixin.qq.com/document/path/91120#构造第三方应用oauth2链接
96
+ * @returns
97
+ */
98
+ getOAuth(suiteId: string, suiteAccessToken?: SuiteAccessToken): Promise<ProviderInterface>;
99
+ /**
100
+ * 获取企业的OAuth实例
101
+ * @param corpId
102
+ * @param suiteAccessToken
103
+ * @see https://developer.work.weixin.qq.com/document/path/91120#构造企业oauth2链接
104
+ * @returns
105
+ */
106
+ getCorpOAuth(corpId: string, suiteAccessToken?: SuiteAccessToken): Promise<ProviderInterface>;
107
+ }
108
+ export = ApplicationInterface;
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+ class ApplicationInterface {
3
+ /**
4
+ * 获取当前账户实例
5
+ * @returns
6
+ */
7
+ getAccount() { return null; }
8
+ /**
9
+ * 获取加密机实例
10
+ * @returns
11
+ */
12
+ getEncryptor() { return null; }
13
+ /**
14
+ * 获取授权应用的加密机实例
15
+ * @returns
16
+ */
17
+ getSuiteEncryptor() { return null; }
18
+ /**
19
+ * 获取服务端实例
20
+ * @returns
21
+ */
22
+ getServer() { return null; }
23
+ /**
24
+ * 获取当前请求实例
25
+ * @returns
26
+ */
27
+ getRequest() { return null; }
28
+ /**
29
+ * 获取客户端实例
30
+ * @returns
31
+ */
32
+ getClient() { return null; }
33
+ /**
34
+ * 创建客户端实例
35
+ * @returns
36
+ */
37
+ createClient() { return null; }
38
+ /**
39
+ * 获取企业的客户端实例
40
+ * @returns
41
+ */
42
+ getAuthorizerClient(corpId, permanentCode, suiteAccessToken = null) { return null; }
43
+ /**
44
+ * 获取jsapi ticket
45
+ * @returns
46
+ */
47
+ getJsApiTicket(corpId, permanentCode, suiteAccessToken = null) { return null; }
48
+ /**
49
+ * 获取网络请求客户端实例
50
+ * @returns
51
+ */
52
+ getHttpClient() { return null; }
53
+ /**
54
+ * 获取配置信息实例
55
+ * @returns
56
+ */
57
+ getConfig() { return null; }
58
+ /**
59
+ * 获取开放平台应用的AccessToken实例
60
+ * @returns
61
+ */
62
+ getProviderAccessToken() { return null; }
63
+ /**
64
+ * 获取授权应用的AccessToken实例
65
+ * @returns
66
+ */
67
+ getSuiteAccessToken() { return null; }
68
+ /**
69
+ * 获取授权应用的Ticket实例
70
+ * @returns
71
+ */
72
+ getSuiteTicket() { return null; }
73
+ /**
74
+ * 获取缓存实例
75
+ * @returns
76
+ */
77
+ getCache() { return null; }
78
+ /**
79
+ * 获取授权应用的OAuth实例
80
+ * @param suiteId
81
+ * @param suiteAccessToken
82
+ * @https://developer.work.weixin.qq.com/document/path/91120#构造第三方应用oauth2链接
83
+ * @returns
84
+ */
85
+ getOAuth(suiteId, suiteAccessToken = null) { return null; }
86
+ /**
87
+ * 获取企业的OAuth实例
88
+ * @param corpId
89
+ * @param suiteAccessToken
90
+ * @see https://developer.work.weixin.qq.com/document/path/91120#构造企业oauth2链接
91
+ * @returns
92
+ */
93
+ getCorpOAuth(corpId, suiteAccessToken = null) { return null; }
94
+ }
95
+ ;
96
+ module.exports = ApplicationInterface;
@@ -0,0 +1,23 @@
1
+ declare abstract class SuiteTicketInterface {
2
+ /**
3
+ * 获取ticket缓存名
4
+ * @returns
5
+ */
6
+ getKey(): string;
7
+ /**
8
+ * 设置ticket缓存名
9
+ * @returns
10
+ */
11
+ setKey(key: string): this;
12
+ /**
13
+ * 设置ticket
14
+ * @returns
15
+ */
16
+ setTicket(key: string): Promise<this>;
17
+ /**
18
+ * 获取ticket
19
+ * @returns
20
+ */
21
+ getTicket(): Promise<string>;
22
+ }
23
+ export = SuiteTicketInterface;
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+ class SuiteTicketInterface {
3
+ /**
4
+ * 获取ticket缓存名
5
+ * @returns
6
+ */
7
+ getKey() { return null; }
8
+ /**
9
+ * 设置ticket缓存名
10
+ * @returns
11
+ */
12
+ setKey(key) { return null; }
13
+ /**
14
+ * 设置ticket
15
+ * @returns
16
+ */
17
+ setTicket(key) { return null; }
18
+ /**
19
+ * 获取ticket
20
+ * @returns
21
+ */
22
+ getTicket() { return null; }
23
+ }
24
+ ;
25
+ module.exports = SuiteTicketInterface;
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import BaseEncryptor from '../Core/Encryptor';
3
+ declare class Encryptor extends BaseEncryptor {
4
+ protected corpId: string;
5
+ protected token: string;
6
+ protected aesKey: string | Buffer;
7
+ constructor(corpId?: string, token?: string, aesKey?: string | Buffer);
8
+ }
9
+ export = Encryptor;
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const Encryptor_1 = __importDefault(require("../Core/Encryptor"));
6
+ class Encryptor extends Encryptor_1.default {
7
+ constructor(corpId = null, token = null, aesKey = null) {
8
+ super(corpId, token, aesKey, null);
9
+ this.corpId = corpId;
10
+ this.token = token;
11
+ this.aesKey = aesKey;
12
+ if (typeof this.aesKey === 'string') {
13
+ this.aesKey = Buffer.from(this.aesKey + '=', 'base64');
14
+ }
15
+ }
16
+ }
17
+ module.exports = Encryptor;
@@ -0,0 +1,50 @@
1
+ import CacheInterface from '../Core/Contracts/CacheInterface';
2
+ import HttpClientInterface from '../Core/HttpClient/Contracts/HttpClientInterface';
3
+ declare class JsApiTicket {
4
+ protected corpId: string;
5
+ protected key: string;
6
+ protected cache: CacheInterface;
7
+ protected httpClient: HttpClientInterface;
8
+ constructor(corpId: string, key?: string, cache?: CacheInterface, httpClient?: HttpClientInterface);
9
+ /**
10
+ * 获取签名配置
11
+ * @param url 完整URL地址
12
+ * @param nonce 随机字符串,默认:随机10位
13
+ * @param timestamp 时间长,默认:当前时间
14
+ * @returns
15
+ */
16
+ createConfigSignature(url: string, nonce?: string, timestamp?: number, jsApiList?: string[], debug?: boolean, beta?: boolean): Promise<Record<string, any>>;
17
+ protected getTicketSignature(ticket: string, nonce: string, timestamp: number, url: string): string;
18
+ /**
19
+ * 获取jsapi_ticket的缓存名称
20
+ * @returns
21
+ */
22
+ getKey(): string;
23
+ /**
24
+ * 获取签名凭证jsapi_ticket
25
+ * @returns
26
+ */
27
+ getTicket(): Promise<string>;
28
+ /**
29
+ * 获取代理应用的签名配置
30
+ * @param agentId 代理应用的id
31
+ * @param url 完整URL地址
32
+ * @param nonce 随机字符串,默认:随机10位
33
+ * @param timestamp 时间长,默认:当前时间
34
+ * @returns
35
+ */
36
+ createAgentConfigSignature(agentId: number, url: string, nonce?: string, timestamp?: number, jsApiList?: string[]): Promise<Record<string, any>>;
37
+ /**
38
+ * 获取代理应用的签名凭证jsapi_ticket
39
+ * @param agentId 代理应用的id
40
+ * @returns
41
+ */
42
+ getAgentTicket(agentId: number): Promise<string>;
43
+ /**
44
+ * 获取代理应用的jsapi_ticket的缓存名称
45
+ * @param agentId 代理应用的id
46
+ * @returns
47
+ */
48
+ getAgentKey(agentId: number): string;
49
+ }
50
+ export = JsApiTicket;
@@ -0,0 +1,152 @@
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
+ const Utils_1 = require("../Core/Support/Utils");
16
+ class JsApiTicket {
17
+ constructor(corpId, key = null, cache = null, httpClient = null) {
18
+ this.corpId = corpId;
19
+ this.key = key;
20
+ this.cache = cache;
21
+ this.httpClient = httpClient;
22
+ if (!this.httpClient) {
23
+ this.httpClient = HttpClient_1.default.create({
24
+ baseURL: 'https://qyapi.weixin.qq.com/',
25
+ });
26
+ }
27
+ }
28
+ /**
29
+ * 获取签名配置
30
+ * @param url 完整URL地址
31
+ * @param nonce 随机字符串,默认:随机10位
32
+ * @param timestamp 时间长,默认:当前时间
33
+ * @returns
34
+ */
35
+ createConfigSignature(url, nonce = null, timestamp = null, jsApiList = [], debug = false, beta = true) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ nonce = nonce || (0, Utils_1.randomString)(10);
38
+ timestamp = timestamp || (0, Utils_1.getTimestamp)();
39
+ let ticket = yield this.getTicket();
40
+ return {
41
+ appId: this.corpId,
42
+ nonceStr: nonce,
43
+ timestamp,
44
+ url,
45
+ signature: this.getTicketSignature(ticket, nonce, timestamp, url),
46
+ jsApiList,
47
+ debug,
48
+ beta,
49
+ };
50
+ });
51
+ }
52
+ getTicketSignature(ticket, nonce, timestamp, url) {
53
+ return (0, Utils_1.createHash)(`jsapi_ticket=${ticket}&noncestr=${nonce}&timestamp=${timestamp}&url=${url}`, 'sha1');
54
+ }
55
+ /**
56
+ * 获取jsapi_ticket的缓存名称
57
+ * @returns
58
+ */
59
+ getKey() {
60
+ if (!this.key) {
61
+ this.key = `open_work.jsapi_ticket.${this.corpId}`;
62
+ }
63
+ return this.key;
64
+ }
65
+ /**
66
+ * 获取签名凭证jsapi_ticket
67
+ * @returns
68
+ */
69
+ getTicket() {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ let key = this.getKey();
72
+ let ticket = '';
73
+ if (this.cache) {
74
+ ticket = yield this.cache.get(key);
75
+ }
76
+ if (!!ticket && typeof ticket === 'string') {
77
+ return ticket;
78
+ }
79
+ let response = (yield this.httpClient.request('get', '/cgi-bin/get_jsapi_ticket', {})).toObject();
80
+ if (!response['ticket']) {
81
+ throw new Error('Failed to get jssdk_ticket: ' + JSON.stringify(response));
82
+ }
83
+ if (this.cache) {
84
+ yield this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
85
+ }
86
+ return response['ticket'];
87
+ });
88
+ }
89
+ /**
90
+ * 获取代理应用的签名配置
91
+ * @param agentId 代理应用的id
92
+ * @param url 完整URL地址
93
+ * @param nonce 随机字符串,默认:随机10位
94
+ * @param timestamp 时间长,默认:当前时间
95
+ * @returns
96
+ */
97
+ createAgentConfigSignature(agentId, url, nonce = null, timestamp = null, jsApiList = []) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ nonce = nonce || (0, Utils_1.randomString)(10);
100
+ timestamp = timestamp || (0, Utils_1.getTimestamp)();
101
+ let ticket = yield this.getTicket();
102
+ return {
103
+ corpid: this.corpId,
104
+ agentid: agentId,
105
+ url,
106
+ nonceStr: nonce,
107
+ timestamp,
108
+ signature: this.getTicketSignature(ticket, nonce, timestamp, url),
109
+ jsApiList,
110
+ };
111
+ });
112
+ }
113
+ /**
114
+ * 获取代理应用的签名凭证jsapi_ticket
115
+ * @param agentId 代理应用的id
116
+ * @returns
117
+ */
118
+ getAgentTicket(agentId) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ let key = this.getAgentKey(agentId);
121
+ let ticket = '';
122
+ if (this.cache) {
123
+ ticket = yield this.cache.get(key);
124
+ }
125
+ if (!!ticket && typeof ticket === 'string') {
126
+ return ticket;
127
+ }
128
+ let response = (yield this.httpClient.request('get', '/cgi-bin/ticket/get', {
129
+ params: {
130
+ type: 'agent_config',
131
+ }
132
+ })).toObject();
133
+ if (!response['ticket']) {
134
+ throw new Error('Failed to get jssdk agentTicket: ' + JSON.stringify(response));
135
+ }
136
+ if (this.cache) {
137
+ yield this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
138
+ }
139
+ return response['ticket'];
140
+ });
141
+ }
142
+ /**
143
+ * 获取代理应用的jsapi_ticket的缓存名称
144
+ * @param agentId 代理应用的id
145
+ * @returns
146
+ */
147
+ getAgentKey(agentId) {
148
+ return `${this.getKey()}.${agentId}`;
149
+ }
150
+ }
151
+ ;
152
+ module.exports = JsApiTicket;
@@ -0,0 +1,78 @@
1
+ import BaseMessage from '../Core/Message';
2
+ declare class Message extends BaseMessage {
3
+ }
4
+ interface Message extends Record<string, any> {
5
+ /**
6
+ * 消息密文,兼容模式、安全模式才有
7
+ */
8
+ Encrypt?: string;
9
+ /**
10
+ * 开发者微信号
11
+ */
12
+ ToUserName?: string;
13
+ /**
14
+ * 发送方帐号 OpenId
15
+ */
16
+ FromUserName?: string;
17
+ /**
18
+ * 消息创建时间
19
+ */
20
+ CreateTime?: number;
21
+ /**
22
+ * 信息类型
23
+ * - `suite_ticket` [推送suite_ticket](https://developer.work.weixin.qq.com/document/path/90628)
24
+ * - `create_auth` [授权成功](https://developer.work.weixin.qq.com/document/path/90642#授权成功通知)
25
+ * - `change_auth` [变更授权](https://developer.work.weixin.qq.com/document/path/90642#变更授权通知)
26
+ * - `cancel_auth` [取消授权](https://developer.work.weixin.qq.com/document/path/90642#取消授权通知)
27
+ * - `change_contact` [成员通知事件](https://developer.work.weixin.qq.com/document/path/90639)、[部门通知事件](https://developer.work.weixin.qq.com/document/path/90640)、[标签通知事件](https://developer.work.weixin.qq.com/document/path/90641)
28
+ * - `share_agent_change` [共享应用事件回调](https://developer.work.weixin.qq.com/document/path/93373)
29
+ * - `reset_permanent_code` [重置永久授权码通知](https://developer.work.weixin.qq.com/document/path/94758)
30
+ * - `corp_arch_auth` [授权组织架构权限通知](https://developer.work.weixin.qq.com/document/path/97378)
31
+ */
32
+ InfoType?: string;
33
+ /**
34
+ * 消息类型
35
+ * - `event` 事件
36
+ */
37
+ MsgType?: string;
38
+ /**
39
+ * 事件类型
40
+ * - `change_app_admin` [应用管理员变更](https://developer.work.weixin.qq.com/document/path/95038)
41
+ * @scope MsgType='event'
42
+ */
43
+ Event?: string;
44
+ /**
45
+ * 第三方应用的SuiteId或者代开发应用模板id
46
+ */
47
+ SuiteId?: string;
48
+ /**
49
+ * 变更类型
50
+ *
51
+ * [成员通知事件](https://developer.work.weixin.qq.com/document/path/90639):
52
+ * - `create_user` 新增成员
53
+ * - `update_user` 更新成员
54
+ * - `delete_user` 删除成员
55
+ *
56
+ * [部门通知事件](https://developer.work.weixin.qq.com/document/path/90640):
57
+ * - `create_party` 新增部门
58
+ * - `update_party` 更新部门
59
+ * - `delete_party` 删除部门
60
+ *
61
+ * [标签通知事件](https://developer.work.weixin.qq.com/document/path/90641):
62
+ * - `update_tag` 标签变更
63
+ *
64
+ * @scope InfoType='change_contact'
65
+ */
66
+ ChangeType?: string;
67
+ /**
68
+ * 授权企业的CorpID
69
+ * @scope InfoType='change_contact' | 'corp_arch_auth'
70
+ */
71
+ AuthCorpId?: string;
72
+ /**
73
+ * 企业应用的id
74
+ * @scope MsgType='text' | 'image' | 'voice' | 'video' | 'location' | 'link'
75
+ */
76
+ AgentID?: string;
77
+ }
78
+ export = Message;
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const Message_1 = __importDefault(require("../Core/Message"));
6
+ class Message extends Message_1.default {
7
+ }
8
+ ;
9
+ module.exports = Message;
@@ -0,0 +1,26 @@
1
+ import CacheInterface from "../Core/Contracts/CacheInterface";
2
+ import HttpClientInterface from "../Core/HttpClient/Contracts/HttpClientInterface";
3
+ import RefreshableAccessTokenInterface from "../Core/Contracts/RefreshableAccessTokenInterface";
4
+ declare class ProviderAccessToken implements RefreshableAccessTokenInterface {
5
+ protected corpId: string;
6
+ protected providerSecret: string;
7
+ protected key: string;
8
+ protected cache: CacheInterface;
9
+ protected httpClient: HttpClientInterface;
10
+ constructor(corpId: string, providerSecret: string, key?: string, cache?: CacheInterface, httpClient?: HttpClientInterface);
11
+ /**
12
+ * 获取access_token的缓存名称
13
+ * @returns
14
+ */
15
+ getKey(): string;
16
+ /**
17
+ * 设置access_token的缓存名称
18
+ * @param key
19
+ * @returns
20
+ */
21
+ setKey(key: string): this;
22
+ getToken(): Promise<string>;
23
+ toQuery(): Promise<Record<string, any>>;
24
+ refresh(): Promise<string>;
25
+ }
26
+ export = ProviderAccessToken;
@@ -0,0 +1,84 @@
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 ProviderAccessToken {
16
+ constructor(corpId, providerSecret, key = null, cache = null, httpClient = null) {
17
+ this.corpId = corpId;
18
+ this.providerSecret = providerSecret;
19
+ this.key = key;
20
+ this.cache = cache;
21
+ this.httpClient = httpClient;
22
+ if (!this.httpClient) {
23
+ this.httpClient = HttpClient_1.default.create({
24
+ baseURL: 'https://qyapi.weixin.qq.com/',
25
+ });
26
+ }
27
+ }
28
+ /**
29
+ * 获取access_token的缓存名称
30
+ * @returns
31
+ */
32
+ getKey() {
33
+ if (!this.key) {
34
+ this.key = `open_work.access_token.${this.corpId}`;
35
+ }
36
+ return this.key;
37
+ }
38
+ /**
39
+ * 设置access_token的缓存名称
40
+ * @param key
41
+ * @returns
42
+ */
43
+ setKey(key) {
44
+ this.key = key;
45
+ return this;
46
+ }
47
+ getToken() {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ let token = '';
50
+ if (this.cache) {
51
+ token = yield this.cache.get(this.getKey());
52
+ }
53
+ if (!!token && typeof token === 'string') {
54
+ return token;
55
+ }
56
+ return this.refresh();
57
+ });
58
+ }
59
+ toQuery() {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ return {
62
+ provider_access_token: yield this.getToken(),
63
+ };
64
+ });
65
+ }
66
+ refresh() {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ let response = (yield this.httpClient.request('post', 'cgi-bin/service/get_provider_token', {
69
+ json: {
70
+ corpid: this.corpId,
71
+ provider_secret: this.providerSecret,
72
+ }
73
+ })).toObject();
74
+ if (!response['provider_access_token']) {
75
+ throw new Error('Failed to get provider_access_token: ' + JSON.stringify(response));
76
+ }
77
+ if (this.cache) {
78
+ yield this.cache.set(this.getKey(), response['provider_access_token'], parseInt(response['expires_in']) - 100);
79
+ }
80
+ return response['provider_access_token'];
81
+ });
82
+ }
83
+ }
84
+ module.exports = ProviderAccessToken;