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

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

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