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

模块信息获取机制大改。

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