diff --git a/.roadhogrc.mock.js b/.roadhogrc.mock.js
index d0d0ccf6d1f9b218adeabb3dc7c14f5a79b8de87..076ee0c687b3686e7b3fb2e7f38e311c208482af 100644
--- a/.roadhogrc.mock.js
+++ b/.roadhogrc.mock.js
@@ -123,6 +123,15 @@ const proxy = {
"path": "/base/category/list"
});
},
+ 'GET /api/401': (req, res) => {
+ res.status(401).send({
+ "timestamp": 1513932555104,
+ "status": 401,
+ "error": "Unauthorized",
+ "message": "Unauthorized",
+ "path": "/base/category/list"
+ });
+ },
};
export default noProxy ? {} : delay(proxy, 1000);
diff --git a/src/common/menu.js b/src/common/menu.js
index 91ae5d41464bb182b3924319221839798a6910d8..7093151ce363bbe7e8539fb2f86e3f9d3e1f0dc3 100644
--- a/src/common/menu.js
+++ b/src/common/menu.js
@@ -94,6 +94,7 @@ const menuData = [{
}, {
name: '触发异常',
path: 'trigger',
+ hideInMenu: true,
}],
}, {
name: '账户',
diff --git a/src/components/GlobalHeader/index.js b/src/components/GlobalHeader/index.js
index f7da5f32ba4de8917d948f0cbc52d5dec12ef85d..8ce5e66ffda0e2a920bc47f725c82189f88d66bb 100644
--- a/src/components/GlobalHeader/index.js
+++ b/src/components/GlobalHeader/index.js
@@ -61,6 +61,7 @@ export default class GlobalHeader extends PureComponent {
diff --git a/src/error.js b/src/error.js
deleted file mode 100644
index 83d1386d5c5a574251c39ac78edc60edbdbd2d09..0000000000000000000000000000000000000000
--- a/src/error.js
+++ /dev/null
@@ -1,17 +0,0 @@
-import { routerRedux } from 'dva/router';
-
-const error = (e, dispatch) => {
- if (e.name === 401 || e.name === 403) {
- dispatch(routerRedux.push('/exception/403'));
- return;
- }
- if (e.name <= 504 && e.name >= 500) {
- dispatch(routerRedux.push('/exception/500'));
- return;
- }
- if (e.name >= 404 && e.name < 422) {
- dispatch(routerRedux.push('/exception/404'));
- }
-};
-
-export default error;
diff --git a/src/index.js b/src/index.js
index f909e5977c54bf77907328a5e6ae9756e495a414..c3ba259d1b20698bb501a89406a81c082d74222d 100644
--- a/src/index.js
+++ b/src/index.js
@@ -8,13 +8,11 @@ import createLoading from 'dva-loading';
import 'moment/locale/zh-cn';
import FastClick from 'fastclick';
import './rollbar';
-import onError from './error';
import './index.less';
// 1. Initialize
const app = dva({
history: createHistory(),
- onError,
});
// 2. Plugins
@@ -28,4 +26,8 @@ app.router(require('./router').default);
// 5. Start
app.start('#root');
+
+
FastClick.attach(document.body);
+
+export default app._store; // eslint-disable-line
diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js
index 078f84d77e5e53f8e01077f03728725b56b1dff8..d39abc16de623da643b8de4d152c3c2101755f68 100644
--- a/src/layouts/BasicLayout.js
+++ b/src/layouts/BasicLayout.js
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { Layout, Icon, message } from 'antd';
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
-import { Route, Redirect, Switch } from 'dva/router';
+import { Route, Redirect, Switch, routerRedux } from 'dva/router';
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
import { enquireScreen } from 'enquire-js';
@@ -98,6 +98,16 @@ class BasicLayout extends React.PureComponent {
}
return title;
}
+ getBashRedirect = () => {
+ // According to the url parameter to redirect
+ // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
+ const urlParams = new URL(window.location.href);
+ const redirect = urlParams.searchParams.get('redirect') || '/dashboard/analysis';
+ // Remove the parameters in the url
+ urlParams.searchParams.delete('redirect');
+ window.history.pushState(null, 'redirect', urlParams.href);
+ return redirect;
+ }
handleMenuCollapse = (collapsed) => {
this.props.dispatch({
type: 'global/changeLayoutCollapsed',
@@ -112,6 +122,10 @@ class BasicLayout extends React.PureComponent {
});
}
handleMenuClick = ({ key }) => {
+ if (key === 'triggerError') {
+ this.props.dispatch(routerRedux.push('/exception/trigger'));
+ return;
+ }
if (key === 'logout') {
this.props.dispatch({
type: 'login/logout',
@@ -129,6 +143,7 @@ class BasicLayout extends React.PureComponent {
const {
currentUser, collapsed, fetchingNotices, notices, routerData, match, location,
} = this.props;
+ const bashRedirect = this.getBashRedirect();
const layout = (
)
}
-
+
diff --git a/src/models/error.js b/src/models/error.js
index 818d65bfcdf20bcf31666537a510846662a2f28a..c2508f674d435728a8c7a3eb54ac5aba2b2f1c41 100644
--- a/src/models/error.js
+++ b/src/models/error.js
@@ -1,4 +1,4 @@
-import { query403, query404, query500 } from '../services/error';
+import { query403, query401, query404, query500 } from '../services/error';
export default {
namespace: 'error',
@@ -16,6 +16,13 @@ export default {
payload: '403',
});
},
+ *query401(_, { call, put }) {
+ yield call(query401);
+ yield put({
+ type: 'trigger',
+ payload: '401',
+ });
+ },
*query500(_, { call, put }) {
yield call(query500);
yield put({
diff --git a/src/models/login.js b/src/models/login.js
index 8e6287cf7cf55b4fcfb403b6dc7871488fe6ab6e..044b264d576894f18c38f592edc246234268cf9b 100644
--- a/src/models/login.js
+++ b/src/models/login.js
@@ -24,18 +24,27 @@ export default {
window.location.reload();
}
},
- *logout(_, { put }) {
- yield put({
- type: 'changeLoginStatus',
- payload: {
- status: false,
- currentAuthority: 'guest',
- },
- });
- // yield put(routerRedux.push('/user/login'));
- // Login out after permission changes to admin or user
- // The refresh will automatically redirect to the login page
- window.location.reload();
+ *logout(_, { put, select }) {
+ try {
+ // get location pathname
+ const urlParams = new URL(window.location.href);
+ const pathname = yield select(state => state.routing.location.pathname);
+ // add the parameters in the url
+ urlParams.searchParams.set('redirect', pathname);
+ window.history.pushState(null, 'login', urlParams.href);
+ } finally {
+ // yield put(routerRedux.push('/user/login'));
+ // Login out after permission changes to admin or user
+ // The refresh will automatically redirect to the login page
+ yield put({
+ type: 'changeLoginStatus',
+ payload: {
+ status: false,
+ currentAuthority: 'guest',
+ },
+ });
+ window.location.reload();
+ }
},
},
diff --git a/src/routes/Exception/triggerException.js b/src/routes/Exception/triggerException.js
index 0ed15a9fb1d3ef32d5c441d4dcf1f2b9fccc8b6c..e552d4db02038664d89e1403e18fe9ecf0550ee8 100644
--- a/src/routes/Exception/triggerException.js
+++ b/src/routes/Exception/triggerException.js
@@ -1,5 +1,5 @@
import React, { PureComponent } from 'react';
-import { Button, Spin } from 'antd';
+import { Button, Spin, Card } from 'antd';
import { connect } from 'dva';
import styles from './style.less';
@@ -10,6 +10,14 @@ export default class TriggerException extends PureComponent {
state={
isloading: false,
}
+ trigger401 = () => {
+ this.setState({
+ isloading: true,
+ });
+ this.props.dispatch({
+ type: 'error/query401',
+ });
+ };
trigger403 = () => {
this.setState({
isloading: true,
@@ -36,17 +44,22 @@ export default class TriggerException extends PureComponent {
};
render() {
return (
-
-
-
-
-
+
+
+
+
+
+
+
+
);
}
}
diff --git a/src/services/error.js b/src/services/error.js
index b1969d82559909d8f71d2c1cb90654ce6bc3fa2b..9697b7eed2c49f977a0dcc327d995cbb14cf9ea1 100644
--- a/src/services/error.js
+++ b/src/services/error.js
@@ -4,6 +4,10 @@ export async function query404() {
return request('/api/404');
}
+export async function query401() {
+ return request('/api/401');
+}
+
export async function query403() {
return request('/api/403');
}
diff --git a/src/utils/request.js b/src/utils/request.js
index d5c5200e10f2e86596f9479ed3769a2e8017c6e2..291d7ceaaad037dda9c096af107272bd1a6c0a37 100644
--- a/src/utils/request.js
+++ b/src/utils/request.js
@@ -1,5 +1,7 @@
import fetch from 'dva/fetch';
import { notification } from 'antd';
+import { routerRedux } from 'dva/router';
+import store from '../index';
const codeMessage = {
200: '服务器成功返回请求的数据',
@@ -61,5 +63,25 @@ export default function request(url, options) {
return response.text();
}
return response.json();
+ })
+ .catch((e) => {
+ const { dispatch } = store;
+ if (e.name === 401) {
+ dispatch({
+ type: 'login/logout',
+ });
+ return;
+ }
+ if (e.name === 403) {
+ dispatch(routerRedux.push('/exception/403'));
+ return;
+ }
+ if (e.name <= 504 && e.name >= 500) {
+ dispatch(routerRedux.push('/exception/500'));
+ return;
+ }
+ if (e.name >= 404 && e.name < 422) {
+ dispatch(routerRedux.push('/exception/404'));
+ }
});
}