提交 3f4bd2a7 authored 作者: vipcxj's avatar vipcxj

fix: 刷新浏览器会丢失路由信息

上级 014eddf5
......@@ -5,6 +5,7 @@
<w>arcfour</w>
<w>dropdown</w>
<w>infos</w>
<w>inited</w>
<w>lodash</w>
<w>parsedkey</w>
<w>publics</w>
......
const data = {
init: false,
modules: [],
};
export default data;
export const setModule = (modules) => {
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
......
......@@ -2,7 +2,7 @@ import { routerRedux } from 'dva/router';
import { logout, fetchModuleInfos } from '../../services/main';
import { fetchDomains, switchDomain, currentDomain } from '../../services/domain';
import { cookie, storage } from '../../utils/config';
import { setModule, getModule, getPath, foreachModule } from '../../data/modules';
import { setModules, getModules, getModule, getPath, foreachModule, isInited } from '../../data/modules';
import { setCookie, delCookie, getLocalStorge, setLocalStorge, delLocalStorge, fullPath } from '../../utils/helper';
const createMenus = () => {
......@@ -101,9 +101,11 @@ export default {
yield put({ type: 'queryUserSuccess', payload: user });
},
*fetchModules(action, { put, call }) {
const modules = yield call(fetchModuleInfos);
setModule(modules);
yield put({ type: 'queryMenusSuccess', payload: createMenus(modules) });
if (!isInited()) {
const modules = yield call(fetchModuleInfos);
setModules(modules);
}
yield put({ type: 'queryMenusSuccess', payload: createMenus(getModules()) });
},
*logout(action, { put, call }) {
yield call(logout);
......
......@@ -4,8 +4,9 @@ import { Router } from 'dva/router';
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 { foreachModule, getChildren } from './data/modules';
import { isInited, setModules, foreachModule, getChildren } from './data/modules';
import Monk from './routes/main/monk';
const { contextPath } = config;
......@@ -70,6 +71,19 @@ const createRoute = async (app, module) => {
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 routes = [];
for (const module of modules) {
......@@ -122,15 +136,21 @@ function RouterConfig({ history, app }) {
}, 'main');
},
getChildRoutes: (nextState, cb) => {
const lvl0 = [];
foreachModule((module) => {
if (!module.parent) {
lvl0.push(module);
}
});
createModuleRoutes(app, lvl0)
.then((result) => {
cb(null, result);
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);
......
......@@ -36,8 +36,8 @@ class Main extends React.Component {
});
}
render() {
const children = this.props.children;
const { menus } = this.props.main;
const { main, children } = this.props;
const { menus } = main;
const mode = this.state.mode;
const menuProps = {
mode,
......@@ -74,7 +74,7 @@ class Main extends React.Component {
}
Main.propTypes = {
children: PropTypes.element.isRequired,
children: PropTypes.element,
main: PropTypes.object.isRequired,
};
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论