mm_os 2.9.8 → 3.0.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.
@@ -1,543 +1,543 @@
1
- const compressing = require('compressing');
2
- const Item = require('mm_machine').Item;
3
- const Log = require('./log.js');
4
- const conf = require('mm_config');
5
-
6
- /**
7
- * Plugin插件驱动类
8
- * @extends {Item}
9
- * @class
10
- */
11
- class Drive extends Item {
12
- /**
13
- * 构造函数
14
- * @param {String} dir 当前目录
15
- * @constructor
16
- */
17
- constructor(dir) {
18
- super(dir, __dirname);
19
- this.default_file = "./plugin.json";
20
- // 默认启用热更新
21
- this.mode = 3;
22
- /**
23
- * 当前语言
24
- */
25
- this.lang_now = "zh_CN";
26
- /**
27
- * 语言包
28
- */
29
- this.lang = {};
30
-
31
- /* 通用项 */
32
- // 配置参数
33
- this.config = {
34
- // 应用域
35
- "app": "",
36
- // 名称, 由中英文和下“_”组成, 用于修改或卸载 例如: demo
37
- // 同时也是版本唯一标识,当商城有两个插件名称相同时,可以通过版本号查询和下载
38
- "name": "",
39
- // 标题, 介绍事件作用
40
- "title": "",
41
- // 描述, 用于描述该事件有什么用的
42
- "description": "",
43
- // 版本号
44
- "version": "1.0",
45
- // 作者
46
- "author": "",
47
- // 官网
48
- "web": "",
49
- // 状态 0未启用,1启用
50
- "state": 1,
51
- // 显示 0不显示,1显示
52
- "show": 0,
53
- // 文件路径, 当调用函数不存在时,会先从文件中加载
54
- "func_file": "./index.js",
55
- // 回调函数名 用于决定调用脚本的哪个函数
56
- "func_name": "",
57
- // 语言包路径
58
- "lang_path": "./lang/",
59
- // 插件图标
60
- "icon": "/img/logo.png",
61
- // 指令前缀
62
- "cmd": "",
63
- // 是否中断执行
64
- "end": true,
65
- // 排序
66
- "sort": 10,
67
- // 配置
68
- "options": []
69
- };
70
- // 配置
71
- this.options = {};
72
- }
73
- }
74
-
75
- /**
76
- * 设置配置
77
- * @param {Object} config 配置
78
- */
79
- Drive.prototype.set_config = function(config) {
80
- var file = this.filename;
81
- var cg = Object.assign({}, this.config, config || {});
82
- this.config = conf(cg, file);
83
- }
84
-
85
- /**
86
- * 插件初始化之后
87
- */
88
- Drive.prototype.set_config_after = function() {
89
- var file = this.filename;
90
- var filename = file.replace('app', 'log').replace($.slash + 'plugin', '').replace('json', 'log').replace($
91
- .slash + 'plugin.log', '.log');
92
- this.log = new Log({
93
- filename
94
- });
95
- var options = this.get_config_options();
96
- this.options = this.merge_options(options);
97
- this.backup_options();
98
- }
99
-
100
- /**
101
- * 更新配置
102
- */
103
- Drive.prototype.update_options = function(options = []) {
104
- var op = this.config.options;
105
- var list = [];
106
- for (var i = options.length - 1; i >= 0; i--) {
107
- var o = options[i];
108
- var obj = op.get({
109
- name: o.name
110
- });
111
- if (obj) {
112
- Object.assign(obj, o);
113
- } else {
114
- list.push(o);
115
- }
116
- }
117
- for (var i = 0; i < options.length; i++) {
118
- var o = options[i];
119
- op.push(o);
120
- }
121
- }
122
-
123
- /**
124
- * 更新配置后
125
- * @param {Object} options
126
- */
127
- Drive.prototype.update_options_after = function(options) {
128
- // if (this.config.state) {
129
- // this.restart();
130
- // }
131
- }
132
-
133
- /**
134
- * 获取配置参数
135
- * @return {Object} 返回配置参数
136
- */
137
- Drive.prototype.get_options = function() {
138
- return this.options;
139
- }
140
-
141
- /**
142
- * 获取配置参数
143
- * @return {Object} 返回配置参数
144
- */
145
- Drive.prototype.get_config_options = function() {
146
- var op = this.config.options || [];
147
- var dict = {};
148
- for (var i = 0; i < op.length; i++) {
149
- var o = op[i];
150
- if (o.type === "number") {
151
- var val = o.value === 'null' ? '0' : o.value;
152
- dict[o.name] = Number(val || '0');
153
- } else if (o.type === "boolean") {
154
- var val = false;
155
- if (o.value) {
156
- if (typeof(o.value) == "string") {
157
- if (o.value == '1' || o.value == 'true') {
158
- val = true;
159
- }
160
- } else if (typeof(o.value) == "boolean") {
161
- val = o.value;
162
- }
163
- }
164
- dict[o.name] = val;
165
- } else if (o.type === "object") {
166
- var val = o.value === 'null' ? '{}' : o.value;
167
- dict[o.name] = o.value.toJSON();
168
- } else if (o.type === "array") {
169
- var val = o.value === 'null' ? '[]' : o.value;
170
- dict[o.name] = o.value.toJSON();
171
- } else if (o.value === "null") {
172
- dict[o.name] = null;
173
- } else if (o.type === "string") {
174
- dict[o.name] = o.value || "";
175
- } else {
176
- dict[o.name] = o.value;
177
- }
178
- }
179
- return dict;
180
- }
181
-
182
- /**
183
- * 获取配置参数
184
- * @param {Object} 设置配置
185
- * @return {Object} 返回配置参数
186
- */
187
- Drive.prototype.design_option = function(body) {
188
- var cg = this.config;
189
- if (Array.isArray(body)) {
190
- cg.options = body;
191
- } else {
192
- var options = cg.options || [];
193
- var option = options.getObj({
194
- name: body.name
195
- });
196
- if (option) {
197
- Object.assign(option, body);
198
- } else {
199
- options.push(body);
200
- }
201
- cg.options = options;
202
- }
203
- }
204
-
205
- /**
206
- * 保存配置
207
- */
208
- Drive.prototype.save_option = function(options) {
209
- this.options = options;
210
- this.backup_options();
211
- }
212
-
213
- /**
214
- * 保存配置
215
- */
216
- Drive.prototype.backup_options = function() {
217
- var l = $.slash;
218
- var file = "/cache/" + l + l + this.filename.right("app" + l).replaceAll(l + "plugin", "").replace(".json",
219
- "/config.json");
220
- file = file.fullname();
221
- file.addDir();
222
- file.saveJson(this.options);
223
- }
224
-
225
- /**
226
- * 合并配置
227
- * @param {Object} cg 配置
228
- */
229
- Drive.prototype.merge_options = function(options) {
230
- var l = $.slash;
231
- var file = "/cache/" + l + l + this.filename.right("app" + l).replaceAll(l + "plugin", "").replace(".json",
232
- "/config.json");
233
- var options_cache = file.loadJson();
234
- if (options_cache) {
235
- $.push(options, options_cache);
236
- }
237
- return options;
238
- }
239
-
240
- /**
241
- * 新建脚本
242
- * @param {String} 文件
243
- */
244
- Drive.prototype.new_script = function(file) {
245
- var fl = __dirname + "/script.js";
246
- if (fl.hasFile()) {
247
- var text = fl.loadText();
248
- if (text) {
249
- var name = 'sys';
250
- var l = $.slash;
251
- if (file.indexOf('plugin' + l) !== -1) {
252
- name = file.between('plugin' + l, l);
253
- if (file.indexOf('app' + l) !== -1) {
254
- var app = file.between('app' + l, l);
255
- text = text.replaceAll('{1}', app);
256
- }
257
- text = text.replaceAll('{0}', name);
258
- } else if (file.indexOf('app' + l) !== -1) {
259
- name = file.between('app' + l, l);
260
- text = text.replaceAll('{0}', name)
261
- }
262
- file.saveText(text);
263
- }
264
- }
265
- };
266
-
267
- /**
268
- * 新建配置
269
- * @param {String} 文件
270
- */
271
- Drive.prototype.new_config = function(file) {
272
- var fl = __dirname + "/config.tpl.json";
273
- if (fl.hasFile()) {
274
- var text = fl.loadText();
275
- if (text) {
276
- var name = 'sys';
277
- var l = $.slash;
278
- if (file.indexOf('plugin' + l) !== -1) {
279
- name = file.between('plugin' + l, l);
280
- if (file.indexOf('app' + l) !== -1) {
281
- var app = file.between('app' + l, l);
282
- text = text.replaceAll('{1}', app).replaceAll('{2}', app + '.' + name);
283
- }
284
- text = text.replaceAll('{0}', name);
285
- } else if (file.indexOf('app' + l) !== -1) {
286
- name = file.between('app' + l, l);
287
- text = text.replaceAll('{0}', name).replaceAll('\r\n "app": "{1}",', '').replaceAll('插件', '应用')
288
- .replaceAll('{2}', name);
289
- }
290
- file.saveText(text);
291
- }
292
- }
293
- };
294
-
295
- /**
296
- * 设置语言
297
- * @param {Sting} lang
298
- * @return {String} 成功返回null,否则返回错误提示
299
- */
300
- Drive.prototype.set_lang = function(lang) {
301
- var msg = null;
302
- if (!lang) {
303
- lang = "zh_CN"
304
- }
305
- var file = this.config.lang_path + ".json";
306
- if (file.hasFile()) {
307
- var obj = file.loadJson();
308
- if (obj) {
309
- this.lang = obj;
310
- } else {
311
- msg = "语言包不是正确的json格式";
312
- }
313
- } else {
314
- msg = "语言包文件不存在";
315
- }
316
- return msg;
317
- };
318
-
319
- /**
320
- * 安装
321
- * @param {Object} option 配置参数
322
- * @return {String} 成功返回null,否则返回错误提示
323
- */
324
- Drive.prototype.install = function(option) {
325
- var msg = null;
326
- return msg;
327
- };
328
-
329
- /**
330
- * 重启设备
331
- * @param {Object} option 配置
332
- */
333
- Drive.prototype.restart = async function(option) {
334
- await this.run('stop');
335
- await this.run('start');
336
- }
337
-
338
- /**
339
- * 初始化
340
- * @param {Object} option 配置参数
341
- * @return {String} 成功返回null, 否则返回错误提示
342
- */
343
- Drive.prototype.init = function(option) {
344
- var msg = null;
345
- return msg;
346
- };
347
-
348
- /**
349
- * 更新
350
- * @param {Object} option 配置参数
351
- * @return {String} 成功返回null, 否则返回错误提示
352
- */
353
- Drive.prototype.update = function(option) {
354
- var msg = null;
355
- return msg;
356
- };
357
-
358
- /**
359
- * 卸载
360
- * @param {Object} option 配置参数
361
- * @return {String} 成功返回null,否则返回错误提示
362
- */
363
- Drive.prototype.uninstall = function(option) {
364
- var msg = null;
365
- return msg;
366
- };
367
-
368
- /**
369
- * 启动
370
- * @param {Object} opiton 配置参数
371
- * @return {String} 成功返回null,否则返回错误提示
372
- */
373
- Drive.prototype.start = function(opiton) {
374
- var msg = null;
375
- return msg;
376
- };
377
-
378
- /**
379
- * 暂停
380
- * @param {Object} opiton 配置参数
381
- * @return {String} 成功返回null,否则返回错误提示
382
- */
383
- Drive.prototype.stop = function(opiton) {
384
- var msg = null;
385
- return msg;
386
- };
387
-
388
- /**
389
- * 结束
390
- * @param {Object} opiton 配置参数
391
- * @return {String} 成功返回null,否则返回错误提示
392
- */
393
- Drive.prototype.end = function(opiton) {
394
- var msg = null;
395
- return msg;
396
- };
397
-
398
- /**
399
- * API接口(用于其他插件调用该插件时)
400
- * @param {Object} ctx HTTP上下文
401
- * @param {Object} db 数据库管理器
402
- * @return {Object} 执行结果
403
- */
404
- Drive.prototype.api = function(ctx, db) {
405
- var ret = {};
406
- return ret;
407
- };
408
-
409
- /**
410
- * 商店(用于下载插件相关模块)
411
- * @param {String} item 插件项
412
- * @return {String} 成功返回null,否则返回错误提示
413
- */
414
- Drive.prototype.store = function(item) {
415
- var msg = null;
416
- return msg;
417
- };
418
-
419
- /**
420
- * 插件
421
- * @param {String} item 插件项
422
- * @return {Object} 旗下插件和信息
423
- */
424
- Drive.prototype.plugin = function(item) {
425
-
426
- };
427
-
428
- /**
429
- * 帮助(讲解插件使用方法)
430
- * @param {String} item 帮助项
431
- * @return {String} 返回使用方法明细
432
- */
433
- Drive.prototype.help = function(item) {
434
- var body = "";
435
- return body;
436
- };
437
-
438
- /**
439
- * 主程序
440
- * @param {Object} param1 参数1
441
- * @param {Object} param2 参数2
442
- * @return {Object} 返回执行结果
443
- */
444
- Drive.prototype.main = function(param1, param2) {
445
- return null;
446
- };
447
-
448
- /**
449
- * 指令(类似命令提示符)
450
- * @param {String} content 指令内容
451
- * @return {String} 执行结果
452
- */
453
- Drive.prototype.cmd = function(content) {
454
- var body = "";
455
- return body;
456
- };
457
-
458
- /**
459
- * 聊天(通过聊天的方式驱动插件, 用于机器人开发)
460
- * @param {String} from_user 发送消息人
461
- * @param {String} to_user 接收消息人
462
- * @param {String} content 内容
463
- * @param {String} group 群组 如果是个人,群组为空
464
- * @param {Number} type 群类型, 1永久会话/群、2临时会话/群
465
- * @param {String} msg_type 消息类型, event事件型、message消息型。默认消息型
466
- * @param {Object} 数据管理器
467
- * @return {String} 回复内容
468
- */
469
- Drive.prototype.chat = async function(from_user, to_user, group, content, type, msg_type, db) {
470
- var body = "";
471
- return body;
472
- };
473
-
474
- /**
475
- * 指令(类似命令提示符)
476
- * @param {String} content 指令内容
477
- * @return {String} 执行结果
478
- */
479
- Drive.prototype.run_cmd = async function(content) {
480
- if (!content) {
481
- content = "";
482
- }
483
- var ret;
484
- try {
485
- ret = this.cmd(content);
486
- } catch (err) {
487
- $.log.error("运行插件指令失败!", this.config.name, err);
488
- }
489
- if (!ret) {
490
- ret = "";
491
- }
492
- return ret;
493
- };
494
-
495
- /**
496
- * 聊天(通过聊天的方式驱动插件, 用于机器人开发)
497
- * @param {String} from_user 发送消息人
498
- * @param {String} to_user 接收消息人
499
- * @param {String} content 内容
500
- * @param {String} group 群组 如果是个人,群组为空
501
- * @param {Number} type 群类型, 1永久会话/群、2临时会话/群
502
- * @param {String} msg_type 消息类型, event事件型、message消息型。默认消息型
503
- * @return {String} 回复内容
504
- */
505
- Drive.prototype.run_chat = async function(from_user, to_user, content, group, type, msg_type, db) {
506
- if (!content) {
507
- content = "";
508
- }
509
- if (!msg_type) {
510
- msg_type = "message";
511
- }
512
- if (!group) {
513
- group = "";
514
- }
515
- var ret;
516
- try {
517
- ret = this.chat(from_user, to_user, group, content, type, msg_type, db);
518
- } catch (err) {
519
- $.log.error("运行插件聊天失败!", this.config.name, err);
520
- }
521
- if (!ret) {
522
- ret = "";
523
- }
524
- return ret;
525
- };
526
-
527
- /**
528
- * 压缩主题模板
529
- * @param {String} 要压缩的目录
530
- * @returns {String} 打包成功返回压缩包文件地址
531
- */
532
- Drive.prototype.zip = async function(zip_dir = "/static/file/zip/") {
533
- var dir = this.filename.dirname();
534
- var file = ("./" + this.config.name + '.zip').fullname(zip_dir);
535
- file.addDir();
536
- var done = await compressing.zip.compressDir(dir, file);
537
- if (file.hasFile()) {
538
- return file;
539
- }
540
- return null;
541
- }
542
-
1
+ const compressing = require('compressing');
2
+ const Item = require('mm_machine').Item;
3
+ const Log = require('./log.js');
4
+ const conf = require('mm_config');
5
+
6
+ /**
7
+ * Plugin插件驱动类
8
+ * @extends {Item}
9
+ * @class
10
+ */
11
+ class Drive extends Item {
12
+ /**
13
+ * 构造函数
14
+ * @param {String} dir 当前目录
15
+ * @constructor
16
+ */
17
+ constructor(dir) {
18
+ super(dir, __dirname);
19
+ this.default_file = "./plugin.json";
20
+ // 默认启用热更新
21
+ this.mode = 3;
22
+ /**
23
+ * 当前语言
24
+ */
25
+ this.lang_now = "zh_CN";
26
+ /**
27
+ * 语言包
28
+ */
29
+ this.lang = {};
30
+
31
+ /* 通用项 */
32
+ // 配置参数
33
+ this.config = {
34
+ // 应用域
35
+ "app": "",
36
+ // 名称, 由中英文和下“_”组成, 用于修改或卸载 例如: demo
37
+ // 同时也是版本唯一标识,当商城有两个插件名称相同时,可以通过版本号查询和下载
38
+ "name": "",
39
+ // 标题, 介绍事件作用
40
+ "title": "",
41
+ // 描述, 用于描述该事件有什么用的
42
+ "description": "",
43
+ // 版本号
44
+ "version": "1.0.0",
45
+ // 作者
46
+ "author": "",
47
+ // 官网
48
+ "web": "",
49
+ // 状态 0未启用,1启用
50
+ "state": 1,
51
+ // 显示 0不显示,1显示
52
+ "show": 0,
53
+ // 文件路径, 当调用函数不存在时,会先从文件中加载
54
+ "func_file": "./index.js",
55
+ // 回调函数名 用于决定调用脚本的哪个函数
56
+ "func_name": "",
57
+ // 语言包路径
58
+ "lang_path": "./lang/",
59
+ // 插件图标
60
+ "icon": "/img/logo.png",
61
+ // 指令前缀
62
+ "cmd": "",
63
+ // 是否中断执行
64
+ "end": true,
65
+ // 排序
66
+ "sort": 10,
67
+ // 配置
68
+ "options": []
69
+ };
70
+ // 配置
71
+ this.options = {};
72
+ }
73
+ }
74
+
75
+ /**
76
+ * 设置配置
77
+ * @param {Object} config 配置
78
+ */
79
+ Drive.prototype.set_config = function(config) {
80
+ var file = this.filename;
81
+ var cg = Object.assign({}, this.config, config || {});
82
+ this.config = conf(cg, file);
83
+ }
84
+
85
+ /**
86
+ * 插件初始化之后
87
+ */
88
+ Drive.prototype.set_config_after = function() {
89
+ var file = this.filename;
90
+ var filename = file.replace('app', 'log').replace($.slash + 'plugin', '').replace('json', 'log').replace($
91
+ .slash + 'plugin.log', '.log');
92
+ this.log = new Log({
93
+ filename
94
+ });
95
+ var options = this.get_config_options();
96
+ this.options = this.merge_options(options);
97
+ this.backup_options();
98
+ }
99
+
100
+ /**
101
+ * 更新配置
102
+ */
103
+ Drive.prototype.update_options = function(options = []) {
104
+ var op = this.config.options;
105
+ var list = [];
106
+ for (var i = options.length - 1; i >= 0; i--) {
107
+ var o = options[i];
108
+ var obj = op.get({
109
+ name: o.name
110
+ });
111
+ if (obj) {
112
+ Object.assign(obj, o);
113
+ } else {
114
+ list.push(o);
115
+ }
116
+ }
117
+ for (var i = 0; i < options.length; i++) {
118
+ var o = options[i];
119
+ op.push(o);
120
+ }
121
+ }
122
+
123
+ /**
124
+ * 更新配置后
125
+ * @param {Object} options
126
+ */
127
+ Drive.prototype.update_options_after = function(options) {
128
+ // if (this.config.state) {
129
+ // this.restart();
130
+ // }
131
+ }
132
+
133
+ /**
134
+ * 获取配置参数
135
+ * @return {Object} 返回配置参数
136
+ */
137
+ Drive.prototype.get_options = function() {
138
+ return this.options;
139
+ }
140
+
141
+ /**
142
+ * 获取配置参数
143
+ * @return {Object} 返回配置参数
144
+ */
145
+ Drive.prototype.get_config_options = function() {
146
+ var op = this.config.options || [];
147
+ var dict = {};
148
+ for (var i = 0; i < op.length; i++) {
149
+ var o = op[i];
150
+ if (o.type === "number") {
151
+ var val = o.value === 'null' ? '0' : o.value;
152
+ dict[o.name] = Number(val || '0');
153
+ } else if (o.type === "boolean") {
154
+ var val = false;
155
+ if (o.value) {
156
+ if (typeof(o.value) == "string") {
157
+ if (o.value == '1' || o.value == 'true') {
158
+ val = true;
159
+ }
160
+ } else if (typeof(o.value) == "boolean") {
161
+ val = o.value;
162
+ }
163
+ }
164
+ dict[o.name] = val;
165
+ } else if (o.type === "object") {
166
+ var val = o.value === 'null' ? '{}' : o.value;
167
+ dict[o.name] = o.value.toJSON();
168
+ } else if (o.type === "array") {
169
+ var val = o.value === 'null' ? '[]' : o.value;
170
+ dict[o.name] = o.value.toJSON();
171
+ } else if (o.value === "null") {
172
+ dict[o.name] = null;
173
+ } else if (o.type === "string") {
174
+ dict[o.name] = o.value || "";
175
+ } else {
176
+ dict[o.name] = o.value;
177
+ }
178
+ }
179
+ return dict;
180
+ }
181
+
182
+ /**
183
+ * 获取配置参数
184
+ * @param {Object} 设置配置
185
+ * @return {Object} 返回配置参数
186
+ */
187
+ Drive.prototype.design_option = function(body) {
188
+ var cg = this.config;
189
+ if (Array.isArray(body)) {
190
+ cg.options = body;
191
+ } else {
192
+ var options = cg.options || [];
193
+ var option = options.getObj({
194
+ name: body.name
195
+ });
196
+ if (option) {
197
+ Object.assign(option, body);
198
+ } else {
199
+ options.push(body);
200
+ }
201
+ cg.options = options;
202
+ }
203
+ }
204
+
205
+ /**
206
+ * 保存配置
207
+ */
208
+ Drive.prototype.save_option = function(options) {
209
+ this.options = options;
210
+ this.backup_options();
211
+ }
212
+
213
+ /**
214
+ * 保存配置
215
+ */
216
+ Drive.prototype.backup_options = function() {
217
+ var l = $.slash;
218
+ var file = "/cache/" + l + l + this.filename.right("app" + l).replaceAll(l + "plugin", "").replace(".json",
219
+ "/config.json");
220
+ file = file.fullname();
221
+ file.addDir();
222
+ file.saveJson(this.options);
223
+ }
224
+
225
+ /**
226
+ * 合并配置
227
+ * @param {Object} cg 配置
228
+ */
229
+ Drive.prototype.merge_options = function(options) {
230
+ var l = $.slash;
231
+ var file = "/cache/" + l + l + this.filename.right("app" + l).replaceAll(l + "plugin", "").replace(".json",
232
+ "/config.json");
233
+ var options_cache = file.loadJson();
234
+ if (options_cache) {
235
+ $.push(options, options_cache);
236
+ }
237
+ return options;
238
+ }
239
+
240
+ /**
241
+ * 新建脚本
242
+ * @param {String} 文件
243
+ */
244
+ Drive.prototype.new_script = function(file) {
245
+ var fl = __dirname + "/script.js";
246
+ if (fl.hasFile()) {
247
+ var text = fl.loadText();
248
+ if (text) {
249
+ var name = 'sys';
250
+ var l = $.slash;
251
+ if (file.indexOf('plugin' + l) !== -1) {
252
+ name = file.between('plugin' + l, l);
253
+ if (file.indexOf('app' + l) !== -1) {
254
+ var app = file.between('app' + l, l);
255
+ text = text.replaceAll('{1}', app);
256
+ }
257
+ text = text.replaceAll('{0}', name);
258
+ } else if (file.indexOf('app' + l) !== -1) {
259
+ name = file.between('app' + l, l);
260
+ text = text.replaceAll('{0}', name)
261
+ }
262
+ file.saveText(text);
263
+ }
264
+ }
265
+ };
266
+
267
+ /**
268
+ * 新建配置
269
+ * @param {String} 文件
270
+ */
271
+ Drive.prototype.new_config = function(file) {
272
+ var fl = __dirname + "/config.tpl.json";
273
+ if (fl.hasFile()) {
274
+ var text = fl.loadText();
275
+ if (text) {
276
+ var name = 'sys';
277
+ var l = $.slash;
278
+ if (file.indexOf('plugin' + l) !== -1) {
279
+ name = file.between('plugin' + l, l);
280
+ if (file.indexOf('app' + l) !== -1) {
281
+ var app = file.between('app' + l, l);
282
+ text = text.replaceAll('{1}', app).replaceAll('{2}', app + '.' + name);
283
+ }
284
+ text = text.replaceAll('{0}', name);
285
+ } else if (file.indexOf('app' + l) !== -1) {
286
+ name = file.between('app' + l, l);
287
+ text = text.replaceAll('{0}', name).replaceAll('\r\n "app": "{1}",', '').replaceAll('插件', '应用')
288
+ .replaceAll('{2}', name);
289
+ }
290
+ file.saveText(text);
291
+ }
292
+ }
293
+ };
294
+
295
+ /**
296
+ * 设置语言
297
+ * @param {Sting} lang
298
+ * @return {String} 成功返回null,否则返回错误提示
299
+ */
300
+ Drive.prototype.set_lang = function(lang) {
301
+ var msg = null;
302
+ if (!lang) {
303
+ lang = "zh_CN"
304
+ }
305
+ var file = this.config.lang_path + ".json";
306
+ if (file.hasFile()) {
307
+ var obj = file.loadJson();
308
+ if (obj) {
309
+ this.lang = obj;
310
+ } else {
311
+ msg = "语言包不是正确的json格式";
312
+ }
313
+ } else {
314
+ msg = "语言包文件不存在";
315
+ }
316
+ return msg;
317
+ };
318
+
319
+ /**
320
+ * 安装
321
+ * @param {Object} option 配置参数
322
+ * @return {String} 成功返回null,否则返回错误提示
323
+ */
324
+ Drive.prototype.install = function(option) {
325
+ var msg = null;
326
+ return msg;
327
+ };
328
+
329
+ /**
330
+ * 重启设备
331
+ * @param {Object} option 配置
332
+ */
333
+ Drive.prototype.restart = async function(option) {
334
+ await this.exec('stop');
335
+ await this.exec('start');
336
+ }
337
+
338
+ /**
339
+ * 初始化
340
+ * @param {Object} option 配置参数
341
+ * @return {String} 成功返回null, 否则返回错误提示
342
+ */
343
+ Drive.prototype.init = function(option) {
344
+ var msg = null;
345
+ return msg;
346
+ };
347
+
348
+ /**
349
+ * 更新
350
+ * @param {Object} option 配置参数
351
+ * @return {String} 成功返回null, 否则返回错误提示
352
+ */
353
+ Drive.prototype.update = function(option) {
354
+ var msg = null;
355
+ return msg;
356
+ };
357
+
358
+ /**
359
+ * 卸载
360
+ * @param {Object} option 配置参数
361
+ * @return {String} 成功返回null,否则返回错误提示
362
+ */
363
+ Drive.prototype.uninstall = function(option) {
364
+ var msg = null;
365
+ return msg;
366
+ };
367
+
368
+ /**
369
+ * 启动
370
+ * @param {Object} opiton 配置参数
371
+ * @return {String} 成功返回null,否则返回错误提示
372
+ */
373
+ Drive.prototype.start = function(opiton) {
374
+ var msg = null;
375
+ return msg;
376
+ };
377
+
378
+ /**
379
+ * 暂停
380
+ * @param {Object} opiton 配置参数
381
+ * @return {String} 成功返回null,否则返回错误提示
382
+ */
383
+ Drive.prototype.stop = function(opiton) {
384
+ var msg = null;
385
+ return msg;
386
+ };
387
+
388
+ /**
389
+ * 结束
390
+ * @param {Object} opiton 配置参数
391
+ * @return {String} 成功返回null,否则返回错误提示
392
+ */
393
+ Drive.prototype.end = function(opiton) {
394
+ var msg = null;
395
+ return msg;
396
+ };
397
+
398
+ /**
399
+ * API接口(用于其他插件调用该插件时)
400
+ * @param {Object} ctx HTTP上下文
401
+ * @param {Object} db 数据库管理器
402
+ * @return {Object} 执行结果
403
+ */
404
+ Drive.prototype.api = function(ctx, db) {
405
+ var ret = {};
406
+ return ret;
407
+ };
408
+
409
+ /**
410
+ * 商店(用于下载插件相关模块)
411
+ * @param {String} item 插件项
412
+ * @return {String} 成功返回null,否则返回错误提示
413
+ */
414
+ Drive.prototype.store = function(item) {
415
+ var msg = null;
416
+ return msg;
417
+ };
418
+
419
+ /**
420
+ * 插件
421
+ * @param {String} item 插件项
422
+ * @return {Object} 旗下插件和信息
423
+ */
424
+ Drive.prototype.plugin = function(item) {
425
+
426
+ };
427
+
428
+ /**
429
+ * 帮助(讲解插件使用方法)
430
+ * @param {String} item 帮助项
431
+ * @return {String} 返回使用方法明细
432
+ */
433
+ Drive.prototype.help = function(item) {
434
+ var body = "";
435
+ return body;
436
+ };
437
+
438
+ /**
439
+ * 主程序
440
+ * @param {Object} param1 参数1
441
+ * @param {Object} param2 参数2
442
+ * @return {Object} 返回执行结果
443
+ */
444
+ Drive.prototype.main = function(param1, param2) {
445
+ return null;
446
+ };
447
+
448
+ /**
449
+ * 指令(类似命令提示符)
450
+ * @param {String} content 指令内容
451
+ * @return {String} 执行结果
452
+ */
453
+ Drive.prototype.cmd = function(content) {
454
+ var body = "";
455
+ return body;
456
+ };
457
+
458
+ /**
459
+ * 聊天(通过聊天的方式驱动插件, 用于机器人开发)
460
+ * @param {String} from_user 发送消息人
461
+ * @param {String} to_user 接收消息人
462
+ * @param {String} content 内容
463
+ * @param {String} group 群组 如果是个人,群组为空
464
+ * @param {Number} type 群类型, 1永久会话/群、2临时会话/群
465
+ * @param {String} msg_type 消息类型, event事件型、message消息型。默认消息型
466
+ * @param {Object} 数据管理器
467
+ * @return {String} 回复内容
468
+ */
469
+ Drive.prototype.chat = async function(from_user, to_user, group, content, type, msg_type, db) {
470
+ var body = "";
471
+ return body;
472
+ };
473
+
474
+ /**
475
+ * 指令(类似命令提示符)
476
+ * @param {String} content 指令内容
477
+ * @return {String} 执行结果
478
+ */
479
+ Drive.prototype.run_cmd = async function(content) {
480
+ if (!content) {
481
+ content = "";
482
+ }
483
+ var ret;
484
+ try {
485
+ ret = this.cmd(content);
486
+ } catch (err) {
487
+ $.log.error("运行插件指令失败!", this.config.name, err);
488
+ }
489
+ if (!ret) {
490
+ ret = "";
491
+ }
492
+ return ret;
493
+ };
494
+
495
+ /**
496
+ * 聊天(通过聊天的方式驱动插件, 用于机器人开发)
497
+ * @param {String} from_user 发送消息人
498
+ * @param {String} to_user 接收消息人
499
+ * @param {String} content 内容
500
+ * @param {String} group 群组 如果是个人,群组为空
501
+ * @param {Number} type 群类型, 1永久会话/群、2临时会话/群
502
+ * @param {String} msg_type 消息类型, event事件型、message消息型。默认消息型
503
+ * @return {String} 回复内容
504
+ */
505
+ Drive.prototype.run_chat = async function(from_user, to_user, content, group, type, msg_type, db) {
506
+ if (!content) {
507
+ content = "";
508
+ }
509
+ if (!msg_type) {
510
+ msg_type = "message";
511
+ }
512
+ if (!group) {
513
+ group = "";
514
+ }
515
+ var ret;
516
+ try {
517
+ ret = this.chat(from_user, to_user, group, content, type, msg_type, db);
518
+ } catch (err) {
519
+ $.log.error("运行插件聊天失败!", this.config.name, err);
520
+ }
521
+ if (!ret) {
522
+ ret = "";
523
+ }
524
+ return ret;
525
+ };
526
+
527
+ /**
528
+ * 压缩主题模板
529
+ * @param {String} 要压缩的目录
530
+ * @returns {String} 打包成功返回压缩包文件地址
531
+ */
532
+ Drive.prototype.zip = async function(zip_dir = "/static/file/zip/") {
533
+ var dir = this.filename.dirname();
534
+ var file = ("./" + this.config.name + '.zip').fullname(zip_dir);
535
+ file.addDir();
536
+ var done = await compressing.zip.compressDir(dir, file);
537
+ if (file.hasFile()) {
538
+ return file;
539
+ }
540
+ return null;
541
+ }
542
+
543
543
  module.exports = Drive;