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

重写跳转api,提取getApp到data/app.js.

上级 2b5cba7e
import persistStore from 'redux-persist/lib/persistStore';
import { histories } from '../utils/auth';
const data = {
......@@ -15,3 +16,10 @@ export async function initApp(app) {
export function getApp() {
return data.app;
}
export const getStore = () => {
return data.app._store; // eslint-disable-line no-underscore-dangle
};
// eslint-disable-next-line no-underscore-dangle
export const createPersistor = theApp => persistStore((theApp || getApp())._store);
......@@ -3,7 +3,6 @@ import dva from 'dva';
import createLoading from 'dva-loading';
import moment from 'moment';
import persistReducer from 'redux-persist/lib/persistReducer';
import persistStore from 'redux-persist/lib/persistStore';
import storage from 'redux-persist/lib/storage';
import { getHistory } from './services/route';
import { initApp } from './data/app';
......@@ -48,11 +47,3 @@ app.router(routerConfig);
// 5. Start
initApp(app).then(theApp => theApp.start('#root'));
export default app;
export const getStore = () => {
return app._store;
};
export const createPersistor = theApp => persistStore(theApp._store);
......@@ -7,7 +7,7 @@ import { setToken, setUser, setDomain, histories } from '../utils/auth';
import { switchDomain, currentDomain } from '../services/domain';
import { errors } from '../utils/error';
import config from '../utils/config';
import { getStore } from '../index';
import { getStore } from '../data/app';
const successAuthed = async (tokenId, userName, remember) => {
await histories.pushHistory('userName', null, remember ? userName : '');
......
......@@ -13,7 +13,7 @@ import App from './routes/app';
import { getMenus, getModuleInfo, getModuleLayout } from './data/modules';
import { bindModel } from './utils/model';
import Monk from './routes/main/monk';
import { createPersistor } from './index';
import { createPersistor } from './data/app';
import styles from './index.css';
......
import createBrowserHistory from 'history/createBrowserHistory';
import isString from 'lodash/isString';
import {
push as pushAction,
replace as replaceAction,
go as goAction,
goBack as goBackAction,
goForward as goForwardAction,
} from 'react-router-redux';
import { makePath } from '../utils/helper';
import { getStore } from '../data/app';
const processPath = (base, path, withContext = false) => {
if (isString(path)) {
......@@ -46,68 +54,41 @@ export const createHistory = (...args) => {
};
export const push = (path, state) => {
return history.push(processPath(getHistoryBase(history), path, false), state);
return getStore().dispatch(pushAction(processPath(getHistoryBase(history), path, false), state));
};
export const replace = (path, state) => {
return history.replace(processPath(getHistoryBase(history), path, false), state);
return getStore().dispatch(replaceAction(processPath(getHistoryBase(history), path, false), state));
};
export const go = (n) => {
return history.go(n);
return getStore().dispatch(goAction(n));
};
export const goBack = () => {
return history.goBack();
return getStore().dispatch(goBackAction());
};
export const goForward = () => {
return history.goForward();
return getStore().dispatch(goForwardAction());
};
const checkThis = (theThis) => {
if (!theThis || !theThis.props) {
throw new Error('The this is not a component.');
}
if (!theThis.props.router) {
throw new Error('please use withRouter.');
}
};
const getThisBase = (theThis) => {
if (theThis.props.location) {
return theThis.props.location.pathname;
} else if (typeof window !== 'undefined') {
return window.location.pathname; // eslint-disable-line no-undef
} else {
throw new Error('can not find base path!');
}
};
export const thisPush = (theThis, pathOrLoc) => {
checkThis(theThis);
const route = processPath(getThisBase(theThis), pathOrLoc);
return theThis.props.router.push(route);
export const thisPush = (theThis, pathOrLoc, state) => {
return push(pathOrLoc, state);
};
export const thisReplace = (theThis, pathOrLoc) => {
checkThis(theThis);
const route = processPath(getThisBase(theThis), pathOrLoc);
return theThis.props.router.replace(route);
export const thisReplace = (theThis, pathOrLoc, state) => {
return replace(pathOrLoc, state);
};
export const thisGo = (theThis, n) => {
checkThis(theThis);
return theThis.props.router.go(n);
return go(n);
};
export const thisGoBack = (theThis) => {
checkThis(theThis);
return theThis.props.router.goBack();
export const thisGoBack = () => {
return goBack();
};
export const thisGoForward = (theThis) => {
checkThis(theThis);
return theThis.props.router.goForward();
export const thisGoForward = () => {
return goForward();
};
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论