mm_os 2.8.8 → 2.8.9
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.
- package/core/base/index.js +28 -28
- package/core/base/mqtt/index.js +314 -314
- package/core/base/mqtt/lib.js +39 -39
- package/core/base/web/index.js +109 -109
- package/core/com/mqtt/index.js +2 -6
- package/demo/index.js +28 -0
- package/middleware/cors/index.js +84 -84
- package/middleware/cors/middleware.json +6 -6
- package/middleware/log/index.js +20 -20
- package/middleware/log/middleware.json +8 -8
- package/middleware/mqtt_base/index.js +10 -10
- package/middleware/mqtt_base/middleware.json +7 -7
- package/middleware/waf/index.js +64 -64
- package/middleware/waf/middleware.json +8 -8
- package/middleware/waf_ip/index.js +115 -115
- package/middleware/waf_ip/middleware.json +9 -9
- package/middleware/web_base/index.js +65 -65
- package/middleware/web_base/middleware.json +8 -8
- package/middleware/web_event/index.js +412 -412
- package/middleware/web_event/middleware.json +9 -9
- package/middleware/web_proxy/index.js +23 -23
- package/middleware/web_proxy/middleware.json +8 -8
- package/middleware/web_router/index.js +33 -33
- package/middleware/web_router/middleware.json +9 -9
- package/middleware/web_socket/index.js +21 -21
- package/middleware/web_socket/middleware.json +8 -8
- package/middleware/web_static/index.js +25 -25
- package/middleware/web_static/middleware.json +8 -8
- package/package.json +3 -3
- package/static/file/image/user/42/2025_01_02_10_58_15.png +0 -0
|
@@ -1,413 +1,413 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 拓展类
|
|
3
|
-
* @class
|
|
4
|
-
*/
|
|
5
|
-
class Event {
|
|
6
|
-
/**
|
|
7
|
-
* 构造函数
|
|
8
|
-
* @constructor
|
|
9
|
-
* @param {Object} config 配置参数
|
|
10
|
-
*/
|
|
11
|
-
constructor(server, config) {
|
|
12
|
-
/**
|
|
13
|
-
* 事件集合
|
|
14
|
-
*/
|
|
15
|
-
this.events = {
|
|
16
|
-
/**
|
|
17
|
-
* 请求前
|
|
18
|
-
*/
|
|
19
|
-
before: [],
|
|
20
|
-
/**
|
|
21
|
-
* 验证
|
|
22
|
-
*/
|
|
23
|
-
check: [],
|
|
24
|
-
/**
|
|
25
|
-
* 主要
|
|
26
|
-
*/
|
|
27
|
-
main: [],
|
|
28
|
-
/**
|
|
29
|
-
* 渲染
|
|
30
|
-
*/
|
|
31
|
-
render: [],
|
|
32
|
-
/**
|
|
33
|
-
* 请求后
|
|
34
|
-
*/
|
|
35
|
-
after: []
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 路由集合
|
|
40
|
-
*/
|
|
41
|
-
this.routes = {
|
|
42
|
-
post: [],
|
|
43
|
-
get: []
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
$.push(server, this, true);
|
|
47
|
-
|
|
48
|
-
if (config.event && $.event_admin) {
|
|
49
|
-
// 创建一个API事件
|
|
50
|
-
var apis = $.event_admin('api', 'API事件');
|
|
51
|
-
apis.update_after = () => {
|
|
52
|
-
for (var k in this.events) {
|
|
53
|
-
this.events[k] = apis["list_" + k];
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
apis.update();
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 路由前后事件
|
|
61
|
-
*/
|
|
62
|
-
server.use(async (ctx, next) => {
|
|
63
|
-
ctx.db = {};
|
|
64
|
-
var db = ctx.db;
|
|
65
|
-
db.ret = null;
|
|
66
|
-
if (ctx.path !== "/favicon.ico") {
|
|
67
|
-
var ret = await this.run_event(ctx, this.events.before, db);
|
|
68
|
-
if (!ret) {
|
|
69
|
-
await next();
|
|
70
|
-
ret = await this.run_event(ctx, this.events.after, db) || db.ret;
|
|
71
|
-
if (ret) {
|
|
72
|
-
ctx.body = ret;
|
|
73
|
-
ctx.status = ctx.status == 404 ? 200 : ctx.status;
|
|
74
|
-
}
|
|
75
|
-
} else {
|
|
76
|
-
ctx.body = ret;
|
|
77
|
-
ctx.status = ctx.status == 404 ? 200 : ctx.status;
|
|
78
|
-
next();
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
next();
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
return server;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 执行
|
|
91
|
-
* @param {Object} ctx 请求上下文
|
|
92
|
-
* @param {Function} next 跳过当前
|
|
93
|
-
* @param {Array} list 路由列表
|
|
94
|
-
* @param {Object} db 数据管理器
|
|
95
|
-
*/
|
|
96
|
-
Event.prototype.run_event = async function(ctx, list, db) {
|
|
97
|
-
try {
|
|
98
|
-
var path = ctx.path.toLocaleLowerCase();
|
|
99
|
-
var method = ctx.method;
|
|
100
|
-
for (var i = 0; i < list.length; i++) {
|
|
101
|
-
var o = list[i];
|
|
102
|
-
var cg = o.config;
|
|
103
|
-
if (cg.method == "ALL" || cg.method == method) {
|
|
104
|
-
if (cg.state === 1 && path.has(cg.target)) {
|
|
105
|
-
var ret = await o.run(ctx, db);
|
|
106
|
-
if (ret) {
|
|
107
|
-
db.ret = ret;
|
|
108
|
-
if (cg.end) {
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
} catch (e) {
|
|
116
|
-
console.log(e);
|
|
117
|
-
}
|
|
118
|
-
return db.ret;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 执行
|
|
123
|
-
* @param {Object} ctx 请求上下文
|
|
124
|
-
* @param {Object} db 数据库管理器
|
|
125
|
-
* @param {Array} list 路由列表
|
|
126
|
-
*/
|
|
127
|
-
Event.prototype.run_route = async function(ctx, db, list) {
|
|
128
|
-
try {
|
|
129
|
-
if (list) {
|
|
130
|
-
var path = ctx.path.toLocaleLowerCase();
|
|
131
|
-
for (var i = 0; i < list.length; i++) {
|
|
132
|
-
var o = list[i];
|
|
133
|
-
var cg = o.config;
|
|
134
|
-
if (cg.state === 1 && path.has(cg.path)) {
|
|
135
|
-
var ret = await o.run(ctx, db);
|
|
136
|
-
if (ret) {
|
|
137
|
-
db.ret = ret;
|
|
138
|
-
}
|
|
139
|
-
if (cg.end) {
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
} catch (e) {
|
|
146
|
-
console.log(e);
|
|
147
|
-
}
|
|
148
|
-
return db.ret;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* 新建事件模型
|
|
153
|
-
* @param {Object} obj 附加值
|
|
154
|
-
* @return {Object} 返回模型
|
|
155
|
-
*/
|
|
156
|
-
Event.prototype.event_model = function(obj, run) {
|
|
157
|
-
return {
|
|
158
|
-
// 配置参数
|
|
159
|
-
config: Object.assign({
|
|
160
|
-
// 接受的请求方式
|
|
161
|
-
method: "ALL",
|
|
162
|
-
// 目标
|
|
163
|
-
target: "",
|
|
164
|
-
// 排序
|
|
165
|
-
sort: 100,
|
|
166
|
-
// 结束
|
|
167
|
-
end: true,
|
|
168
|
-
}, obj),
|
|
169
|
-
// 回调函数
|
|
170
|
-
run: run || function(ctx, db) {
|
|
171
|
-
return null;
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* 新建路由模型
|
|
178
|
-
* @param {Object} obj 附加值
|
|
179
|
-
* @return {Object} 返回模型
|
|
180
|
-
*/
|
|
181
|
-
Event.prototype.route_model = function(obj, run) {
|
|
182
|
-
return {
|
|
183
|
-
// 配置参数
|
|
184
|
-
config: Object.assign({
|
|
185
|
-
// 接受的请求方式
|
|
186
|
-
method: "GET",
|
|
187
|
-
// 请求路径
|
|
188
|
-
path: "",
|
|
189
|
-
// 排序
|
|
190
|
-
sort: 100,
|
|
191
|
-
// 结束
|
|
192
|
-
end: true,
|
|
193
|
-
// 缓存时长, 单位:分钟
|
|
194
|
-
cache: 0,
|
|
195
|
-
// 缓存方式 client客户端缓存, server服务端缓存
|
|
196
|
-
cache_method: "client server"
|
|
197
|
-
}, obj),
|
|
198
|
-
// 回调函数
|
|
199
|
-
run: run || function(ctx, db) {
|
|
200
|
-
return null;
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* 添加post路由
|
|
207
|
-
* @param {String} path 路由路径
|
|
208
|
-
* @param {Function} func 回调函数
|
|
209
|
-
* @param {Number} sort 排列顺序
|
|
210
|
-
*/
|
|
211
|
-
Event.prototype.post = function(path, run, sort = 100) {
|
|
212
|
-
this.routes["post"].push(this.route_model({
|
|
213
|
-
method: "POST",
|
|
214
|
-
path,
|
|
215
|
-
sort
|
|
216
|
-
}, run));
|
|
217
|
-
this.route_sort('post');
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* 添加get路由
|
|
222
|
-
* @param {String} path 路由路径
|
|
223
|
-
* @param {Function} func 回调函数
|
|
224
|
-
* @param {Number} sort 排列顺序
|
|
225
|
-
*/
|
|
226
|
-
Event.prototype.get = function(path, run, sort = 100) {
|
|
227
|
-
this.routes["get"].push(this.route_model({
|
|
228
|
-
method: "GET",
|
|
229
|
-
path,
|
|
230
|
-
sort
|
|
231
|
-
}, run));
|
|
232
|
-
this.route_sort('get');
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* 请求前
|
|
237
|
-
* @param {String} target 目标
|
|
238
|
-
* @param {Function} func 回调函数
|
|
239
|
-
* @param {Number} sort 排列顺序
|
|
240
|
-
*/
|
|
241
|
-
Event.prototype.before = function(target, run, sort = 100) {
|
|
242
|
-
this.events["before"].push(this.event_model({
|
|
243
|
-
end: false,
|
|
244
|
-
target,
|
|
245
|
-
sort
|
|
246
|
-
}, run));
|
|
247
|
-
this.event_sort('before');
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* 请求后
|
|
252
|
-
* @param {String} target 目标
|
|
253
|
-
* @param {Function} func 回调函数
|
|
254
|
-
* @param {Number} sort 排列顺序
|
|
255
|
-
*/
|
|
256
|
-
Event.prototype.check = function(target, run, sort = 100) {
|
|
257
|
-
this.events["check"].push(this.event_model({
|
|
258
|
-
target,
|
|
259
|
-
sort
|
|
260
|
-
}, run));
|
|
261
|
-
this.event_sort('check');
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* 主要
|
|
266
|
-
* @param {String} target 目标
|
|
267
|
-
* @param {Function} func 回调函数
|
|
268
|
-
* @param {Number} sort 排列顺序
|
|
269
|
-
*/
|
|
270
|
-
Event.prototype.main = function(target, run, sort = 100) {
|
|
271
|
-
this.events["main"].push(this.event_model({
|
|
272
|
-
end: false,
|
|
273
|
-
target,
|
|
274
|
-
sort
|
|
275
|
-
}, run));
|
|
276
|
-
this.event_sort('main');
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* 渲染
|
|
281
|
-
* @param {String} target 目标
|
|
282
|
-
* @param {Function} func 回调函数
|
|
283
|
-
* @param {Number} sort 排列顺序
|
|
284
|
-
*/
|
|
285
|
-
Event.prototype.render = function(target, run, sort = 100) {
|
|
286
|
-
this.events["render"].push(this.event_model({
|
|
287
|
-
target,
|
|
288
|
-
sort
|
|
289
|
-
}, run));
|
|
290
|
-
this.event_sort('render');
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* 请求后
|
|
295
|
-
* @param {String} target 目标
|
|
296
|
-
* @param {Function} func 回调函数
|
|
297
|
-
* @param {Number} sort 排列顺序
|
|
298
|
-
*/
|
|
299
|
-
Event.prototype.after = function(target, run, sort = 100) {
|
|
300
|
-
this.events["after"].push(this.event_model({
|
|
301
|
-
end: false,
|
|
302
|
-
target,
|
|
303
|
-
sort
|
|
304
|
-
}, run));
|
|
305
|
-
this.event_sort('after');
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* 路由排序方式
|
|
311
|
-
* @param {Object} obj_a 对象A
|
|
312
|
-
* @param {Object} obj_b 对象B
|
|
313
|
-
*/
|
|
314
|
-
Event.prototype.route_sort_way = function(obj_a, obj_b) {
|
|
315
|
-
var a = obj_a.config;
|
|
316
|
-
var b = obj_b.config;
|
|
317
|
-
var n = a.sort - b.sort;
|
|
318
|
-
if (n == 0) {
|
|
319
|
-
return b.path.length - a.path.length;
|
|
320
|
-
} else {
|
|
321
|
-
return n;
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* 路由排序
|
|
327
|
-
* @param {String} key 排序类型
|
|
328
|
-
*/
|
|
329
|
-
Event.prototype.route_sort = function(key) {
|
|
330
|
-
if (key) {
|
|
331
|
-
if (this.routes[key]) {
|
|
332
|
-
this.routes[key].sort(this.route_sort_way);
|
|
333
|
-
}
|
|
334
|
-
} else {
|
|
335
|
-
this.routes["post"].sort(this.route_sort_way);
|
|
336
|
-
this.routes["get"].sort(this.route_sort_way);
|
|
337
|
-
}
|
|
338
|
-
};
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* 事件排序方式
|
|
342
|
-
* @param {Object} obj_a 对象A
|
|
343
|
-
* @param {Object} obj_b 对象B
|
|
344
|
-
*/
|
|
345
|
-
Event.prototype.event_sort_way = function(obj_a, obj_b) {
|
|
346
|
-
var a = obj_a.config;
|
|
347
|
-
var b = obj_b.config;
|
|
348
|
-
var n = a.sort - b.sort;
|
|
349
|
-
if (n == 0) {
|
|
350
|
-
return b.target.length - a.target.length;
|
|
351
|
-
} else {
|
|
352
|
-
return n;
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* 事件排序
|
|
358
|
-
* @param {String} key 排序类型
|
|
359
|
-
*/
|
|
360
|
-
Event.prototype.event_sort = function(key) {
|
|
361
|
-
if (key) {
|
|
362
|
-
if (this.events[key]) {
|
|
363
|
-
this.events[key].sort(this.event_sort_way);
|
|
364
|
-
}
|
|
365
|
-
} else {
|
|
366
|
-
this.events["before"].sort(this.event_sort_way);
|
|
367
|
-
this.events["check"].sort(this.event_sort_way);
|
|
368
|
-
this.events["main"].sort(this.event_sort_way);
|
|
369
|
-
this.events["render"].sort(this.event_sort_way);
|
|
370
|
-
this.events["after"].sort(this.event_sort_way);
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* 对象转移
|
|
376
|
-
* @param {Array} lt 排序类型
|
|
377
|
-
* @param {Array} list 排序类型
|
|
378
|
-
* @param {String} paths 路径集合
|
|
379
|
-
* @param {String} key 主键
|
|
380
|
-
*/
|
|
381
|
-
Event.prototype.shift = function(lt, list, paths, key = "target") {
|
|
382
|
-
for (var i = list.length - 1; i >= 0; i--) {
|
|
383
|
-
var o = list[i];
|
|
384
|
-
var p = o.config[key];
|
|
385
|
-
for (var n = 0; n < paths.length; n++) {
|
|
386
|
-
if (p.has(paths[n])) {
|
|
387
|
-
lt.push(o);
|
|
388
|
-
list.splice(i, 1);
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* 排序
|
|
396
|
-
* @param {String} key 排序类型
|
|
397
|
-
*/
|
|
398
|
-
Event.prototype.sort = function() {
|
|
399
|
-
this.event_sort();
|
|
400
|
-
this.route_sort();
|
|
401
|
-
};
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* 应用
|
|
405
|
-
* @param {Object} server 服务
|
|
406
|
-
* @param {Object} config 配置参数
|
|
407
|
-
*/
|
|
408
|
-
module.exports = function(server, config) {
|
|
409
|
-
"./app".fullname().addDir();
|
|
410
|
-
|
|
411
|
-
server = new Event(server, config);
|
|
412
|
-
return server;
|
|
1
|
+
/**
|
|
2
|
+
* 拓展类
|
|
3
|
+
* @class
|
|
4
|
+
*/
|
|
5
|
+
class Event {
|
|
6
|
+
/**
|
|
7
|
+
* 构造函数
|
|
8
|
+
* @constructor
|
|
9
|
+
* @param {Object} config 配置参数
|
|
10
|
+
*/
|
|
11
|
+
constructor(server, config) {
|
|
12
|
+
/**
|
|
13
|
+
* 事件集合
|
|
14
|
+
*/
|
|
15
|
+
this.events = {
|
|
16
|
+
/**
|
|
17
|
+
* 请求前
|
|
18
|
+
*/
|
|
19
|
+
before: [],
|
|
20
|
+
/**
|
|
21
|
+
* 验证
|
|
22
|
+
*/
|
|
23
|
+
check: [],
|
|
24
|
+
/**
|
|
25
|
+
* 主要
|
|
26
|
+
*/
|
|
27
|
+
main: [],
|
|
28
|
+
/**
|
|
29
|
+
* 渲染
|
|
30
|
+
*/
|
|
31
|
+
render: [],
|
|
32
|
+
/**
|
|
33
|
+
* 请求后
|
|
34
|
+
*/
|
|
35
|
+
after: []
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 路由集合
|
|
40
|
+
*/
|
|
41
|
+
this.routes = {
|
|
42
|
+
post: [],
|
|
43
|
+
get: []
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
$.push(server, this, true);
|
|
47
|
+
|
|
48
|
+
if (config.event && $.event_admin) {
|
|
49
|
+
// 创建一个API事件
|
|
50
|
+
var apis = $.event_admin('api', 'API事件');
|
|
51
|
+
apis.update_after = () => {
|
|
52
|
+
for (var k in this.events) {
|
|
53
|
+
this.events[k] = apis["list_" + k];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
apis.update();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 路由前后事件
|
|
61
|
+
*/
|
|
62
|
+
server.use(async (ctx, next) => {
|
|
63
|
+
ctx.db = {};
|
|
64
|
+
var db = ctx.db;
|
|
65
|
+
db.ret = null;
|
|
66
|
+
if (ctx.path !== "/favicon.ico") {
|
|
67
|
+
var ret = await this.run_event(ctx, this.events.before, db);
|
|
68
|
+
if (!ret) {
|
|
69
|
+
await next();
|
|
70
|
+
ret = await this.run_event(ctx, this.events.after, db) || db.ret;
|
|
71
|
+
if (ret) {
|
|
72
|
+
ctx.body = ret;
|
|
73
|
+
ctx.status = ctx.status == 404 ? 200 : ctx.status;
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
ctx.body = ret;
|
|
77
|
+
ctx.status = ctx.status == 404 ? 200 : ctx.status;
|
|
78
|
+
next();
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
next();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return server;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 执行
|
|
91
|
+
* @param {Object} ctx 请求上下文
|
|
92
|
+
* @param {Function} next 跳过当前
|
|
93
|
+
* @param {Array} list 路由列表
|
|
94
|
+
* @param {Object} db 数据管理器
|
|
95
|
+
*/
|
|
96
|
+
Event.prototype.run_event = async function(ctx, list, db) {
|
|
97
|
+
try {
|
|
98
|
+
var path = ctx.path.toLocaleLowerCase();
|
|
99
|
+
var method = ctx.method;
|
|
100
|
+
for (var i = 0; i < list.length; i++) {
|
|
101
|
+
var o = list[i];
|
|
102
|
+
var cg = o.config;
|
|
103
|
+
if (cg.method == "ALL" || cg.method == method) {
|
|
104
|
+
if (cg.state === 1 && path.has(cg.target)) {
|
|
105
|
+
var ret = await o.run(ctx, db);
|
|
106
|
+
if (ret) {
|
|
107
|
+
db.ret = ret;
|
|
108
|
+
if (cg.end) {
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} catch (e) {
|
|
116
|
+
console.log(e);
|
|
117
|
+
}
|
|
118
|
+
return db.ret;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 执行
|
|
123
|
+
* @param {Object} ctx 请求上下文
|
|
124
|
+
* @param {Object} db 数据库管理器
|
|
125
|
+
* @param {Array} list 路由列表
|
|
126
|
+
*/
|
|
127
|
+
Event.prototype.run_route = async function(ctx, db, list) {
|
|
128
|
+
try {
|
|
129
|
+
if (list) {
|
|
130
|
+
var path = ctx.path.toLocaleLowerCase();
|
|
131
|
+
for (var i = 0; i < list.length; i++) {
|
|
132
|
+
var o = list[i];
|
|
133
|
+
var cg = o.config;
|
|
134
|
+
if (cg.state === 1 && path.has(cg.path)) {
|
|
135
|
+
var ret = await o.run(ctx, db);
|
|
136
|
+
if (ret) {
|
|
137
|
+
db.ret = ret;
|
|
138
|
+
}
|
|
139
|
+
if (cg.end) {
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
} catch (e) {
|
|
146
|
+
console.log(e);
|
|
147
|
+
}
|
|
148
|
+
return db.ret;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 新建事件模型
|
|
153
|
+
* @param {Object} obj 附加值
|
|
154
|
+
* @return {Object} 返回模型
|
|
155
|
+
*/
|
|
156
|
+
Event.prototype.event_model = function(obj, run) {
|
|
157
|
+
return {
|
|
158
|
+
// 配置参数
|
|
159
|
+
config: Object.assign({
|
|
160
|
+
// 接受的请求方式
|
|
161
|
+
method: "ALL",
|
|
162
|
+
// 目标
|
|
163
|
+
target: "",
|
|
164
|
+
// 排序
|
|
165
|
+
sort: 100,
|
|
166
|
+
// 结束
|
|
167
|
+
end: true,
|
|
168
|
+
}, obj),
|
|
169
|
+
// 回调函数
|
|
170
|
+
run: run || function(ctx, db) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 新建路由模型
|
|
178
|
+
* @param {Object} obj 附加值
|
|
179
|
+
* @return {Object} 返回模型
|
|
180
|
+
*/
|
|
181
|
+
Event.prototype.route_model = function(obj, run) {
|
|
182
|
+
return {
|
|
183
|
+
// 配置参数
|
|
184
|
+
config: Object.assign({
|
|
185
|
+
// 接受的请求方式
|
|
186
|
+
method: "GET",
|
|
187
|
+
// 请求路径
|
|
188
|
+
path: "",
|
|
189
|
+
// 排序
|
|
190
|
+
sort: 100,
|
|
191
|
+
// 结束
|
|
192
|
+
end: true,
|
|
193
|
+
// 缓存时长, 单位:分钟
|
|
194
|
+
cache: 0,
|
|
195
|
+
// 缓存方式 client客户端缓存, server服务端缓存
|
|
196
|
+
cache_method: "client server"
|
|
197
|
+
}, obj),
|
|
198
|
+
// 回调函数
|
|
199
|
+
run: run || function(ctx, db) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 添加post路由
|
|
207
|
+
* @param {String} path 路由路径
|
|
208
|
+
* @param {Function} func 回调函数
|
|
209
|
+
* @param {Number} sort 排列顺序
|
|
210
|
+
*/
|
|
211
|
+
Event.prototype.post = function(path, run, sort = 100) {
|
|
212
|
+
this.routes["post"].push(this.route_model({
|
|
213
|
+
method: "POST",
|
|
214
|
+
path,
|
|
215
|
+
sort
|
|
216
|
+
}, run));
|
|
217
|
+
this.route_sort('post');
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 添加get路由
|
|
222
|
+
* @param {String} path 路由路径
|
|
223
|
+
* @param {Function} func 回调函数
|
|
224
|
+
* @param {Number} sort 排列顺序
|
|
225
|
+
*/
|
|
226
|
+
Event.prototype.get = function(path, run, sort = 100) {
|
|
227
|
+
this.routes["get"].push(this.route_model({
|
|
228
|
+
method: "GET",
|
|
229
|
+
path,
|
|
230
|
+
sort
|
|
231
|
+
}, run));
|
|
232
|
+
this.route_sort('get');
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 请求前
|
|
237
|
+
* @param {String} target 目标
|
|
238
|
+
* @param {Function} func 回调函数
|
|
239
|
+
* @param {Number} sort 排列顺序
|
|
240
|
+
*/
|
|
241
|
+
Event.prototype.before = function(target, run, sort = 100) {
|
|
242
|
+
this.events["before"].push(this.event_model({
|
|
243
|
+
end: false,
|
|
244
|
+
target,
|
|
245
|
+
sort
|
|
246
|
+
}, run));
|
|
247
|
+
this.event_sort('before');
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 请求后
|
|
252
|
+
* @param {String} target 目标
|
|
253
|
+
* @param {Function} func 回调函数
|
|
254
|
+
* @param {Number} sort 排列顺序
|
|
255
|
+
*/
|
|
256
|
+
Event.prototype.check = function(target, run, sort = 100) {
|
|
257
|
+
this.events["check"].push(this.event_model({
|
|
258
|
+
target,
|
|
259
|
+
sort
|
|
260
|
+
}, run));
|
|
261
|
+
this.event_sort('check');
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* 主要
|
|
266
|
+
* @param {String} target 目标
|
|
267
|
+
* @param {Function} func 回调函数
|
|
268
|
+
* @param {Number} sort 排列顺序
|
|
269
|
+
*/
|
|
270
|
+
Event.prototype.main = function(target, run, sort = 100) {
|
|
271
|
+
this.events["main"].push(this.event_model({
|
|
272
|
+
end: false,
|
|
273
|
+
target,
|
|
274
|
+
sort
|
|
275
|
+
}, run));
|
|
276
|
+
this.event_sort('main');
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* 渲染
|
|
281
|
+
* @param {String} target 目标
|
|
282
|
+
* @param {Function} func 回调函数
|
|
283
|
+
* @param {Number} sort 排列顺序
|
|
284
|
+
*/
|
|
285
|
+
Event.prototype.render = function(target, run, sort = 100) {
|
|
286
|
+
this.events["render"].push(this.event_model({
|
|
287
|
+
target,
|
|
288
|
+
sort
|
|
289
|
+
}, run));
|
|
290
|
+
this.event_sort('render');
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 请求后
|
|
295
|
+
* @param {String} target 目标
|
|
296
|
+
* @param {Function} func 回调函数
|
|
297
|
+
* @param {Number} sort 排列顺序
|
|
298
|
+
*/
|
|
299
|
+
Event.prototype.after = function(target, run, sort = 100) {
|
|
300
|
+
this.events["after"].push(this.event_model({
|
|
301
|
+
end: false,
|
|
302
|
+
target,
|
|
303
|
+
sort
|
|
304
|
+
}, run));
|
|
305
|
+
this.event_sort('after');
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* 路由排序方式
|
|
311
|
+
* @param {Object} obj_a 对象A
|
|
312
|
+
* @param {Object} obj_b 对象B
|
|
313
|
+
*/
|
|
314
|
+
Event.prototype.route_sort_way = function(obj_a, obj_b) {
|
|
315
|
+
var a = obj_a.config;
|
|
316
|
+
var b = obj_b.config;
|
|
317
|
+
var n = a.sort - b.sort;
|
|
318
|
+
if (n == 0) {
|
|
319
|
+
return b.path.length - a.path.length;
|
|
320
|
+
} else {
|
|
321
|
+
return n;
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 路由排序
|
|
327
|
+
* @param {String} key 排序类型
|
|
328
|
+
*/
|
|
329
|
+
Event.prototype.route_sort = function(key) {
|
|
330
|
+
if (key) {
|
|
331
|
+
if (this.routes[key]) {
|
|
332
|
+
this.routes[key].sort(this.route_sort_way);
|
|
333
|
+
}
|
|
334
|
+
} else {
|
|
335
|
+
this.routes["post"].sort(this.route_sort_way);
|
|
336
|
+
this.routes["get"].sort(this.route_sort_way);
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* 事件排序方式
|
|
342
|
+
* @param {Object} obj_a 对象A
|
|
343
|
+
* @param {Object} obj_b 对象B
|
|
344
|
+
*/
|
|
345
|
+
Event.prototype.event_sort_way = function(obj_a, obj_b) {
|
|
346
|
+
var a = obj_a.config;
|
|
347
|
+
var b = obj_b.config;
|
|
348
|
+
var n = a.sort - b.sort;
|
|
349
|
+
if (n == 0) {
|
|
350
|
+
return b.target.length - a.target.length;
|
|
351
|
+
} else {
|
|
352
|
+
return n;
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 事件排序
|
|
358
|
+
* @param {String} key 排序类型
|
|
359
|
+
*/
|
|
360
|
+
Event.prototype.event_sort = function(key) {
|
|
361
|
+
if (key) {
|
|
362
|
+
if (this.events[key]) {
|
|
363
|
+
this.events[key].sort(this.event_sort_way);
|
|
364
|
+
}
|
|
365
|
+
} else {
|
|
366
|
+
this.events["before"].sort(this.event_sort_way);
|
|
367
|
+
this.events["check"].sort(this.event_sort_way);
|
|
368
|
+
this.events["main"].sort(this.event_sort_way);
|
|
369
|
+
this.events["render"].sort(this.event_sort_way);
|
|
370
|
+
this.events["after"].sort(this.event_sort_way);
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* 对象转移
|
|
376
|
+
* @param {Array} lt 排序类型
|
|
377
|
+
* @param {Array} list 排序类型
|
|
378
|
+
* @param {String} paths 路径集合
|
|
379
|
+
* @param {String} key 主键
|
|
380
|
+
*/
|
|
381
|
+
Event.prototype.shift = function(lt, list, paths, key = "target") {
|
|
382
|
+
for (var i = list.length - 1; i >= 0; i--) {
|
|
383
|
+
var o = list[i];
|
|
384
|
+
var p = o.config[key];
|
|
385
|
+
for (var n = 0; n < paths.length; n++) {
|
|
386
|
+
if (p.has(paths[n])) {
|
|
387
|
+
lt.push(o);
|
|
388
|
+
list.splice(i, 1);
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* 排序
|
|
396
|
+
* @param {String} key 排序类型
|
|
397
|
+
*/
|
|
398
|
+
Event.prototype.sort = function() {
|
|
399
|
+
this.event_sort();
|
|
400
|
+
this.route_sort();
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* 应用
|
|
405
|
+
* @param {Object} server 服务
|
|
406
|
+
* @param {Object} config 配置参数
|
|
407
|
+
*/
|
|
408
|
+
module.exports = function(server, config) {
|
|
409
|
+
"./app".fullname().addDir();
|
|
410
|
+
|
|
411
|
+
server = new Event(server, config);
|
|
412
|
+
return server;
|
|
413
413
|
};
|