提交 76451936 authored 作者: vipcxj's avatar vipcxj

模块信息获取机制大改。

上级 c465c148
const proxy = {
"target": "http://192.168.1.5:8090",
"target": "http://192.168.1.121:8180",
"changeOrigin": true,
"pathRewrite": {
"^/api": "/big-machine/restful-services"
......@@ -28,7 +28,8 @@ module.exports = {
"/api/user": proxy,
"/api/domain": proxy,
"/api/datasource": proxy,
"/api/modules": proxy,
"/api/module": proxy,
"/api/configure": proxy,
}
},
"production": {
......
......@@ -73,8 +73,6 @@ SiderMenu.propTypes = {
name: PropTypes.string,
icon: PropTypes.string,
text: PropTypes.string,
hint: PropTypes.string,
path: PropTypes.string,
children: PropTypes.array,
})),
};
......
import { flatMap } from 'lodash';
import { fetchMenus, fetchModuleInfos, fetchModuleLayout } from '../services/modules';
const data = {
init: false,
modules: [],
flags: {
menus: false,
infos: false,
layout: false,
},
menus: [],
infos: [],
layout: [],
};
export default data;
export const setModules = (modules) => {
data.init = true;
data.modules = modules;
// export const setMenus = (menus) => {
// data.init = true;
// data.menus = menus;
// };
const combineMenu = (menu1, menu2) => {
return {
...menu2,
children: combineMenus(menu1.children, menu2.children),
};
};
export const getModules = () => {
return data.modules;
const combineMenus = (menus1, menus2) => {
const menus = [...menus1];
for (const menu of menus2) {
let find = false;
for (const i in menus) {
if (menus[i].name === menu.name) {
menus[i] = combineMenu(menus[i], menu);
find = true;
break;
}
}
if (!find) {
menus.push(menu);
}
}
return menus;
};
export const isInited = () => {
return data.init;
export const getMenus = async () => {
if (!data.flags.menus) {
const context = require.context('../register/modules', true, /^.*\.(js|jsx|json)/);
const staticMenus = flatMap(context.keys(), context);
const dynamicMenus = await fetchMenus();
data.menus = combineMenus(staticMenus, dynamicMenus);
data.flags.menus = true;
}
return data.menus;
};
// noinspection EqualityComparisonWithCoercionJS
export const getModule = id => data.modules.filter(m => m.id == id).pop(); // eslint-disable-line eqeqeq, max-len
export const invalidateMenus = () => {
data.flags.menus = false;
};
export const getPath = (id) => {
const module = getModule(id);
if (module.parent) {
const parent = getModule(module.parent);
return [...getPath(parent.id), id];
} else {
return [id];
export const getModuleInfos = async () => {
if (!data.flags.infos) {
data.infos = await fetchModuleInfos();
data.flags.infos = true;
}
return data.infos;
};
export const getChildren = (id) => {
const children = [];
for (const module of data.modules) {
// noinspection EqualityComparisonWithCoercionJS
if (module.parent == id) { // eslint-disable-line eqeqeq
children.push(module);
}
}
return children;
export const getModuleInfo = async (name) => {
const infos = await getModuleInfos();
return (infos || []).filter(info => info.name === name).pop();
};
export function foreachModule(callback) {
for (const module of data.modules) {
callback(module);
}
}
export const invalidateInfos = () => {
data.flags.infos = false;
};
export const setModuleConfigure = (id, configure) => {
const module = getModule(id);
if (module) {
module.configure = configure;
export const getModuleLayout = async (name) => {
if (!data.flags.layout) {
data.layout = await fetchModuleLayout(name);
data.flags.layout = true;
}
return data.layout;
};
export const getModuleConfigure = (id) => {
const module = getModule(id);
return module ? module.configure : null;
export const invalidateLayout = () => {
data.flags.layout = false;
};
// export const setModules = (modules) => {
// data.init = true;
// data.modules = modules;
// };
//
// export const getModules = () => {
// return data.modules;
// };
//
// export const isInited = () => {
// return data.init;
// };
//
// // noinspection EqualityComparisonWithCoercionJS
// export const getModule = id => data.modules.filter(m => m.id == id).pop(); // eslint-disable-line eqeqeq, max-len
//
// export const getPath = (id) => {
// const module = getModule(id);
// if (module.parent) {
// const parent = getModule(module.parent);
// return [...getPath(parent.id), id];
// } else {
// return [id];
// }
// };
//
// export const getChildren = (id) => {
// const children = [];
// for (const module of data.modules) {
// // noinspection EqualityComparisonWithCoercionJS
// if (module.parent == id) { // eslint-disable-line eqeqeq
// children.push(module);
// }
// }
// return children;
// };
//
// export function foreachModule(callback) {
// for (const module of data.modules) {
// callback(module);
// }
// }
//
// export const setModuleConfigure = (id, configure) => {
// const module = getModule(id);
// if (module) {
// module.configure = configure;
// }
// };
//
// export const getModuleConfigure = (id) => {
// const module = getModule(id);
// return module ? module.configure : null;
// };
import { routerRedux } from 'dva/router';
import { logout, fetchModuleInfos } from '../../services/main';
import { logout } from '../../services/main';
import { fetchDomains, switchDomain, currentDomain } from '../../services/domain';
import { cookie } from '../../utils/config';
import { setModules, getModules, getModule, getPath, foreachModule, isInited } from '../../data/modules';
import { getMenus } from '../../data/modules';
import { setCookie, delCookie, fullPath, getCookie } from '../../utils/helper';
const createMenus = () => {
const menus = [];
const makePath = (_menus, path) => {
if (path.length === 0) {
return null;
}
const [cur, ...rest] = path;
const find = _menus.filter(menu => menu.name === `${cur}`).pop();
if (find) {
if (rest.length === 0) {
return find;
} else {
return makePath(find.children, rest);
}
} else {
let container = _menus;
for (const id of path) {
const m = getModule(id);
const menu = {
name: `${m.id}`,
text: m.name,
children: [],
};
container.push(menu);
container = menu.children;
}
}
};
const makeIcon = (menu) => {
const children = menu.children;
if (children.length === 0) {
menu.icon = 'file'; // eslint-disable-line no-param-reassign
} else {
menu.icon = 'folder'; // eslint-disable-line no-param-reassign
for (const child of children) {
makeIcon(child);
}
}
const createMenu = (configure) => {
return {
name: configure.name,
icon: configure.icon,
text: configure.showName,
children: [
...(configure.children || []).map(child => createMenu(child)),
...(configure.modules || []).map(module => ({
name: module.name,
icon: module.icon,
text: module.showName,
})),
],
};
foreachModule((module) => {
const path = getPath(module.id);
makePath(menus, path);
});
for (const menu of menus) {
makeIcon(menu);
}
return menus;
};
const createMenus = (menusConfigure) => {
return (menusConfigure || []).map(configure => createMenu(configure));
};
// const createMenus = () => {
// const menus = [];
// const makePath = (_menus, path) => {
// if (path.length === 0) {
// return null;
// }
// const [cur, ...rest] = path;
// const find = _menus.filter(menu => menu.name === `${cur}`).pop();
// if (find) {
// if (rest.length === 0) {
// return find;
// } else {
// return makePath(find.children, rest);
// }
// } else {
// let container = _menus;
// for (const id of path) {
// const m = getModule(id);
// const menu = {
// name: `${m.id}`,
// text: m.name,
// children: [],
// };
// container.push(menu);
// container = menu.children;
// }
// }
// };
// const makeIcon = (menu) => {
// const children = menu.children;
// if (children.length === 0) {
// menu.icon = 'file'; // eslint-disable-line no-param-reassign
// } else {
// menu.icon = 'folder'; // eslint-disable-line no-param-reassign
// for (const child of children) {
// makeIcon(child);
// }
// }
// };
// foreachModule((module) => {
// const path = getPath(module.id);
// makePath(menus, path);
// });
// for (const menu of menus) {
// makeIcon(menu);
// }
// return menus;
// };
export default {
namespace: 'main',
......@@ -101,11 +121,8 @@ export default {
yield put({ type: 'queryUserSuccess', payload: user });
},
*fetchModules(action, { put, call }) {
if (!isInited()) {
const modules = yield call(fetchModuleInfos);
setModules(modules);
}
yield put({ type: 'queryMenusSuccess', payload: createMenus(getModules()) });
const configures = yield call(getMenus);
yield put({ type: 'queryMenusSuccess', payload: createMenus(configures) });
},
*logout(action, { put, call }) {
yield call(logout);
......
export default [
{
name: 'group0',
showName: '测试组0',
children: [
{
name: 'group00',
showName: '测试组00',
modules: [{
name: 'test0',
showName: '测试模块0',
layout: {
route: 'task',
},
}],
},
],
},
{
name: 'group1',
showName: '测试组1',
modules: [
{
name: 'test1',
showName: '测试模块1',
layout: {
route: 'task',
},
},
{
name: 'test2',
showName: '测试模块2',
layout: {
route: 'task',
},
},
],
},
];
import React from 'react';
import PropTypes from 'prop-types';
import { Router } from 'dva/router';
import _ from 'lodash';
import config from './utils/config';
import { isAuthed, hasDomain } from './utils/auth';
import { fullPath, makePromise0 } from './utils/helper';
import { fetchModuleInfos } from './services/main';
import App from './routes/app';
import { isInited, setModules, foreachModule, getChildren } from './data/modules';
import { getMenus, getModuleInfo, getModuleLayout } from './data/modules';
import Monk from './routes/main/monk';
const { contextPath } = config;
......@@ -35,66 +35,66 @@ const authenticated = (nextState, replace) => {
maybeSwitch(nextState, replace);
};
const createRoute = async (app, module) => {
const route = {
path: `${module.id}`,
name: module.name,
};
if (!module.group) {
if (module.route) {
const modelBundle = await makePromise0(require('bundle!./models/main/modules/' + module.route + '/index.js')); // eslint-disable-line import/no-dynamic-require, prefer-template, global-require
registerModel(app, modelBundle);
const routeBundle = await makePromise0(require('bundle!./routes/main/modules/' + module.route + '/index.js')); // eslint-disable-line import/no-dynamic-require, prefer-template, global-require
route.component = routeBundle;
if (routeBundle.route) {
for (const key in routeBundle.route) {
if ({}.hasOwnProperty.call(routeBundle.route, key)) {
route[key] = routeBundle.route[key];
}
}
}
} else {
route.component = Monk;
}
} else {
route.component = Monk;
route.getChildRoutes = (nextState, cb) => {
createModuleRoutes(app, getChildren(module.id))
const createRoute = async (app, { name, showName, modules, children }) => {
// noinspection JSUnusedLocalSymbols
return {
path: name,
name: showName,
component: Monk,
getChildRoutes(nextState, cb) {
createRoutes(app, modules, children)
.then((result) => {
cb(null, result);
})
.catch((err) => {
cb(err, null);
});
};
}
return route;
},
};
};
const fetchModules = async () => {
if (!isInited()) {
setModules(await fetchModuleInfos());
}
const modules = [];
foreachModule((module) => {
if (!module.parent) {
modules.push(module);
}
});
return modules;
};
const createModuleRoutes = async (app, modules) => {
const createRoutes = async (app, modules, groups) => {
const routes = [];
for (const module of modules) {
routes.push(await createRoute(app, module));
if (modules) {
for (const module of modules) {
let info;
let layout;
if (_.isString(module)) {
info = await getModuleInfo(module);
layout = await getModuleLayout(module);
} else {
info = module;
layout = module.layout;
}
const { name, showName } = info;
const route = {
path: name,
name: showName,
};
if (layout.route) {
const modelBundle = await makePromise0(require('bundle!./models/main/modules/' + layout.route + '/index.js')); // eslint-disable-line import/no-dynamic-require, prefer-template, global-require
registerModel(app, modelBundle);
const routeBundle = await makePromise0(require('bundle!./routes/main/modules/' + layout.route + '/index.js')); // eslint-disable-line import/no-dynamic-require, prefer-template, global-require
route.component = routeBundle;
if (routeBundle.route) {
for (const key in routeBundle.route) {
if ({}.hasOwnProperty.call(routeBundle.route, key)) {
route[key] = routeBundle.route[key];
}
}
}
} else {
route.component = Monk;
}
routes.push(route);
}
}
if (groups) {
for (const group of groups) {
routes.push(await createRoute(app, group));
}
}
routes.push({
path: '*',
onEnter(nextState, replace) {
replace(fullPath('/main'));
},
});
return routes;
};
......@@ -136,15 +136,9 @@ function RouterConfig({ history, app }) {
}, 'main');
},
getChildRoutes: (nextState, cb) => {
fetchModules()
.then(() => {
const lvl0 = [];
foreachModule((module) => {
if (!module.parent) {
lvl0.push(module);
}
});
createModuleRoutes(app, lvl0)
getMenus()
.then((menus) => {
createRoutes(app, [], menus)
.then((result) => {
cb(null, result);
})
......@@ -155,6 +149,25 @@ function RouterConfig({ history, app }) {
.catch((err) => {
cb(err, null);
});
// fetchModules()
// .then(() => {
// const lvl0 = [];
// foreachModule((module) => {
// if (!module.parent) {
// lvl0.push(module);
// }
// });
// createModuleRoutes(app, lvl0)
// .then((result) => {
// cb(null, result);
// })
// .catch((err) => {
// cb(err, null);
// });
// })
// .catch((err) => {
// cb(err, null);
// });
},
},
],
......
......@@ -75,27 +75,27 @@ export async function getGlobalDatasourceMeta(name) {
}
export async function calcModuleDatasource(mdName, dsName, { pst, psz, filters = [], sortBys = [], sortTypes = [], params = {} }) {
return request(`/api/modules/module/${mdName}/datasource/${dsName}`, makeQueryParams({ pst, psz, filters, sortBys, sortTypes, params }));
return request(`/api/module/user/${mdName}/datasource/${dsName}`, makeQueryParams({ pst, psz, filters, sortBys, sortTypes, params }));
}
export async function countModuleDatasource(mdName, dsName, { filters = [], params = {} }) {
return request(`/api/modules/module/${mdName}/datasource/${dsName}/count`, makeQueryParams({ filters, params }));
return request(`/api/module/user/${mdName}/datasource/${dsName}/count`, makeQueryParams({ filters, params }));
}
export async function updateModuleDatasource(mdName, dsName, key, params = {}) {
return post(`/api/modules/module/${mdName}/datasource/${dsName}/update`, {
return post(`/api/module/user/${mdName}/datasource/${dsName}/update`, {
key,
params,
});
}
export async function createModuleDatasource(mdName, dsName, params = {}) {
return post(`/api/modules/module/${mdName}/datasource/${dsName}/create`, {
return post(`/api/module/user/${mdName}/datasource/${dsName}/create`, {
params,
});
}
export async function getModuleDatasourceMeta(mdName, dsName) {
return request(`/api/modules/module/${mdName}/datasource/${dsName}/meta`);
return request(`/api/module/user/${mdName}/datasource/${dsName}/meta`);
}
import request from '../utils/request';
import post from '../utils/post';
export async function logout() {
return post('/api/auth/logout');
}
export async function fetchModuleInfos() {
return request('/api/module/all/info');
}
import request from '../utils/request';
export async function allModuleInfos() {
return request('/api/modules/info');
export async function fetchMenus() {
return request('/api/configure/user/menus');
}
export async function getModuleConfigure(name) {
return request(`/api/modules/module/${name}/configure`);
export async function fetchModuleInfos() {
return request('/api/module/user/info');
}
export async function fetchModuleLayout(name) {
return request(`/api/module/user/${name}/layout`);
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论