mm_os
Version:
MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。
204 lines (192 loc) • 5.38 kB
JavaScript
const compressing = require('compressing');
const Item = require('mm_machine').Drive;
/**
* 挂件驱动类
* @augments {Item}
* @class
*/
class Drive extends Item {
static config = {
// 应用于,根据应用而判断使用
'app': 'home',
// 分组, 可以将特有的分一个组, 方便用户查询
'group': 'default',
// 分类, 例如: 查询生活类、监测类、便民类
'type': '默认',
// 脚本文件
'main': './pendant.js',
// 渲染
'template': './pendant.html',
// 配置
'options': [],
// 定位 由后台指定的展示位置
'location': []
};
/**
* 构造函数
* @param {object} config 配置参数
* @param {object} parent 父对象
* @class
*/
constructor(config, parent) {
super({ ...Drive.config, ...config }, parent);
// 默认启用热更新
this.mode = 3;
}
};
/**
* 创建一个参数模型
* @returns {object} 返回一个参数模型
*/
Drive.prototype.model = function () {
return {
// 应用于,根据应用而判断使用
'app': 'home',
// 名称, 用于动态增删改配置
'name': 'demo',
// 标题, 用于查询时
'title': '示例组件',
// 描述, 用于介绍该组件的作用
'description': '暂无描述',
// 执行顺序, 数值越小的越优先显示
'sort': 100,
// 分组, 可以将特有的分一个组, 方便用户查询
'group': 'default',
// 分类, 例如: 查询生活类、监测类、便民类
'type': '监测',
// 脚本文件, 非必传,可和渲染文件二选一传
'main': './pendant.js',
// 渲染, 非必传,可和脚本文件二选一传
'template': './pendant.html',
// 状态, 0为未开启 1为开启
'state': 1,
// 配置
'options': [],
// 定位 由后台指定的展示位置
'location': [{
'path': '/admin/index',
'position': 'main',
'available': 1
}]
};
};
/**
* 新建配置文件
* @param {string} file 文件
*/
Drive.prototype.newConfig = function (file) {
var fl = __dirname + '/config.tpl.json';
if (fl.hasFile()) {
var text = fl.loadText();
if (text) {
var l = $.slash;
var arr = file.split(l);
arr = arr.slice(arr.indexOf('app'));
var app = arr[1];
var name = file.dirname();
text = text.replaceAll('{0}', name);
text = text.replaceAll('{1}', app);
file.saveText(text);
}
}
};
/**
* 新建脚本
* @param {string} file 文件
*/
Drive.prototype.newScript = function (file) {
var fl = __dirname + '/script.tpl.js';
if (fl.hasFile()) {
var text = fl.loadText();
if (text) {
var l = $.slash;
var arr = file.split(l);
var name = arr[arr.length - 2];
text = text.replaceAll('{0}', name);
file.saveText(text);
}
}
var file_html = './pendant.html'.fullname(file.dirname());
if (!file_html.hasFile()) {
var flh = __dirname + '/pendant.html';
if (flh.hasFile()) {
var html = flh.loadText();
file_html.saveText(html);
}
}
};
/**
* 获取配置参数
* @param {object} body 设置配置
*/
Drive.prototype.designOption = function (body) {
var cg = this.config;
if (Array.isArray(body)) {
cg.options = body;
} else {
var options = cg.options || [];
var option = options.getObj({
name: body.name
});
if (option) {
Object.assign(option, body);
} else {
options.push(body);
}
cg.options = options;
}
};
/**
* 挂件模板
* @param {object} ctx 请求上下文
* @param {object} db 数据管理器,如: { next: async function{}, ret: {} }
* @returns {object} 执行结果
*/
Drive.prototype.tpl = async function (ctx, db) {
var html_file = this.config.template || './pendant.html';
return html_file.fullname(this.getDir()).loadText();
};
/**
* 组件主函数
* @param {object} ctx 请求上下文
* @param {object} db 数据管理器,如: { next: async function{}, ret: {} }
* @returns {object} 执行结果
*/
Drive.prototype.main = async function (ctx, db) {
var model = {};
var html_file = this.config.template || './pendant.html';
return db.tpl.view(html_file.fullname(this.getDir()), model);
};
/**
* 压缩主题模板
* @param {string} zip_dir 要压缩的目录
* @returns {string} 打包成功返回压缩包文件地址
*/
Drive.prototype.zip = async function (zip_dir = '/static/file/zip/') {
var dir = this.getDir();
var file = ('./' + this.config.name + '.zip').fullname(zip_dir);
file.addDir();
await compressing.zip.compressDir(dir, file);
if (file.hasFile()) {
return file;
}
return null;
};
/**
* 获取模型
* @param {string} type 模型类型
* @returns {object} 返回获取到的模型
*/
Drive.prototype.getModel = function (type) {
let model = { ...this.config };
let dir = this.getDir();
let l = $.slash;
let app_name = dir.between('app' + l, l);
let plugin_name = dir.between('plugin' + l, l);
let name = dir.basename();
model.app = app_name;
model.plugin = plugin_name;
model.name = model.name || app_name + '_' + name;
return model;
};
module.exports = Drive;