mm_os
Version:
MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。
297 lines (274 loc) • 6.64 kB
JavaScript
const {
Drive
} = require('mm_machine');
/**
* 插件基类
* 负责管理插件的生命周期、资源加载和依赖管理
*/
class Plugin extends Drive {
static config = {
// 插件名称
name: '',
// 插件标题
title: '插件',
// 插件描述
description: '介绍插件的作用',
// 插件版本
version: '1.0.0',
// 用于什么应用
app: 'tool',
// 作者
author: 'qww',
// 主文件
main: './index.js',
// git仓库地址
git: '',
// 依赖的npm模块
dependencies: []
};
/**
* 构造函数
* @param {object} config 配置参数
* @param {object} parent 父对象
*/
constructor(config, parent) {
super({ ...Plugin.config, ...config || {} }, parent);
this.options = {};
}
}
/**
* 获取模板目录
* @returns {string} 模板目录
*/
Plugin.prototype.getTplDir = function () {
return __dirname;
};
/**
* 初始化核心
* @param {object} app 应用实例
* @param {object} eventer 事件管理器
* @param {object} logger 日志管理器
* @private
*/
Plugin.prototype._initCore = async function (app, eventer, logger) {
// 初始化依赖项
if (logger) {
this.setLogger(logger);
}
if (eventer) {
this.getEventer = function () {
return eventer;
};
}
if (app) {
this.getApp = function () {
return app;
};
this._db = app.db;
this._cache = app.cache;
this._filer = app.filer;
}
// // 初始化管理
// await this._initManager();
// // 加载资源
// await this._loadSources();
// // 初始化资源
// await this._initSources();
// 初始化配置
this._initOptions();
};
/**
* 获取应用实例
* @returns {object} 应用实例
*/
Plugin.prototype.getApp = function () {
// 初始化资源
return this.getParent();
};
/**
* 获取插件配置
* @returns {object} 插件配置
*/
Plugin.prototype.getOptions = function () {
return this.options;
};
/**
* 插件初始化之后
*/
Plugin.prototype._initOptions = function () {
var options = this.getConfigOptions();
this.options = this.mergeOptions(options);
this.backupOptions();
};
/**
* 转换数字类型值
* @param {*} value 原始值
* @returns {number} 转换后的值
* @private
*/
Plugin.prototype._convertNumber = function (value) {
if (value === 'null') return 0;
return Number(value || '0');
};
/**
* 转换布尔类型值
* @param {*} value 原始值
* @returns {boolean} 转换后的值
* @private
*/
Plugin.prototype._convertBoolean = function (value) {
if (!value) return false;
if (typeof value === 'boolean') return value;
if (typeof value === 'string') {
return value === '1' || value === 'true';
}
return false;
};
/**
* 转换对象类型值
* @param {*} value 原始值
* @returns {object} 转换后的值
* @private
*/
Plugin.prototype._convertObject = function (value) {
if (value === 'null') return {};
return value.toJSON ? value.toJSON() : {};
};
/**
* 转换数组类型值
* @param {*} value 原始值
* @returns {Array} 转换后的值
* @private
*/
Plugin.prototype._convertArray = function (value) {
if (value === 'null') return [];
return value.toJSON ? value.toJSON() : [];
};
/**
* 转换字符串类型值
* @param {*} value 原始值
* @returns {string} 转换后的值
* @private
*/
Plugin.prototype._convertString = function (value) {
if (value === 'null') return null;
return value || '';
};
/**
* 转换单个配置项的值
* @param {object} option 配置项
* @returns {*} 转换后的值
* @private
*/
Plugin.prototype._convertOptionValue = function (option) {
var val = option.value;
if (val === 'null') return null;
var type = option.type;
switch (type) {
case 'number':
return this._convertNumber(val);
case 'boolean':
return this._convertBoolean(val);
case 'object':
return this._convertObject(val);
case 'array':
return this._convertArray(val);
case 'string':
return this._convertString(val);
default:
return val;
}
};
/**
* 获取配置参数
* @returns {object} 返回配置参数
*/
Plugin.prototype.getConfigOptions = function () {
var op = this.config.options || [];
var dict = {};
for (var i = 0; i < op.length; i++) {
var o = op[i];
dict[o.name] = this._convertOptionValue(o);
}
return dict;
};
/**
* 设计配置
* @param {object} body 配置参数
*/
Plugin.prototype.designOptions = 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} options 配置参数
*/
Plugin.prototype.saveOptions = function (options) {
this.options = options;
this.backupOptions();
};
/**
* 保存配置
*/
Plugin.prototype.backupOptions = function () {
var file = this._getCacheOptionsFile();
file = file.fullname();
file.addDir();
file.saveJson(this.options);
};
/**
* 合并配置
* @param {object} options 配置参数
* @returns {object} 返回配置参数
*/
Plugin.prototype.mergeOptions = function (options) {
var file = this._getCacheOptionsFile();
var options_cache = file.loadJson();
if (options_cache) {
$.push(options, options_cache);
}
return options;
};
/**
* 获取缓存配置文件
* @returns {string} 缓存配置文件
*/
Plugin.prototype._getCacheOptionsFile = function () {
let plugin_name = this.config.name;
let app_name = this.getApp().config.name;
var file = `/cache/${app_name}/${plugin_name}/config.json`.fullname();
return file;
};
/**
* 获取模型
* @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 || name;
model.path = '/' + app_name + '/' + name;
return model;
};
exports.Plugin = Plugin;