UNPKG

wiz-frameworks

Version:

wizlong react framework

119 lines (110 loc) 3.4 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /** * 解析路由格式 * @author mll * @date 2019-9-24 */ import { onError } from '../utils'; import buildRotue from './createDynamicRoutes'; import { createRoutes } from './core'; /** * 构建路由主要方法 * @param {*} app * @param {*} rotues */ export default (function (app) { var rotues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; if (!rotues || rotues.length === 0) { onError('buildCreateRotues: 路由参数不能为空'); return rotues; } var r = []; rotues.forEach(function (config) { if (_checkConfig(config)) { config['childRoutes'] = _buildChildRotues(app, config['childRoutes']); r.push(config); } else { return; } }); return createRoutes(app, function () { return r; }); }); /** * 构建子路由 * @param {*} rotues */ var _buildChildRotues = function _buildChildRotues(app, rotues) { var r = []; rotues.forEach(function (rotue) { var cr = _checkChildRoutes(app, rotue); cr && r.push(cr); }); return r; }; /** * 检查路由基础属性 * @param {*} config */ var _checkConfig = function _checkConfig(config) { var f = true; if (!config) { onError('buildCreateRotues: 路由参数不能为空'); f = false; } else if (!config['path']) { onError('buildCreateRotues: path 路由路径不能为空'); f = false; } else if (!config['component']) { onError('buildCreateRotues: component 路由基础容器不能为空'); f = false; } else if (!config['indexRoute']) { onError('buildCreateRotues: indexRoute 初始路由不能为空'); f = false; } else if (!config['childRoutes'] || config['childRoutes'].length === 0) { onError('buildCreateRotues: childRoutes 子路由不能为空'); f = false; } return f; }; /** * 检查子路由属性 * @param {*} rotue:{key,path,component,title} */ var _checkChildRoutes = function _checkChildRoutes(app, rotue) { var f = true; if (!rotue) { onError('checkChildRoutes: 子路由 参数 不能为空'); f = false; } else if (!rotue['key']) { onError('checkChildRoutes: 子路由 key 不能为空'); f = false; } else if (rotue['key'] !== '404' && !rotue['path']) { onError('checkChildRoutes: 子路由 path 不能为空'); f = false; } else if (!rotue['component']) { onError('checkChildRoutes: 子路由 component 不能为空'); f = false; } else if (!rotue['title']) { onError('checkChildRoutes: 子路由 title 不能为空'); f = false; } return f ? _buildRotue(app, rotue) : undefined; }; /** * 初始化路由结构 * @param {*} title * @param {*} path * @param {*} component * @param {*} model */ var initRotue = function initRotue(rotue) { return _extends({ model: [] }, rotue); }; /** * 构建路由 * @param {*} rotue */ var _buildRotue = function _buildRotue(app, rotue) { return buildRotue(app, initRotue(rotue)); };