UNPKG

mm_os

Version:

MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。

138 lines (129 loc) 3.06 kB
const compressing = require('compressing'); const Index = require('mm_machine').Index; const Drive = require('./drive').Drive; /** * 主题模板管理类 * @class */ class Template extends Index { /** * 配置参数 * @type {object} */ static config = { /** * 名称 * @type {string} */ name: '', /** * 标题 * @type {string} */ title: 'template管理', /** * 描述 * @type {string} */ description: '这是template管理器', /** * 检索文件名 * @type {string} */ filename: 'template.json', /** * 模板目录 * @type {string} */ tpl_dir: __dirname, /** * 基础目录 * @type {string} */ base_dir: '../common/template'.fullname(__dirname), /** * 自定义目录,加载项目自定义资源 * @type {string} */ dir: './static/template'.fullname(), /** * 搜索模式 dir按目录搜索 | file按文件名搜索 * @type {string} */ search_way: 'file', /** * 是否懒加载 * @type {boolean} */ lazy_load: true, /** * 模式 * 1.生产模式,改变文件不会重新加载 * 2.热更新模式,改变配置文件会重新加载配置,不重新加载脚本 * 3.热重载模式,改变配置文件都会加载配置和脚本 * 4.重载模式,执行完后重新加载脚本,避免变量污染 * 5.热更新+重载模式,改变配置文件重新加载配置和脚本,执行完后重新加载脚本 * @type {number} */ mode: 3 }; /** * 构造函数 * @param {object} config 配置参数 * @param {object} parent 父项 */ constructor(config, parent) { super({ ...Template.config, ...config }, parent); } } Template.prototype.Drive = Drive; /** * 解压模板 * @param {string} file 模板压缩文件 * @returns {object} 解压后的文件对象 */ Template.prototype.unzip = async function(file) { var done = await compressing.zip.uncompress(file, '/static/template'.fullname()); return done; }; /** * 获取模板 * @param {string} key 模板键名 * @returns {object} 模板对象 */ Template.prototype.get = function(key) { return this.mods[key]; }; /** * 主题模板模板池 */ if (!$.pool.template) { $.pool.template = {}; } /** * 主题模板管理器,用于创建缓存 * @param {string} scope 作用域 * @param {string} title 标题 * @returns {object} 返回一个缓存类 */ function templateAdmin(scope, title) { var sc = scope || $.val.scope + ''; var obj = $.pool.template[sc]; if (!obj) { $.pool.template[sc] = new Template({ name: sc, title: title }); obj = $.pool.template[sc]; } return obj; } /** * @module 导出模板管理器 */ if ($.admin) { $.admin.template = templateAdmin; } module.exports = { Template };