UNPKG

mm_os

Version:

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

447 lines (406 loc) 11.2 kB
/** * 视图模型 * @class */ class Model { /** * 配置参数 * @param {object} config 配置参数 */ constructor(config) { } } /** * 获取显示类型 * @param {string} key 键值 * @param {boolean} f 是否带格式 * @returns {string} 返回显示类型 */ Model.prototype.getType = function(key, f) { var typeMap = { 'search': f ? 'select' : '', 'view': 'text', 'table': f ? 'select' : 'text', 'edit': f ? 'select' : 'input', 'list': 'text', 'details': 'text', 'batch': f ? 'select' : '', 'filter': f ? 'select' : '' }; return typeMap[key] !== undefined ? typeMap[key] : (f ? 'select' : ''); }; /** * 查询显示方式 * @param {Array} arr 显示类型数组 * @param {string} key 键值 * @param {boolean} f 是否带格式 * @returns {string} 返回显示方式 */ Model.prototype.getShow = function(arr, key, f) { var bl = false; var show = ''; for (var i = 0; i < arr.length; i++) { var str = arr[i]; if (str.indexOf(key + '_no') !== -1) { bl = true; break; } else if (str.indexOf(key + '_input') !== -1) { show = 'input'; break; } else if (str.indexOf(key + '_select') !== -1) { show = 'select'; break; } else if (str.indexOf(key + '_text') !== -1) { show = 'text'; break; } } if (!show && !bl) { show = this.getType(key, f); } return show; }; /** * 获取表格所需键值对 * @param {object} model 模型 * @returns {object} 返回视图所用模型 */ Model.prototype.field = async function(model) { var list = model.param.list; if (!list) { model.param.list = []; return []; } var field = this._getFieldByFileType(model); var requireds = this._getRequiredFields(model); var field_names = field.replace(/`/g, '').split(','); return this._processFieldList(list, field_names, requireds, model); }; /** * 根据文件类型获取字段 * @param {object} model 模型 * @returns {string} 字段字符串 * @private */ Model.prototype._getFieldByFileType = function(model) { if (model.file.indexOf('_form') !== -1 || model.file.indexOf('_view') !== -1) { return model.sql.field_obj || ''; } return model.sql.field_default || ''; }; /** * 获取必填字段 * @param {object} model 模型 * @returns {Array} 必填字段数组 * @private */ Model.prototype._getRequiredFields = function(model) { var add = model.param.add; return add ? add.body_required : []; }; /** * 处理字段列表 * @param {Array} list 字段列表 * @param {Array} field_names 字段名数组 * @param {Array} requireds 必填字段数组 * @param {object} model 模型 * @returns {Array} 处理后的字段列表 * @private */ Model.prototype._processFieldList = function(list, field_names, requireds, model) { var pm = []; for (var i = 0; i < list.length; i++) { var o = list[i]; var name = o.name; if (field_names.indexOf(name) !== -1) { var obj = this._processField(o, requireds, model); pm.push(obj); } } return pm; }; /** * 处理单个字段 * @param {object} field 字段对象 * @param {Array} requireds 必填字段数组 * @param {object} model 模型 * @returns {object} 处理后的字段对象 * @private */ Model.prototype._processField = function(field, requireds, model) { var obj = { ...field}; var format_info = this._getFieldFormat(obj.name, model); if (format_info.format) { obj.title = format_info.format.title; obj.format = format_info.format; } if (requireds.indexOf(obj.name) !== -1) { obj.required = true; } obj.show = this._getFieldShow(obj.description, format_info.has_format); return obj; }; /** * 获取字段格式 * @param {string} fieldName 字段名 * @param {object} model 模型 * @returns {object} 格式信息 * @private */ Model.prototype._getFieldFormat = function(fieldName, model) { var format = model.sql.format.getObj({ key: fieldName }); return { format: format, has_format: !!format }; }; /** * 获取字段显示配置 * @param {string} description 字段描述 * @param {boolean} has_format 是否有格式 * @returns {object} 显示配置 * @private */ Model.prototype._getFieldShow = function(description, has_format) { if (description.indexOf('##') === -1) { return this._getDefaultShowConfig(has_format); } return this._getCustomShowConfig(description, has_format); }; /** * 获取默认显示配置 * @param {boolean} has_format 是否有格式 * @returns {object} 默认显示配置 * @private */ Model.prototype._getDefaultShowConfig = function(has_format) { return { form: has_format ? 'select' : 'input', view: 'text', search: has_format ? 'select' : '', table: has_format ? 'select' : 'text', list: 'text', edit: has_format ? 'select' : 'input', details: 'text', batch: has_format ? 'select' : '', filter: has_format ? 'select' : '' }; }; /** * 获取自定义显示配置 * @param {string} description 字段描述 * @param {boolean} has_format 是否有格式 * @returns {object} 自定义显示配置 * @private */ Model.prototype._getCustomShowConfig = function(description, has_format) { var desc = description.between('##', '##'); var ar = desc.trim().split(' '); if (desc.indexOf('none') !== -1) { return this._getEmptyShowConfig(); } return this._buildShowConfig(ar, has_format); }; /** * 获取空显示配置 * @returns {object} 空显示配置 * @private */ Model.prototype._getEmptyShowConfig = function() { return { form: '', view: '', search: '', table: '', list: '', edit: '', details: '', batch: '', filter: '' }; }; /** * 构建显示配置 * @param {Array} configArray 配置数组 * @param {boolean} has_format 是否有格式 * @returns {object} 显示配置 * @private */ Model.prototype._buildShowConfig = function(configArray, has_format) { return { search: this.getShow(configArray, 'search', has_format), table: this.getShow(configArray, 'table', has_format), form: this.getShow(configArray, 'form', has_format), view: this.getShow(configArray, 'view', has_format), batch: this.getShow(configArray, 'batch', has_format), list: this.getShow(configArray, 'list', has_format), details: this.getShow(configArray, 'details', has_format), filter: this.getShow(configArray, 'filter', has_format), edit: this.getShow(configArray, 'edit', has_format) }; }; /** * 运算视图模型(css) * @param {object} model 模型 * @returns {object} 返回视图所用模型 */ Model.prototype.css = async function(model) { var css = {}; return css; }; /** * 运算视图模型(html) * @param {object} model 模型 * @returns {object} 返回视图所用模型 */ Model.prototype.html = async function(model) { var html = {}; if (!model.sql) { return html; } return html; }; /** * 运算视图模型(js) * @param {object} model 模型 * @returns {object} 返回视图所用模型 */ Model.prototype.js = async function(model) { var js = { data: [], query: [] }; var field = model.field; var path_start = model.file.indexOf('admin') !== -1 ? '/apis/' : '/api/'; this._processField(js, field, path_start); this._processQuery(js, model.param); return js; }; /** * 处理字段 * @param {object} js js对象 * @param {Array} field 字段列表 * @param {string} path_start 路径前缀 * @private */ Model.prototype._processField = function(js, field, path_start) { for (var i = 0; i < field.length; i++) { var o = field[i]; var format = o.format; if (format) { var obj = this._createFormatObj(o, format, path_start); if (obj) { js.data.push(obj); } } } }; /** * 创建格式对象 * @param {object} field 字段对象 * @param {object} format 格式对象 * @param {string} path_start 路径前缀 * @returns {object} 格式对象 * @private */ Model.prototype._createFormatObj = function(field, format, path_start) { if (format.table) { return this._createTableFormatObj(field, format, path_start); } else { return this._createListFormatObj(field, format); } }; /** * 创建表格格式对象 * @param {object} field 字段对象 * @param {object} format 格式对象 * @param {string} path_start 路径前缀 * @returns {object} 表格格式对象 * @private */ Model.prototype._createTableFormatObj = function(field, format, path_start) { var basename = format.table.split('_').splice(1).join('_'); var name = 'list_' + basename; var id = format.nav_id || format.key; var path = path_start + format.table.replace('_', '/') + '?size=0'; var obj = { basename, title: format.title, id, name, field: format.name, value: [], path }; return this._addSpecialFields(obj, field.name, name); }; /** * 添加特殊字段 * @param {object} obj 对象 * @param {string} field_name 字段名 * @param {string} label 标签 * @returns {object} 新对象 * @private */ Model.prototype._addSpecialFields = function(obj, field_name, label) { var new_obj = { ...obj }; if (field_name === 'father_id' || field_name === 'fid') { new_obj.father_id = field_name; } if (field_name === 'title') { new_obj.title_name = 'title'; } return new_obj; }; /** * 创建列表格式对象 * @param {object} field 字段对象 * @param {object} format 格式对象 * @returns {object} 列表格式对象 * @private */ Model.prototype._createListFormatObj = function(field, format) { var basename = format.key; var name = 'arr_' + basename; return { title: format.title, name, value: format.list }; }; /** * 处理查询 * @param {object} js js对象 * @param {object} param 参数对象 * @private */ Model.prototype._processQuery = function(js, param) { if (param && param.get) { var arr = param.get.query_required.concat(param.get.query); var lt = param.list; for (var i = 0; i < arr.length; i++) { var o = lt.getObj({ name: arr[i] }); if (o) { js.query.push({ title: o.title, name: o.name, type: o.type, select: o.description ? (o.description.indexOf('(') !== -1) : false }); } } } }; /** * 运算视图模型 * @param {object} model 模型 * @returns {object} 返回视图所用模型 */ Model.prototype.run = async function(model) { model.field = await this.field(model); model.js = await this.js(model); model.html = await this.html(model); model.css = await this.css(model); return model; }; module.exports = Model;