mm_os 4.2.6 → 4.2.7

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 (74) hide show
  1. package/README.md +1 -1
  2. package/README_EN.md +2 -2
  3. package/adapter/adapter.js +105 -52
  4. package/adapter/custom_persistence.js +32 -31
  5. package/adapter/mqtt.js +59 -79
  6. package/adapter/socket.js +188 -58
  7. package/adapter/web.js +92 -68
  8. package/adapter/websocket.js +43 -146
  9. package/com/api/drive.js +732 -708
  10. package/com/api/index.js +196 -198
  11. package/com/cmd/config.tpl.json +48 -122
  12. package/com/cmd/drive.js +176 -1548
  13. package/com/cmd/index.js +287 -1066
  14. package/com/cmd/job/config.json +66 -0
  15. package/com/cmd/job/index.js +241 -0
  16. package/com/cmd/job/test.js +188 -0
  17. package/com/cmd/script.tpl.js +12 -33
  18. package/com/db/drive.js +2206 -2019
  19. package/com/db/index.js +241 -242
  20. package/com/event/config.tpl.json +3 -1
  21. package/com/event/drive.js +3 -1
  22. package/com/event/index.js +73 -49
  23. package/com/event/script.tpl.js +1 -1
  24. package/com/mqtt/config.tpl.json +16 -8
  25. package/com/mqtt/drive.js +696 -676
  26. package/com/mqtt/index.js +1012 -822
  27. package/com/mqtt/index.test.js +503 -0
  28. package/com/mqtt/mm_mqtt.js +446 -420
  29. package/com/mqtt/script.tpl.js +3 -7
  30. package/com/nav/drive.js +7 -7
  31. package/com/nav/index.js +228 -231
  32. package/com/param/index.js +19 -17
  33. package/com/pendant/index.js +467 -441
  34. package/com/socket/drive.js +679 -616
  35. package/com/socket/index.js +94 -48
  36. package/com/sql/drive.js +1283 -1259
  37. package/com/sql/index.js +152 -150
  38. package/com/static/drive.js +1 -1
  39. package/com/static/index.js +232 -226
  40. package/com/task/drive.js +428 -405
  41. package/com/task/index.js +154 -148
  42. package/com/template/drive.js +1 -1
  43. package/com/template/index.js +141 -138
  44. package/com.js +87 -6
  45. package/common/middleware/web_after/index.js +4 -10
  46. package/common/middleware/web_base/index.js +1 -1
  47. package/common/middleware/web_cors/index.js +1 -1
  48. package/common/middleware/web_error/index.js +131 -119
  49. package/common/middleware/web_error/middleware.json +2 -1
  50. package/common/middleware/web_logger/index.js +1 -1
  51. package/common/middleware/web_main/index.js +2 -0
  52. package/common/middleware/web_static/index.js +2 -2
  53. package/common/middleware/web_waf/index.js +1 -1
  54. package/core/app/config.tpl.json +4 -4
  55. package/core/app/index.js +710 -534
  56. package/core/middleware/index.js +1 -9
  57. package/core/mod/config.tpl.json +1 -1
  58. package/core/model/config.tpl.json +1 -1
  59. package/core/plugin/config.tpl.json +15 -3
  60. package/core/plugin/index.js +31 -0
  61. package/core/pusher/index.js +158 -161
  62. package/core/scene/loop.js +1306 -1255
  63. package/core/store/index.js +1794 -1755
  64. package/core/store/sql.js +1488 -1464
  65. package/core/zone/bat/index.js +744 -728
  66. package/core/zone/config.tpl.json +3 -3
  67. package/core/zone/index.js +655 -616
  68. package/os.js +0 -4
  69. package/package.json +16 -16
  70. package/server.js +583 -427
  71. package/com/cmd/nlp.js +0 -525
  72. package/core/app/index copy.js +0 -611
  73. package/software.js +0 -194
  74. package/tcp.js +0 -279
@@ -1,441 +1,467 @@
1
- const Manager = require('mm_machine').Manager;
2
- const Drive = require('./drive');
3
-
4
- /**
5
- * Pendant 挂件类
6
- * @augments {Manager}
7
- * @class
8
- */
9
- class Pendant extends Manager {
10
- /**
11
- * 配置参数
12
- * @type {object}
13
- */
14
- static config = {
15
- /**
16
- * 名称
17
- * @type {string}
18
- */
19
- name: '',
20
- /**
21
- * 标题
22
- * @type {string}
23
- */
24
- title: '数据库管理',
25
- /**
26
- * 描述
27
- * @type {string}
28
- */
29
- description: '这是数据库管理器',
30
- /**
31
- * 检索文件名
32
- * @type {string}
33
- */
34
- filename: 'pendant.json',
35
- /**
36
- * 模板目录
37
- * @type {string}
38
- */
39
- tpl_dir: __dirname,
40
- /**
41
- * 基础目录
42
- * @type {string}
43
- */
44
- base_dir: '../common/pendant'.fullname(__dirname),
45
- /**
46
- * 自定义目录,加载项目自定义资源
47
- * @type {string}
48
- */
49
- dir: './app'.fullname(),
50
- /**
51
- * 搜索模式 dir按目录搜索 | file按文件名搜索
52
- * @type {string}
53
- */
54
- search_way: 'file',
55
- /**
56
- * 是否懒加载
57
- * @type {boolean}
58
- */
59
- lazy_load: true,
60
- /**
61
- * 模式
62
- * 1.生产模式,改变文件不会重新加载
63
- * 2.热更新模式,改变配置文件会重新加载配置,不重新加载脚本
64
- * 3.热重载模式,改变配置文件都会加载配置和脚本
65
- * 4.重载模式,执行完后重新加载脚本,避免变量污染
66
- * 5.热更新+重载模式,改变配置文件重新加载配置和脚本,执行完后重新加载脚本
67
- * @type {number}
68
- */
69
- mode: 3
70
- };
71
-
72
- /**
73
- * 构造函数
74
- * @param {object} config 配置参数
75
- * @param {object} parent 父级模块
76
- */
77
- constructor(config, parent) {
78
- super({ ...Pendant.config, ...config }, parent);
79
- }
80
- }
81
-
82
- /**
83
- * 挂件驱动类
84
- * @type {Drive}
85
- */
86
- Pendant.prototype.Drive = Drive;
87
-
88
- /**
89
- * 加载列表
90
- * @param {Array} list 文件列表
91
- */
92
- Pendant.prototype.loadList = async function (list) {
93
- var _this = this;
94
-
95
- // 遍历文件路径
96
- for (var i = 0; i < list.length; i++) {
97
- var file = list[i];
98
- var dir = file.dirname();
99
- // 载入文件
100
- var obj = file.loadJson(dir);
101
- if (obj) {
102
- if (obj.constructor == Array) {
103
- for (var n = 0; n < obj.length; n++) {
104
- var o = obj[n];
105
- // 实例化一个驱动
106
- await _this.load(dir, o, file);
107
- }
108
- } else {
109
- await _this.load(dir, obj, file);
110
- }
111
- } else {
112
- var fl = _this.dir_base + '/config.tpl.json';
113
- if (fl.hasFile()) {
114
- var text = fl.loadText();
115
- if (text) {
116
- var l = $.slash;
117
- var name = file.between('Pendant' + l, l);
118
- text = text.replaceAll('{0}', name);
119
- var app = file.between('app' + l, l);
120
- text = text.replaceAll('{1}', app);
121
- file.saveText(text);
122
- }
123
- }
124
- }
125
- }
126
- };
127
-
128
- /**
129
- * 生成字体样式
130
- * @param {object} o 样式对象
131
- * @returns {string} 字体样式
132
- */
133
- Pendant.prototype._generateFontStyle = function (o) {
134
- var style = '';
135
- if (o.font_size) {
136
- style += `\n font-size: ${o.font_size};`;
137
- }
138
- if (o.font_color) {
139
- style += `\n color: ${o.font_color};`;
140
- }
141
- return style;
142
- };
143
-
144
- /**
145
- * 生成边框样式
146
- * @param {object} o 样式对象
147
- * @returns {string} 边框样式
148
- */
149
- Pendant.prototype._generateBorderStyle = function (o) {
150
- var borders = [
151
- { pos: 'top', style: o.border_top_style, size: o.border_top_size, color: o.border_top_color },
152
- { pos: 'right', style: o.border_right_style, size: o.border_right_size, color: o.border_right_color },
153
- { pos: 'bottom', style: o.border_bottom_style, size: o.border_bottom_size, color: o.border_bottom_color },
154
- { pos: 'left', style: o.border_left_style, size: o.border_left_size, color: o.border_left_color }
155
- ];
156
- var style = '';
157
- for (var i = 0; i < borders.length; i++) {
158
- var b = borders[i];
159
- if (b.style !== 'none') {
160
- style += `\n border-${b.pos}: ${b.size || 0} ${b.style || 'solid'} ${b.color || '#dbdbdb'};`;
161
- }
162
- }
163
- return style;
164
- };
165
-
166
- /**
167
- * 生成边距样式
168
- * @param {object} o 样式对象
169
- * @returns {string} 边距样式
170
- */
171
- Pendant.prototype._generateMarginStyle = function (o) {
172
- var style = '';
173
- if (o.margin_top) {
174
- style += `\n margin-top: ${o.margin_top};`;
175
- }
176
- if (o.margin_right) {
177
- style += `\n margin-right: ${o.margin_right};`;
178
- }
179
- if (o.margin_bottom) {
180
- style += `\n margin-bottom: ${o.margin_bottom};`;
181
- }
182
- if (o.margin_left) {
183
- style += `\n margin-left: ${o.margin_left};`;
184
- }
185
- return style;
186
- };
187
-
188
- /**
189
- * 生成填充样式
190
- * @param {object} o 样式对象
191
- * @returns {string} 填充样式
192
- */
193
- Pendant.prototype._generatePaddingStyle = function (o) {
194
- var style = '';
195
- if (o.padding_top) {
196
- style += `\n padding-top: ${o.padding_top};`;
197
- }
198
- if (o.padding_right) {
199
- style += `\n padding-right: ${o.padding_right};`;
200
- }
201
- if (o.padding_bottom) {
202
- style += `\n padding-bottom: ${o.padding_bottom};`;
203
- }
204
- if (o.padding_left) {
205
- style += `\n padding-left: ${o.padding_left};`;
206
- }
207
- return style;
208
- };
209
-
210
- /**
211
- * 生成圆角样式
212
- * @param {object} o 样式对象
213
- * @returns {string} 圆角样式
214
- */
215
- Pendant.prototype._generateRadiusStyle = function (o) {
216
- var style = '';
217
- if (o.radius_top_right) {
218
- style += `\n border-top-right-radius: ${o.radius_top_right};`;
219
- }
220
- if (o.radius_top_left) {
221
- style += `\n border-top-left-radius: ${o.radius_top_left};`;
222
- }
223
- if (o.radius_bottom_right) {
224
- style += `\n border-bottom-right-radius: ${o.radius_bottom_right};`;
225
- }
226
- if (o.radius_bottom_left) {
227
- style += `\n border-bottom-left-radius: ${o.radius_bottom_left};`;
228
- }
229
- return style;
230
- };
231
-
232
- /**
233
- * 生成背景样式
234
- * @param {object} o 样式对象
235
- * @returns {string} 背景样式
236
- */
237
- Pendant.prototype._generateBgStyle = function (o) {
238
- var style = '';
239
- if (o.bg_url) {
240
- style += `\n background-image: url(${o.bg_url});`;
241
- style += `\n background-size: 100% auto;`;
242
- style += `\n background-repeat: ${o.bg_repeat || 'no-repeat'};`;
243
- }
244
- if (o.bg_color) {
245
- style += `\n background-color: ${o.bg_color || ''};`;
246
- }
247
- return style;
248
- };
249
-
250
- /**
251
- * 生成链接样式
252
- * @param {string} id 元素ID
253
- * @param {object} o 样式对象
254
- * @returns {string} 链接样式
255
- */
256
- Pendant.prototype._generateLinkStyle = function (id, o) {
257
- var style = '';
258
- if (o.a_size || o.a_color) {
259
- style += `\n#${id} a {`;
260
- if (o.a_size) {
261
- style += `\n font-size: ${o.a_size};`;
262
- }
263
- if (o.a_color) {
264
- style += `\n color: ${o.a_color};`;
265
- }
266
- style += `\n}`;
267
- }
268
- return style;
269
- };
270
-
271
- /**
272
- * 生成样式
273
- * @param {string} id 元素ID
274
- * @param {object} o 样式对象
275
- * @returns {string} 样式
276
- */
277
- Pendant.prototype._generateStyle = function (id, o) {
278
- var style = `#${id} {`;
279
- style += this._generateFontStyle(o);
280
- style += this._generateBorderStyle(o);
281
- style += this._generateMarginStyle(o);
282
- style += this._generatePaddingStyle(o);
283
- style += this._generateRadiusStyle(o);
284
- style += this._generateBgStyle(o);
285
- style += `\n}`;
286
- style += this._generateLinkStyle(id, o);
287
- return style;
288
- };
289
-
290
- /**
291
- * 生成分类样式
292
- * @param {object} o 布局对象
293
- * @returns {string} 分类样式
294
- */
295
- Pendant.prototype._generateLayout = function (o) {
296
- var layout = '';
297
- if (o.default) {
298
- layout += ' col-' + o.default;
299
- }
300
- if (o.pc) {
301
- layout += ' col-xxl-' + o.pc;
302
- }
303
- if (o.note) {
304
- layout += ' col-xl-' + o.note;
305
- }
306
- if (o.pad) {
307
- layout += ' col-lg-' + o.pad;
308
- }
309
- if (o.pad_mini) {
310
- layout += ' col-sm-' + o.pad_mini;
311
- }
312
- if (o.phone) {
313
- layout += ' col-xs-' + o.phone;
314
- }
315
- return layout.trim();
316
- };
317
-
318
- /**
319
- * 配置模型
320
- * @param {object} block 配置块
321
- * @returns {string} 返回html代码
322
- */
323
- Pendant.prototype.optionModel = function (block) {
324
- var id = block.id || block.name;
325
- var style = '';
326
- if (block.style) {
327
- var o = block.style.toJson();
328
- style = this._generateStyle(id, o);
329
- }
330
-
331
- var layout = '';
332
- if (block.layout) {
333
- var o = block.layout.toJson();
334
- layout = this._generateLayout(o);
335
- }
336
-
337
- var data = (block.data || '[]').toJson();
338
- var attr = (block.attr || '{}').toJson();
339
- var options = (block.options || '{}').toJson();
340
-
341
- return {
342
- id,
343
- class: block.class,
344
- style,
345
- data,
346
- attr,
347
- layout,
348
- options
349
- };
350
- };
351
-
352
- /**
353
- * 渲染模块
354
- * @param {object} ctx 请求上下文
355
- * @param {object} db 数据管理器,如: { next: async function{}, ret: {} }
356
- * @param {Array} blocks 模块数组
357
- * @returns {string} 返回html代码
358
- */
359
- Pendant.prototype.renderBlock = async function (ctx, db, blocks) {
360
- var html = '';
361
- for (var i = 0; i < blocks.length; i++) {
362
- var block = blocks[i];
363
- var o = this.getMod(block.pendant);
364
- if (o) {
365
- var option = this.optionModel(block);
366
- var ret = await o.call('main', ctx, db, option);
367
- if (ret) {
368
- html += '\n' + ret;
369
- }
370
- }
371
- }
372
- return html;
373
- };
374
-
375
- /**
376
- * 执行渲染
377
- * @param {object} ctx 请求上下文
378
- * @param {object} db 数据管理器,如: { next: async function{}, ret: {} }
379
- * @param {string} path 页面
380
- * @param {string} position 位置
381
- * @returns {string} 返回html代码
382
- */
383
- Pendant.prototype.render = async function (ctx, db, path, position) {
384
- var html = '';
385
- var infos = this.getInfos();
386
- for (var k in infos) {
387
- var info = infos[k];
388
- if (info.state) {
389
- let mod = this.getMod(info.name);
390
- if (mod) {
391
- var obj = mod.config.location.getObj({
392
- path,
393
- position
394
- });
395
- if (obj && obj.available) {
396
- var ret = await mod.call('main', ctx, db);
397
- if (ret) {
398
- html += '\n' + ret;
399
- }
400
- }
401
- }
402
- }
403
- }
404
- return html;
405
- };
406
-
407
- /* 导出指令 */
408
- exports.Pendant = Pendant;
409
-
410
- /**
411
- * Pendant组件池
412
- */
413
- if (!$.pool.pendant) {
414
- $.pool.pendant = {};
415
- }
416
-
417
- /**
418
- * Pendant管理器,用于创建缓存
419
- * @param {string} scope 作用域
420
- * @param {string} title 标题
421
- * @returns {object} 返回一个缓存类
422
- */
423
- function pendantAdmin(scope, title) {
424
- var sc = scope || $.val.scope + '';
425
- var obj = $.pool.pendant[sc];
426
- if (!obj) {
427
- $.pool.pendant[sc] = new Pendant({
428
- name: sc,
429
- title: title
430
- });
431
- obj = $.pool.pendant[sc];
432
- }
433
- return obj;
434
- }
435
-
436
- /**
437
- * @module 导出Pendant管理器
438
- */
439
- if ($.admin) {
440
- $.admin.pendant = pendantAdmin;
441
- }
1
+ const Manager = require("mm_machine").Manager;
2
+ const Drive = require("./drive");
3
+
4
+ /**
5
+ * Pendant 挂件类
6
+ * @augments {Manager}
7
+ * @class
8
+ */
9
+ class Pendant extends Manager {
10
+ /**
11
+ * 配置参数
12
+ * @type {object}
13
+ */
14
+ static config = {
15
+ /**
16
+ * 名称
17
+ * @type {string}
18
+ */
19
+ name: "",
20
+ /**
21
+ * 标题
22
+ * @type {string}
23
+ */
24
+ title: "数据库管理",
25
+ /**
26
+ * 描述
27
+ * @type {string}
28
+ */
29
+ description: "这是数据库管理器",
30
+ /**
31
+ * 检索文件名
32
+ * @type {string}
33
+ */
34
+ filename: "pendant.json",
35
+ /**
36
+ * 模板目录
37
+ * @type {string}
38
+ */
39
+ tpl_dir: __dirname,
40
+ /**
41
+ * 基础目录
42
+ * @type {string}
43
+ */
44
+ base_dir: "../common/pendant".fullname(__dirname),
45
+ /**
46
+ * 自定义目录,加载项目自定义资源
47
+ * @type {string}
48
+ */
49
+ dir: "./app".fullname(),
50
+ /**
51
+ * 搜索模式 dir按目录搜索 | file按文件名搜索
52
+ * @type {string}
53
+ */
54
+ search_way: "file",
55
+ /**
56
+ * 是否懒加载
57
+ * @type {boolean}
58
+ */
59
+ lazy_load: true,
60
+ /**
61
+ * 模式
62
+ * 1.生产模式,改变文件不会重新加载
63
+ * 2.热更新模式,改变配置文件会重新加载配置,不重新加载脚本
64
+ * 3.热重载模式,改变配置文件都会加载配置和脚本
65
+ * 4.重载模式,执行完后重新加载脚本,避免变量污染
66
+ * 5.热更新+重载模式,改变配置文件重新加载配置和脚本,执行完后重新加载脚本
67
+ * @type {number}
68
+ */
69
+ mode: process.env.NODE_ENV === "production" ? 1 : 3,
70
+ };
71
+
72
+ /**
73
+ * 构造函数
74
+ * @param {object} config 配置参数
75
+ * @param {object} parent 父级模块
76
+ */
77
+ constructor(config, parent) {
78
+ super({ ...Pendant.config, ...config }, parent);
79
+ }
80
+ }
81
+
82
+ /**
83
+ * 挂件驱动类
84
+ * @type {Drive}
85
+ */
86
+ Pendant.prototype.Drive = Drive;
87
+
88
+ /**
89
+ * 加载列表
90
+ * @param {Array} list 文件列表
91
+ */
92
+ Pendant.prototype.loadList = async function (list) {
93
+ var _this = this;
94
+
95
+ // 遍历文件路径
96
+ for (var i = 0; i < list.length; i++) {
97
+ var file = list[i];
98
+ var dir = file.dirname();
99
+ // 载入文件
100
+ var obj = file.loadJson(dir);
101
+ if (obj) {
102
+ if (obj.constructor == Array) {
103
+ for (var n = 0; n < obj.length; n++) {
104
+ var o = obj[n];
105
+ // 实例化一个驱动
106
+ await _this.load(dir, o, file);
107
+ }
108
+ } else {
109
+ await _this.load(dir, obj, file);
110
+ }
111
+ } else {
112
+ var fl = _this.dir_base + "/config.tpl.json";
113
+ if (fl.hasFile()) {
114
+ var text = fl.loadText();
115
+ if (text) {
116
+ var l = $.slash;
117
+ var name = file.between("Pendant" + l, l);
118
+ text = text.replaceAll("{0}", name);
119
+ var app = file.between("app" + l, l);
120
+ text = text.replaceAll("{1}", app);
121
+ file.saveText(text);
122
+ }
123
+ }
124
+ }
125
+ }
126
+ };
127
+
128
+ /**
129
+ * 生成字体样式
130
+ * @param {object} o 样式对象
131
+ * @returns {string} 字体样式
132
+ */
133
+ Pendant.prototype._generateFontStyle = function (o) {
134
+ var style = "";
135
+ if (o.font_size) {
136
+ style += `\n font-size: ${o.font_size};`;
137
+ }
138
+ if (o.font_color) {
139
+ style += `\n color: ${o.font_color};`;
140
+ }
141
+ return style;
142
+ };
143
+
144
+ /**
145
+ * 生成边框样式
146
+ * @param {object} o 样式对象
147
+ * @returns {string} 边框样式
148
+ */
149
+ Pendant.prototype._generateBorderStyle = function (o) {
150
+ var borders = [
151
+ {
152
+ pos: "top",
153
+ style: o.border_top_style,
154
+ size: o.border_top_size,
155
+ color: o.border_top_color,
156
+ },
157
+ {
158
+ pos: "right",
159
+ style: o.border_right_style,
160
+ size: o.border_right_size,
161
+ color: o.border_right_color,
162
+ },
163
+ {
164
+ pos: "bottom",
165
+ style: o.border_bottom_style,
166
+ size: o.border_bottom_size,
167
+ color: o.border_bottom_color,
168
+ },
169
+ {
170
+ pos: "left",
171
+ style: o.border_left_style,
172
+ size: o.border_left_size,
173
+ color: o.border_left_color,
174
+ },
175
+ ];
176
+ var style = "";
177
+ for (var i = 0; i < borders.length; i++) {
178
+ var b = borders[i];
179
+ if (b.style !== "none") {
180
+ style += `\n border-${b.pos}: ${b.size || 0} ${b.style || "solid"} ${b.color || "#dbdbdb"};`;
181
+ }
182
+ }
183
+ return style;
184
+ };
185
+
186
+ /**
187
+ * 生成边距样式
188
+ * @param {object} o 样式对象
189
+ * @returns {string} 边距样式
190
+ */
191
+ Pendant.prototype._generateMarginStyle = function (o) {
192
+ var style = "";
193
+ if (o.margin_top) {
194
+ style += `\n margin-top: ${o.margin_top};`;
195
+ }
196
+ if (o.margin_right) {
197
+ style += `\n margin-right: ${o.margin_right};`;
198
+ }
199
+ if (o.margin_bottom) {
200
+ style += `\n margin-bottom: ${o.margin_bottom};`;
201
+ }
202
+ if (o.margin_left) {
203
+ style += `\n margin-left: ${o.margin_left};`;
204
+ }
205
+ return style;
206
+ };
207
+
208
+ /**
209
+ * 生成填充样式
210
+ * @param {object} o 样式对象
211
+ * @returns {string} 填充样式
212
+ */
213
+ Pendant.prototype._generatePaddingStyle = function (o) {
214
+ var style = "";
215
+ if (o.padding_top) {
216
+ style += `\n padding-top: ${o.padding_top};`;
217
+ }
218
+ if (o.padding_right) {
219
+ style += `\n padding-right: ${o.padding_right};`;
220
+ }
221
+ if (o.padding_bottom) {
222
+ style += `\n padding-bottom: ${o.padding_bottom};`;
223
+ }
224
+ if (o.padding_left) {
225
+ style += `\n padding-left: ${o.padding_left};`;
226
+ }
227
+ return style;
228
+ };
229
+
230
+ /**
231
+ * 生成圆角样式
232
+ * @param {object} o 样式对象
233
+ * @returns {string} 圆角样式
234
+ */
235
+ Pendant.prototype._generateRadiusStyle = function (o) {
236
+ var style = "";
237
+ if (o.radius_top_right) {
238
+ style += `\n border-top-right-radius: ${o.radius_top_right};`;
239
+ }
240
+ if (o.radius_top_left) {
241
+ style += `\n border-top-left-radius: ${o.radius_top_left};`;
242
+ }
243
+ if (o.radius_bottom_right) {
244
+ style += `\n border-bottom-right-radius: ${o.radius_bottom_right};`;
245
+ }
246
+ if (o.radius_bottom_left) {
247
+ style += `\n border-bottom-left-radius: ${o.radius_bottom_left};`;
248
+ }
249
+ return style;
250
+ };
251
+
252
+ /**
253
+ * 生成背景样式
254
+ * @param {object} o 样式对象
255
+ * @returns {string} 背景样式
256
+ */
257
+ Pendant.prototype._generateBgStyle = function (o) {
258
+ var style = "";
259
+ if (o.bg_url) {
260
+ style += `\n background-image: url(${o.bg_url});`;
261
+ style += `\n background-size: 100% auto;`;
262
+ style += `\n background-repeat: ${o.bg_repeat || "no-repeat"};`;
263
+ }
264
+ if (o.bg_color) {
265
+ style += `\n background-color: ${o.bg_color || ""};`;
266
+ }
267
+ return style;
268
+ };
269
+
270
+ /**
271
+ * 生成链接样式
272
+ * @param {string} id 元素ID
273
+ * @param {object} o 样式对象
274
+ * @returns {string} 链接样式
275
+ */
276
+ Pendant.prototype._generateLinkStyle = function (id, o) {
277
+ var style = "";
278
+ if (o.a_size || o.a_color) {
279
+ style += `\n#${id} a {`;
280
+ if (o.a_size) {
281
+ style += `\n font-size: ${o.a_size};`;
282
+ }
283
+ if (o.a_color) {
284
+ style += `\n color: ${o.a_color};`;
285
+ }
286
+ style += `\n}`;
287
+ }
288
+ return style;
289
+ };
290
+
291
+ /**
292
+ * 生成样式
293
+ * @param {string} id 元素ID
294
+ * @param {object} o 样式对象
295
+ * @returns {string} 样式
296
+ */
297
+ Pendant.prototype._generateStyle = function (id, o) {
298
+ var style = `#${id} {`;
299
+ style += this._generateFontStyle(o);
300
+ style += this._generateBorderStyle(o);
301
+ style += this._generateMarginStyle(o);
302
+ style += this._generatePaddingStyle(o);
303
+ style += this._generateRadiusStyle(o);
304
+ style += this._generateBgStyle(o);
305
+ style += `\n}`;
306
+ style += this._generateLinkStyle(id, o);
307
+ return style;
308
+ };
309
+
310
+ /**
311
+ * 生成分类样式
312
+ * @param {object} o 布局对象
313
+ * @returns {string} 分类样式
314
+ */
315
+ Pendant.prototype._generateLayout = function (o) {
316
+ var layout = "";
317
+ if (o.default) {
318
+ layout += " col-" + o.default;
319
+ }
320
+ if (o.pc) {
321
+ layout += " col-xxl-" + o.pc;
322
+ }
323
+ if (o.note) {
324
+ layout += " col-xl-" + o.note;
325
+ }
326
+ if (o.pad) {
327
+ layout += " col-lg-" + o.pad;
328
+ }
329
+ if (o.pad_mini) {
330
+ layout += " col-sm-" + o.pad_mini;
331
+ }
332
+ if (o.phone) {
333
+ layout += " col-xs-" + o.phone;
334
+ }
335
+ return layout.trim();
336
+ };
337
+
338
+ /**
339
+ * 配置模型
340
+ * @param {object} block 配置块
341
+ * @returns {string} 返回html代码
342
+ */
343
+ Pendant.prototype.optionModel = function (block) {
344
+ var id = block.id || block.name;
345
+ var style = "";
346
+ if (block.style) {
347
+ var o = block.style.toJson();
348
+ style = this._generateStyle(id, o);
349
+ }
350
+
351
+ var layout = "";
352
+ if (block.layout) {
353
+ var o = block.layout.toJson();
354
+ layout = this._generateLayout(o);
355
+ }
356
+
357
+ var data = (block.data || "[]").toJson();
358
+ var attr = (block.attr || "{}").toJson();
359
+ var options = (block.options || "{}").toJson();
360
+
361
+ return {
362
+ id,
363
+ class: block.class,
364
+ style,
365
+ data,
366
+ attr,
367
+ layout,
368
+ options,
369
+ };
370
+ };
371
+
372
+ /**
373
+ * 渲染模块
374
+ * @param {object} ctx 请求上下文
375
+ * @param {object} db 数据管理器,如: { next: async function{}, ret: {} }
376
+ * @param {Array} blocks 模块数组
377
+ * @returns {string} 返回html代码
378
+ */
379
+ Pendant.prototype.renderBlock = async function (ctx, db, blocks) {
380
+ var html = "";
381
+ for (var i = 0; i < blocks.length; i++) {
382
+ var block = blocks[i];
383
+ var o = this.getMod(block.pendant);
384
+ if (o) {
385
+ if (!o.isLoaded()) {
386
+ await o.do("load");
387
+ }
388
+ var option = this.optionModel(block);
389
+ var ret = await o.call("main", ctx, db, option);
390
+ if (ret) {
391
+ html += "\n" + ret;
392
+ }
393
+ }
394
+ }
395
+ return html;
396
+ };
397
+
398
+ /**
399
+ * 执行渲染
400
+ * @param {object} ctx 请求上下文
401
+ * @param {object} db 数据管理器,如: { next: async function{}, ret: {} }
402
+ * @param {string} path 页面
403
+ * @param {string} position 位置
404
+ * @returns {string} 返回html代码
405
+ */
406
+ Pendant.prototype.render = async function (ctx, db, path, position) {
407
+ var html = "";
408
+ var infos = this.getInfos();
409
+ for (var k in infos) {
410
+ var info = infos[k];
411
+ if (info.state) {
412
+ let mod = this.getMod(info.name);
413
+ if (mod) {
414
+ var obj = mod.config.location.getObj({
415
+ path,
416
+ position,
417
+ });
418
+ if (obj && obj.available) {
419
+ if (!mod.isLoaded()) {
420
+ await mod.do("load");
421
+ }
422
+ var ret = await mod.call("main", ctx, db);
423
+ if (ret) {
424
+ html += "\n" + ret;
425
+ }
426
+ }
427
+ }
428
+ }
429
+ }
430
+ return html;
431
+ };
432
+
433
+ /* 导出指令 */
434
+ exports.Pendant = Pendant;
435
+
436
+ /**
437
+ * Pendant组件池
438
+ */
439
+ if (!$.pool.pendant) {
440
+ $.pool.pendant = {};
441
+ }
442
+
443
+ /**
444
+ * Pendant管理器,用于创建缓存
445
+ * @param {string} scope 作用域
446
+ * @param {string} title 标题
447
+ * @returns {object} 返回一个缓存类
448
+ */
449
+ function pendantAdmin(scope, title) {
450
+ var sc = scope || $.val.scope + "";
451
+ var obj = $.pool.pendant[sc];
452
+ if (!obj) {
453
+ $.pool.pendant[sc] = new Pendant({
454
+ name: sc,
455
+ title: title,
456
+ });
457
+ obj = $.pool.pendant[sc];
458
+ }
459
+ return obj;
460
+ }
461
+
462
+ /**
463
+ * @module 导出Pendant管理器
464
+ */
465
+ if ($.admin) {
466
+ $.admin.pendant = pendantAdmin;
467
+ }