From c9d71c91022aa28314cc7c5d4a07e07b4dd2799c Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Tue, 26 Jun 2018 17:57:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=8F=96=20getQueryPath=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=B0=81=E8=A3=85=E8=B7=AF=E5=BE=84=E5=92=8C=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8F=82=E6=95=B0=E6=8B=BC=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layouts/UserLayout.js | 10 ++++------ src/models/login.js | 8 ++++++-- src/router.js | 5 ++++- src/utils/utils.js | 10 +++++++++- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/layouts/UserLayout.js b/src/layouts/UserLayout.js index 93ca4dc5..bcc45a9a 100644 --- a/src/layouts/UserLayout.js +++ b/src/layouts/UserLayout.js @@ -5,7 +5,7 @@ import { Icon } from 'antd'; import GlobalFooter from '../components/GlobalFooter'; import styles from './UserLayout.less'; import logo from '../assets/logo.svg'; -import { getRoutes, getPageQuery } from '../utils/utils'; +import { getRoutes, getPageQuery, getQueryPath } from '../utils/utils'; const links = [ { @@ -32,13 +32,11 @@ const copyright = ( ); function getLoginPathWithRedirectPath() { - const routePath = '/user/login'; const params = getPageQuery(); const { redirect } = params; - if (redirect) { - return `${routePath}?redirect=${encodeURIComponent(redirect)}`; - } - return routePath; + return getQueryPath('/user/login', { + redirect, + }); } class UserLayout extends React.PureComponent { diff --git a/src/models/login.js b/src/models/login.js index 14bdd76c..9c68dddf 100644 --- a/src/models/login.js +++ b/src/models/login.js @@ -2,7 +2,7 @@ import { routerRedux } from 'dva/router'; import { fakeAccountLogin } from '../services/api'; import { setAuthority } from '../utils/authority'; import { reloadAuthorized } from '../utils/Authorized'; -import { getPageQuery } from '../utils/utils'; +import { getPageQuery, getQueryPath } from '../utils/utils'; export default { namespace: 'login', @@ -49,7 +49,11 @@ export default { }); reloadAuthorized(); yield put( - routerRedux.push(`/user/login?redirect=${encodeURIComponent(window.location.href)}`) + routerRedux.push( + getQueryPath('/user/login', { + redirect: window.location.href, + }) + ) ); }, }, diff --git a/src/router.js b/src/router.js index 144c8267..15e2634a 100644 --- a/src/router.js +++ b/src/router.js @@ -5,6 +5,7 @@ import zhCN from 'antd/lib/locale-provider/zh_CN'; import dynamic from 'dva/dynamic'; import { getRouterData } from './common/router'; import Authorized from './utils/Authorized'; +import { getQueryPath } from './utils/utils'; import styles from './index.less'; const { ConnectedRouter } = routerRedux; @@ -26,7 +27,9 @@ function RouterConfig({ history, app }) { path="/" render={props => } authority={['admin', 'user']} - redirectPath={`/user/login?redirect=${encodeURIComponent(window.location.href)}`} + redirectPath={getQueryPath('/user/login', { + redirect: window.location.href, + })} /> diff --git a/src/utils/utils.js b/src/utils/utils.js index ab4acb17..84608aa2 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -1,5 +1,5 @@ import moment from 'moment'; -import { parse } from 'qs'; +import { parse, stringify } from 'qs'; export function fixedZero(val) { return val * 1 < 10 ? `0${val}` : val; @@ -166,6 +166,14 @@ export function getPageQuery() { return parse(window.location.href.split('?')[1]); } +export function getQueryPath(path = '', query = {}) { + const search = stringify(query); + if (search.length) { + return `${path}?${search}`; + } + return path; +} + /* eslint no-useless-escape:0 */ const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/; -- GitLab