node-easywechat 3.1.4 → 3.2.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.
@@ -0,0 +1,212 @@
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 ServerInterface_1 = __importDefault(require("../Core/Contracts/ServerInterface"));
15
+ const Response_1 = __importDefault(require("../Core/Http/Response"));
16
+ const Message_1 = __importDefault(require("./Message"));
17
+ class Server extends ServerInterface_1.default {
18
+ constructor(encryptor, request = null) {
19
+ super();
20
+ this.encryptor = encryptor;
21
+ this.request = request;
22
+ }
23
+ /**
24
+ * 服务端消息处理
25
+ * @returns
26
+ */
27
+ serve() {
28
+ var _a, _b, _c;
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ let query = this.request.getQueryParams();
31
+ if (!!query['echostr']) {
32
+ let echostr = this.encryptor.decrypt(query['echostr'], (_a = query['msg_signature']) !== null && _a !== void 0 ? _a : '', (_b = query['nonce']) !== null && _b !== void 0 ? _b : '', (_c = query['timestamp']) !== null && _c !== void 0 ? _c : '');
33
+ return new Response_1.default(200, { 'Content-Type': 'text/html' }, echostr);
34
+ }
35
+ let message = yield this.getRequestMessage(this.request);
36
+ this.prepend(this.decryptRequestMessage(query));
37
+ let response = yield this.handle(new Response_1.default(200, {}, 'SUCCESS'), message);
38
+ if (!(response instanceof Response_1.default)) {
39
+ response = yield this.transformToReply(response, message, this.encryptor);
40
+ }
41
+ return response;
42
+ });
43
+ }
44
+ /**
45
+ * 设置联系人变化的消息处理器
46
+ * @param handler
47
+ */
48
+ handleContactChanged(handler) {
49
+ return this.with(function (message, next) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ return message.Event === 'change_contact' ? handler(message, next) : next(message);
52
+ });
53
+ });
54
+ }
55
+ /**
56
+ * 设置用户标签变化的消息处理器
57
+ * @param handler
58
+ */
59
+ handleUserTagUpdated(handler) {
60
+ return this.with(function (message, next) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ return message.Event === 'change_contact' && message.ChangeType === 'update_tag' ? handler(message, next) : next(message);
63
+ });
64
+ });
65
+ }
66
+ /**
67
+ * 设置用户创建的消息处理器
68
+ * @param handler
69
+ */
70
+ handleUserCreated(handler) {
71
+ return this.with(function (message, next) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ return message.Event === 'change_contact' && message.ChangeType === 'create_user' ? handler(message, next) : next(message);
74
+ });
75
+ });
76
+ }
77
+ /**
78
+ * 设置用户更新的消息处理器
79
+ * @param handler
80
+ */
81
+ handleUserUpdated(handler) {
82
+ return this.with(function (message, next) {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ return message.Event === 'change_contact' && message.ChangeType === 'update_user' ? handler(message, next) : next(message);
85
+ });
86
+ });
87
+ }
88
+ /**
89
+ * 设置用户删除的消息处理器
90
+ * @param handler
91
+ */
92
+ handleUserDeleted(handler) {
93
+ return this.with(function (message, next) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ return message.Event === 'change_contact' && message.ChangeType === 'delete_user' ? handler(message, next) : next(message);
96
+ });
97
+ });
98
+ }
99
+ /**
100
+ * 设置部门创建的消息处理器
101
+ * @param handler
102
+ */
103
+ handlePartyCreated(handler) {
104
+ return this.with(function (message, next) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ return message.Event === 'change_contact' && message.ChangeType === 'create_party' ? handler(message, next) : next(message);
107
+ });
108
+ });
109
+ }
110
+ /**
111
+ * 设置部门更新的消息处理器
112
+ * @param handler
113
+ */
114
+ handlePartyUpdated(handler) {
115
+ return this.with(function (message, next) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ return message.Event === 'change_contact' && message.ChangeType === 'update_party' ? handler(message, next) : next(message);
118
+ });
119
+ });
120
+ }
121
+ /**
122
+ * 设置部门删除的消息处理器
123
+ * @param handler
124
+ */
125
+ handlePartyDeleted(handler) {
126
+ return this.with(function (message, next) {
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ return message.Event === 'change_contact' && message.ChangeType === 'delete_party' ? handler(message, next) : next(message);
129
+ });
130
+ });
131
+ }
132
+ /**
133
+ * 设置异步任务完成的消息处理器
134
+ * @param handler
135
+ */
136
+ handleBatchJobsFinished(handler) {
137
+ return this.with(function (message, next) {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ return message.Event === 'batch_job_result' ? handler(message, next) : next(message);
140
+ });
141
+ });
142
+ }
143
+ /**
144
+ * 添加普通消息处理器
145
+ * @param type
146
+ * @param handler
147
+ * @returns
148
+ */
149
+ addMessageListener(type, handler) {
150
+ return this.withHandler(function (message, next) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ return message.MsgType === type ? handler(message, next) : next(message);
153
+ });
154
+ });
155
+ }
156
+ /**
157
+ * 添加事件消息处理器
158
+ * @param event
159
+ * @param handler
160
+ * @returns
161
+ */
162
+ addEventListener(event, handler) {
163
+ return this.withHandler(function (message, next) {
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ return message.Event === event ? handler(message, next) : next(message);
166
+ });
167
+ });
168
+ }
169
+ /**
170
+ * 获取来自微信服务器的推送消息
171
+ * @param request 未设置该参数时,则从当前服务端收到的请求中获取
172
+ * @returns
173
+ */
174
+ getRequestMessage(request = null) {
175
+ return Message_1.default.createFromRequest(request !== null && request !== void 0 ? request : this.request);
176
+ }
177
+ validateUrl() {
178
+ return (message, next) => __awaiter(this, void 0, void 0, function* () {
179
+ var _a, _b, _c;
180
+ let query = this.request.getQueryParams();
181
+ if (!this.encryptor)
182
+ return null;
183
+ let echostr = this.encryptor.decrypt(query['echostr'], (_a = query['msg_signature']) !== null && _a !== void 0 ? _a : '', (_b = query['timestamp']) !== null && _b !== void 0 ? _b : '', (_c = query['nonce']) !== null && _c !== void 0 ? _c : '');
184
+ return new Response_1.default(200, { 'Content-Type': 'text/html' }, echostr);
185
+ });
186
+ }
187
+ decryptRequestMessage(query) {
188
+ return (message, next) => __awaiter(this, void 0, void 0, function* () {
189
+ var _a, _b, _c;
190
+ if (!this.encryptor)
191
+ return null;
192
+ yield this.decryptMessage(message, this.encryptor, (_a = query['msg_signature']) !== null && _a !== void 0 ? _a : '', (_b = query['timestamp']) !== null && _b !== void 0 ? _b : '', (_c = query['nonce']) !== null && _c !== void 0 ? _c : '');
193
+ return next(message);
194
+ });
195
+ }
196
+ /**
197
+ * 获取解密后的消息
198
+ * @param request 未设置该参数时,则从当前服务端收到的请求中获取
199
+ * @returns
200
+ */
201
+ getDecryptedMessage(request = null) {
202
+ var _a, _b, _c;
203
+ return __awaiter(this, void 0, void 0, function* () {
204
+ request = request !== null && request !== void 0 ? request : this.request;
205
+ let message = yield this.getRequestMessage(request);
206
+ let query = request.getQueryParams();
207
+ return yield this.decryptMessage(message, this.encryptor, (_a = query['msg_signature']) !== null && _a !== void 0 ? _a : '', (_b = query['timestamp']) !== null && _b !== void 0 ? _b : '', (_c = query['nonce']) !== null && _c !== void 0 ? _c : '');
208
+ });
209
+ }
210
+ }
211
+ ;
212
+ module.exports = Server;
@@ -0,0 +1,25 @@
1
+ import Application from './Application';
2
+ declare class Utils {
3
+ protected app: Application;
4
+ constructor(app: Application);
5
+ /**
6
+ * 构建jssdk配置
7
+ * @param url 完整URL地址
8
+ * @param jsApiList api列表,默认:[]。可用列表:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#63
9
+ * @param openTagList 开放标签列表,默认:[]。可用列表:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#附录-所有开放标签列表
10
+ * @param debug 是否开启调试模式,默认:false
11
+ * @returns
12
+ */
13
+ buildJsSdkConfig(url: string, jsApiList?: string[], openTagList?: string[], debug?: boolean): Promise<Record<string, any>>;
14
+ /**
15
+ * 构建代理应用的jssdk配置
16
+ * @param agentId 代理应用id
17
+ * @param url 完整URL地址
18
+ * @param jsApiList api列表,默认:[]。可用列表:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#63
19
+ * @param openTagList 开放标签列表,默认:[]。可用列表:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#附录-所有开放标签列表
20
+ * @param debug 是否开启调试模式,默认:false
21
+ * @returns
22
+ */
23
+ buildJsSdkAgentConfig(agentId: number, url: string, jsApiList?: string[], openTagList?: string[], debug?: boolean): Promise<Record<string, any>>;
24
+ }
25
+ export = Utils;
@@ -0,0 +1,56 @@
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
+ class Utils {
16
+ constructor(app) {
17
+ this.app = app;
18
+ }
19
+ /**
20
+ * 构建jssdk配置
21
+ * @param url 完整URL地址
22
+ * @param jsApiList api列表,默认:[]。可用列表:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#63
23
+ * @param openTagList 开放标签列表,默认:[]。可用列表:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#附录-所有开放标签列表
24
+ * @param debug 是否开启调试模式,默认:false
25
+ * @returns
26
+ */
27
+ buildJsSdkConfig(url, jsApiList = [], openTagList = [], debug = false) {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ return (0, merge_1.default)({
30
+ jsApiList,
31
+ openTagList,
32
+ debug,
33
+ }, yield this.app.getTicket().createConfigSignature(url));
34
+ });
35
+ }
36
+ /**
37
+ * 构建代理应用的jssdk配置
38
+ * @param agentId 代理应用id
39
+ * @param url 完整URL地址
40
+ * @param jsApiList api列表,默认:[]。可用列表:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#63
41
+ * @param openTagList 开放标签列表,默认:[]。可用列表:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#附录-所有开放标签列表
42
+ * @param debug 是否开启调试模式,默认:false
43
+ * @returns
44
+ */
45
+ buildJsSdkAgentConfig(agentId, url, jsApiList = [], openTagList = [], debug = false) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ return (0, merge_1.default)({
48
+ jsApiList,
49
+ openTagList,
50
+ debug,
51
+ }, yield this.app.getTicket().createAgentConfigSignature(agentId, url));
52
+ });
53
+ }
54
+ }
55
+ ;
56
+ module.exports = Utils;
package/dist/index.d.ts CHANGED
@@ -1,12 +1,13 @@
1
- import { OfficialAccountConfig, MiniAppConfig, LogHandler, ServerEventType, ServerHandlerClosure, PayConfig, OpenPlatformConfig } from './Types/global';
1
+ import { OfficialAccountConfig, MiniAppConfig, LogHandler, ServerEventType, ServerHandlerClosure, PayConfig, OpenPlatformConfig, WorkConfig } from './Types/global';
2
2
  import OfficialAccount from './OfficialAccount/Application';
3
3
  import MiniApp from './MiniApp/Application';
4
4
  import Pay from './Pay/Application';
5
5
  import OpenPlatform from './OpenPlatform/Application';
6
+ import Work from './Work/Application';
6
7
  import CacheInterface from './Core/Contracts/CacheInterface';
7
8
  import ServerRequest from './Core/Http/ServerRequest';
8
9
  import FormData from 'form-data';
9
- export { OfficialAccount, OfficialAccountConfig, MiniApp, MiniAppConfig, Pay, PayConfig, OpenPlatform, OpenPlatformConfig, CacheInterface, ServerRequest, LogHandler, ServerEventType, ServerHandlerClosure,
10
+ export { OfficialAccount, OfficialAccountConfig, MiniApp, MiniAppConfig, Pay, PayConfig, OpenPlatform, OpenPlatformConfig, Work, WorkConfig, CacheInterface, ServerRequest, LogHandler, ServerEventType, ServerHandlerClosure,
10
11
  /**
11
12
  * 表单对象
12
13
  * @see https://github.com/axios/axios#formdata
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.FormData = exports.ServerRequest = exports.CacheInterface = exports.OpenPlatform = exports.Pay = exports.MiniApp = exports.OfficialAccount = void 0;
6
+ exports.FormData = exports.ServerRequest = exports.CacheInterface = exports.Work = exports.OpenPlatform = exports.Pay = exports.MiniApp = exports.OfficialAccount = void 0;
7
7
  const Application_1 = __importDefault(require("./OfficialAccount/Application"));
8
8
  exports.OfficialAccount = Application_1.default;
9
9
  const Application_2 = __importDefault(require("./MiniApp/Application"));
@@ -12,6 +12,8 @@ const Application_3 = __importDefault(require("./Pay/Application"));
12
12
  exports.Pay = Application_3.default;
13
13
  const Application_4 = __importDefault(require("./OpenPlatform/Application"));
14
14
  exports.OpenPlatform = Application_4.default;
15
+ const Application_5 = __importDefault(require("./Work/Application"));
16
+ exports.Work = Application_5.default;
15
17
  const CacheInterface_1 = __importDefault(require("./Core/Contracts/CacheInterface"));
16
18
  exports.CacheInterface = CacheInterface_1.default;
17
19
  const ServerRequest_1 = __importDefault(require("./Core/Http/ServerRequest"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-easywechat",
3
- "version": "3.1.4",
3
+ "version": "3.2.0",
4
4
  "description": "EasyWechat SDK for Node.js (NOT OFFICIAL)",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {