From 29e87b8659825e192b327b7f6bc78b32440c1f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Mon, 15 Jan 2018 16:51:21 +0800 Subject: [PATCH] Change trigger menu address Modify the starting error handling (#675) * Change trigger menu address Modify the starting error handling * add loginout redirect * use select get pathname --- .roadhogrc.mock.js | 9 ++++++ src/common/menu.js | 1 + src/components/GlobalHeader/index.js | 1 + src/error.js | 17 ----------- src/index.js | 6 ++-- src/layouts/BasicLayout.js | 19 ++++++++++-- src/models/error.js | 9 +++++- src/models/login.js | 33 +++++++++++++-------- src/routes/Exception/triggerException.js | 37 ++++++++++++++++-------- src/services/error.js | 4 +++ src/utils/request.js | 22 ++++++++++++++ 11 files changed, 112 insertions(+), 46 deletions(-) delete mode 100644 src/error.js diff --git a/.roadhogrc.mock.js b/.roadhogrc.mock.js index d0d0ccf6..076ee0c6 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 91ae5d41..7093151c 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 f7da5f32..8ce5e66f 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 83d1386d..00000000 --- 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 f909e597..c3ba259d 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 078f84d7..d39abc16 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 818d65bf..c2508f67 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 8e6287cf..044b264d 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 0ed15a9f..e552d4db 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 b1969d82..9697b7ee 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 d5c5200e..291d7cea 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')); + } }); } -- GitLab