node-easywechat 3.2.0 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/.github/workflows/publish-npm-2.yml +1 -1
  2. package/.github/workflows/publish-npm-3.yml +1 -1
  3. package/CHANGELOG.md +13 -0
  4. package/README.md +3 -3
  5. package/dist/MiniApp/Application.d.ts +4 -4
  6. package/dist/OfficialAccount/Application.d.ts +4 -4
  7. package/dist/OpenPlatform/Application.d.ts +5 -5
  8. package/dist/OpenPlatform/Application.js +1 -1
  9. package/dist/OpenWork/Account.d.ts +17 -0
  10. package/dist/OpenWork/Account.js +30 -0
  11. package/dist/OpenWork/Application.d.ts +120 -0
  12. package/dist/OpenWork/Application.js +294 -0
  13. package/dist/OpenWork/Authorization.d.ts +12 -0
  14. package/dist/OpenWork/Authorization.js +49 -0
  15. package/dist/OpenWork/AuthorizerAccessToken.d.ts +38 -0
  16. package/dist/OpenWork/AuthorizerAccessToken.js +105 -0
  17. package/dist/OpenWork/Config.d.ts +5 -0
  18. package/dist/OpenWork/Config.js +19 -0
  19. package/dist/OpenWork/Contracts/AccountInterface.d.ts +33 -0
  20. package/dist/OpenWork/Contracts/AccountInterface.js +35 -0
  21. package/dist/OpenWork/Contracts/ApplicationInterface.d.ts +108 -0
  22. package/dist/OpenWork/Contracts/ApplicationInterface.js +96 -0
  23. package/dist/OpenWork/Contracts/SuiteTicketInterface.d.ts +23 -0
  24. package/dist/OpenWork/Contracts/SuiteTicketInterface.js +25 -0
  25. package/dist/OpenWork/Encryptor.d.ts +9 -0
  26. package/dist/OpenWork/Encryptor.js +17 -0
  27. package/dist/OpenWork/JsApiTicket.d.ts +50 -0
  28. package/dist/OpenWork/JsApiTicket.js +152 -0
  29. package/dist/OpenWork/Message.d.ts +78 -0
  30. package/dist/OpenWork/Message.js +9 -0
  31. package/dist/OpenWork/ProviderAccessToken.d.ts +26 -0
  32. package/dist/OpenWork/ProviderAccessToken.js +84 -0
  33. package/dist/OpenWork/Server.d.ts +103 -0
  34. package/dist/OpenWork/Server.js +242 -0
  35. package/dist/OpenWork/SuiteAccessToken.d.ts +28 -0
  36. package/dist/OpenWork/SuiteAccessToken.js +86 -0
  37. package/dist/OpenWork/SuiteEncryptor.d.ts +9 -0
  38. package/dist/OpenWork/SuiteEncryptor.js +17 -0
  39. package/dist/OpenWork/SuiteTicket.d.ts +13 -0
  40. package/dist/OpenWork/SuiteTicket.js +49 -0
  41. package/dist/Pay/Application.d.ts +4 -4
  42. package/dist/Types/global.d.ts +11 -1
  43. package/dist/Work/Application.d.ts +5 -5
  44. package/dist/Work/Application.js +1 -1
  45. package/dist/index.d.ts +3 -2
  46. package/dist/index.js +3 -1
  47. package/package.json +2 -2
@@ -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;
@@ -0,0 +1,103 @@
1
+ import ServerRequestInterface from '../Core/Http/Contracts/ServerRequestInterface';
2
+ import Encryptor from '../Core/Encryptor';
3
+ import ServerInterface from '../Core/Contracts/ServerInterface';
4
+ import Response from '../Core/Http/Response';
5
+ import Message from './Message';
6
+ import { ServerHandlerClosure } from '../Types/global';
7
+ declare class Server extends ServerInterface {
8
+ protected encryptor: Encryptor;
9
+ protected providerEncryptor: Encryptor;
10
+ protected request: ServerRequestInterface;
11
+ protected defaultSuiteTicketHandler: ServerHandlerClosure;
12
+ constructor(encryptor: Encryptor, providerEncryptor: Encryptor, request?: ServerRequestInterface);
13
+ /**
14
+ * 服务端消息处理
15
+ * @returns
16
+ */
17
+ serve(): Promise<Response>;
18
+ withDefaultSuiteTicketHandler(handler: ServerHandlerClosure): this;
19
+ /**
20
+ * 设置联系人变化的消息处理器
21
+ * @param handler
22
+ */
23
+ handleSuiteTicketRefreshed(handler: ServerHandlerClosure): this;
24
+ /**
25
+ * 设置授权成功的消息处理器
26
+ * @param handler
27
+ */
28
+ handleAuthCreated(handler: ServerHandlerClosure): this;
29
+ /**
30
+ * 设置变更授权的消息处理器
31
+ * @param handler
32
+ */
33
+ handleAuthUpdated(handler: ServerHandlerClosure): this;
34
+ /**
35
+ * 设置取消授权的消息处理器
36
+ * @param handler
37
+ */
38
+ handleAuthCancelled(handler: ServerHandlerClosure): this;
39
+ /**
40
+ * 设置用户创建的消息处理器
41
+ * @param handler
42
+ */
43
+ handleUserCreated(handler: ServerHandlerClosure): this;
44
+ /**
45
+ * 设置用户更新的消息处理器
46
+ * @param handler
47
+ */
48
+ handleUserUpdated(handler: ServerHandlerClosure): this;
49
+ /**
50
+ * 设置用户删除的消息处理器
51
+ * @param handler
52
+ */
53
+ handleUserDeleted(handler: ServerHandlerClosure): this;
54
+ /**
55
+ * 设置部门创建的消息处理器
56
+ * @param handler
57
+ */
58
+ handlePartyCreated(handler: ServerHandlerClosure): this;
59
+ /**
60
+ * 设置部门更新的消息处理器
61
+ * @param handler
62
+ */
63
+ handlePartyUpdated(handler: ServerHandlerClosure): this;
64
+ /**
65
+ * 设置部门删除的消息处理器
66
+ * @param handler
67
+ */
68
+ handlePartyDeleted(handler: ServerHandlerClosure): this;
69
+ /**
70
+ * 设置用户标签变化的消息处理器
71
+ * @param handler
72
+ */
73
+ handleUserTagUpdated(handler: ServerHandlerClosure): this;
74
+ /**
75
+ * 设置共享应用事件的消息处理器
76
+ * @param handler
77
+ */
78
+ handleShareAgentChanged(handler: ServerHandlerClosure): this;
79
+ /**
80
+ * 设置重置永久授权码的消息处理器
81
+ * @param handler
82
+ */
83
+ handleResetPermanentCode(handler: ServerHandlerClosure): this;
84
+ /**
85
+ * 设置应用管理员变更的消息处理器
86
+ * @param handler
87
+ */
88
+ handleChangeAppAdmin(handler: ServerHandlerClosure): this;
89
+ /**
90
+ * 获取来自微信服务器的推送消息
91
+ * @param request 未设置该参数时,则从当前服务端收到的请求中获取
92
+ * @returns
93
+ */
94
+ getRequestMessage(request?: ServerRequestInterface): Promise<Message>;
95
+ protected decryptRequestMessage(query: Record<string, any>): ServerHandlerClosure;
96
+ /**
97
+ * 获取解密后的消息
98
+ * @param request 未设置该参数时,则从当前服务端收到的请求中获取
99
+ * @returns
100
+ */
101
+ getDecryptedMessage(request?: ServerRequestInterface): Promise<import("../Core/Message")>;
102
+ }
103
+ export = Server;