Quellcode durchsuchen

兼容hash 路由模式

yoyo837 vor 7 Jahren
Ursprung
Commit
63c8ceb200
3 geänderte Dateien mit 14 neuen und 4 gelöschten Zeilen
  1. 3 3
      src/layouts/UserLayout.js
  2. 6 1
      src/models/login.js
  3. 5 0
      src/utils/utils.js

+ 3 - 3
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 } from '../utils/utils';
+import { getRoutes, getPageQuery } from '../utils/utils';
 
 const links = [
   {
@@ -33,8 +33,8 @@ const copyright = (
 
 function getLoginPathWithRedirectPath() {
   const routePath = '/user/login';
-  const urlParams = new URL(window.location.href);
-  const redirect = urlParams.searchParams.get('redirect');
+  const params = getPageQuery();
+  const { redirect } = params;
   if (redirect) {
     return `${routePath}?redirect=${encodeURIComponent(redirect)}`;
   }

+ 6 - 1
src/models/login.js

@@ -2,6 +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';
 
 export default {
   namespace: 'login',
@@ -21,11 +22,15 @@ export default {
       if (response.status === 'ok') {
         reloadAuthorized();
         const urlParams = new URL(window.location.href);
-        let redirect = urlParams.searchParams.get('redirect');
+        const params = getPageQuery();
+        let { redirect } = params;
         if (redirect) {
           const redirectUrlParams = new URL(redirect);
           if (redirectUrlParams.origin === urlParams.origin) {
             redirect = redirect.substr(urlParams.origin.length);
+            if (redirect.startsWith('/#')) {
+              redirect = redirect.substr(2);
+            }
           } else {
             window.location.href = redirect;
             return;

+ 5 - 0
src/utils/utils.js

@@ -1,4 +1,5 @@
 import moment from 'moment';
+import { parse } from 'qs';
 
 export function fixedZero(val) {
   return val * 1 < 10 ? `0${val}` : val;
@@ -161,6 +162,10 @@ export function getRoutes(path, routerData) {
   return renderRoutes;
 }
 
+export function getPageQuery() {
+  return parse(window.location.href.split('?')[1]);
+}
+
 /* eslint no-useless-escape:0 */
 const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;