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

1.增加快速导航支持

上级 1ba9bbc8
......@@ -2,6 +2,7 @@ import { routerRedux } from 'dva/router';
import { fetchDomains, switchDomain, currentDomain } from '../services/domain';
import { fullPath } from '../utils/helper';
import { getDomain, setDomain } from '../utils/auth';
import config from '../utils/config';
export default {
......@@ -30,7 +31,11 @@ export default {
const { path, name } = yield call(currentDomain);
yield call(setDomain, name, path);
yield put({ type: 'queryInit', payload: path });
if (config.fastNavigationPage) {
yield put(routerRedux.push(fullPath(`/${config.fastNavigationPage}`)));
} else {
yield put(routerRedux.push(fullPath('/main')));
}
},
},
......
......@@ -4,6 +4,7 @@ import { fullPath } from '../utils/helper';
import { setToken, setUser, setDomain, histories } from '../utils/auth';
import { switchDomain, currentDomain } from '../services/domain';
import { errors } from '../utils/error';
import config from '../utils/config';
export default {
namespace: 'login',
......@@ -40,8 +41,12 @@ export default {
yield call(switchDomain, path);
const { name } = yield call(currentDomain);
yield call(setDomain, name, path);
if (config.fastNavigationPage) {
yield put(routerRedux.push(fullPath(`/${config.fastNavigationPage}`)));
} else {
yield put(routerRedux.push(fullPath('/main')));
}
}
},
},
subscriptions: {},
......
......@@ -3,7 +3,7 @@ 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 { isAuthed, hasDomain, histories } from './utils/auth';
import { fullPath, makePromise0 } from './utils/helper';
import { processError } from './utils/error';
import App from './routes/app';
......@@ -36,14 +36,28 @@ const authenticated = async (replace) => {
await maybeSwitch(replace);
};
const createRoute = async (app, { name, showName, modules, children }) => {
const combinePath = (base, path) => {
if (!base) {
return path;
}
if (base[base.length - 1] === '/') {
return `${base}${path}`;
} else {
return `${base}/${path}`;
}
};
const createRoute = async (app, group, basePath) => {
const { name, showName, modules, children } = group;
const theFullPath = combinePath(basePath, name);
// noinspection JSUnusedLocalSymbols
return {
path: name,
fullPath: theFullPath,
name: showName,
component: Monk,
getChildRoutes(nextState, cb) {
createRoutes(app, modules, children)
createRoutes(app, modules, children, theFullPath)
.then((result) => {
cb(null, result);
})
......@@ -55,7 +69,7 @@ const createRoute = async (app, { name, showName, modules, children }) => {
};
const createRoutes = async (app, modules, groups) => {
const createRoutes = async (app, modules, groups, basePath) => {
const routes = [];
if (modules) {
for (const module of modules) {
......@@ -68,9 +82,10 @@ const createRoutes = async (app, modules, groups) => {
info = module;
layout = module.layout;
}
const { name, showName } = info;
const { name, showName, icon, description } = info;
const route = {
path: name,
fullPath: combinePath(basePath, name),
name: showName,
};
if (layout.route) {
......@@ -91,12 +106,39 @@ const createRoutes = async (app, modules, groups) => {
if (route.onEnter) {
const onEnter = route.onEnter;
route.onEnter = (nextState, replace, cb) => {
console.log(nextState);
if (nextState && nextState.location && nextState.location.pathname === route.fullPath) {
histories.pushHistory('module', {
name,
showName,
icon,
description,
path: route.fullPath,
}).then(() => new Promise((resolve, reject) => {
onEnter(nextState, replace, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
})).then(() => cb()).catch(err => cb(err));
} else {
return onEnter(nextState, replace, cb);
}
};
} else {
route.onEnter = (nextState) => {
console.log(nextState);
route.onEnter = (nextState, replace, cb) => {
if (nextState && nextState.location && nextState.location.pathname === route.fullPath) {
histories.pushHistory('module', {
name,
showName,
icon,
description,
path: route.fullPath,
}).then(() => cb()).catch(err => cb(err));
} else {
cb();
}
};
}
routes.push(route);
......@@ -104,7 +146,7 @@ const createRoutes = async (app, modules, groups) => {
}
if (groups) {
for (const group of groups) {
routes.push(await createRoute(app, group));
routes.push(await createRoute(app, group, basePath));
}
}
return routes;
......@@ -154,7 +196,7 @@ function RouterConfig({ history, app }) {
getChildRoutes: (nextState, cb) => {
getMenus()
.then((menus) => {
createRoutes(app, [], menus)
createRoutes(app, [], menus, `${contextPath}/main`)
.then((result) => {
cb(null, result);
})
......@@ -170,6 +212,20 @@ function RouterConfig({ history, app }) {
],
},
];
if (config.fastNavigationPage) {
routes[0].childRoutes.push({
path: 'fastNav',
onEnter: (ignored, replace, cb) => {
authenticated(replace).then(() => cb()).catch(err => cb(err));
},
getComponent(ignored, cb) {
require.ensure([], (require) => {
registerModel(app, require('./models/main/modules/' + config.fastNavigationPage)); // eslint-disable-line import/no-dynamic-require, prefer-template
cb(null, require('./routes/main/modules/' + config.fastNavigationPage)); // eslint-disable-line import/no-dynamic-require, prefer-template
}, 'fastNav');
},
});
}
return (
<Router history={history} routes={routes} onError={processError} />
);
......
import _ from 'lodash';
const { isString, get, sortBy } = _;
/**
* @callback KeyExtractor
* @param value
* @return key
*/
/**
* 获取用于对比的key
* @param value
* @param {string|KeyExtractor} [keyMethod]
* @return {*}
*/
const getKey = (value, keyMethod) => {
if (!keyMethod) {
return value;
} else if (isString(keyMethod)) {
return get(value, keyMethod);
} else {
return keyMethod(value);
}
};
/**
* 在数组中查找某个对象,并返回
* @param {Array} array
* @param toFind 查找目标
* @param {string|KeyExtractor} [key] 用于判断2个数据是否相同
* @return result 找到的对象
*/
const findBy = (array, toFind, key) => {
if (array) {
for (const el of array) {
if (getKey(el, key) === toFind) {
return el;
}
}
}
return null;
};
/**
* 计算频率数据
* @param {Array} array 数据
* @param {string|KeyExtractor} [key] 用于判断2个数据是否相同
* @param {string} [order=desc] 怎么排序,可选值为asc和desc
*/
export const toHistogram = (array, key, order = 'desc') => {
if (!array || array.length === 0) {
return [];
}
const res = [];
let newKey;
if (!key) {
newKey = 'data';
} else if (isString(key)) {
newKey = `data.${key}`;
} else {
newKey = value => key(value.data);
}
for (const el of array) {
const find = findBy(res, getKey(el, key), newKey);
if (find) {
++find.num;
} else {
res.push({
data: el,
num: 1,
});
}
}
switch (order) {
case 'asc':
return _(res).sortBy(v => v.num).reverse().value();
case 'desc':
return sortBy(res, v => v.num);
default:
throw new Error(`unsupported order: ${order}`);
}
};
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论