Browse Source

将privateRouter抽离成为单独的组件文件(解决打包报错的问题)

zhuth 7 years ago
parent
commit
8dc1e3984e
2 changed files with 31 additions and 26 deletions
  1. 29 0
      src/routes/privateRoute.jsx
  2. 2 26
      src/routes/router.js

+ 29 - 0
src/routes/privateRoute.jsx

@@ -0,0 +1,29 @@
+import { Route } from 'dva/router'
+import RootLayout from '../components/common/rootLayout'
+import * as moment from 'moment'
+
+export default ({ component: Component, ...rest }) => (
+    <Route
+        {...rest}
+        render={props =>{
+
+            if (!window.localStorage.getItem('token')) {
+                window.localStorage.setItem("token", "false");
+            }else {
+                /**
+                 * 是否过期
+                 * 1.是-----设置isAuthenticated为false
+                 */
+                const t = moment(+window.localStorage.getItem('expireTime'));
+                if(t.isValid() && t.diff(moment()) < 0) {
+                    window.localStorage.setItem("token", "false");
+                }
+            }
+
+            const isAuthenticated = window.localStorage.getItem('token') !== "false";
+            return <RootLayout location={props.location} isAuthenticated={isAuthenticated}>
+                <Component isAuthenticated={isAuthenticated} {...props} />
+            </RootLayout>
+        }}
+    />
+);

+ 2 - 26
src/routes/router.js

@@ -1,7 +1,7 @@
 import React from 'react'
 import { LocaleProvider } from 'antd'
 import { Router, Route, Switch } from 'dva/router'
-import RootLayout from '../components/common/rootLayout'
+import PrivateRoute from './privateRoute'
 import Login from '../components/common/login/login'
 import Register from '../components/common/login/register'
 import MainLayout from './mainLayout'
@@ -11,7 +11,7 @@ import DashboardDesigner from '../components/dashboardDesigner/layout'
 import zhCN from 'antd/lib/locale-provider/zh_CN'
 import Demo from '../demo';
 import Xiaomi from '../xiaomi'
-import * as moment from 'moment'
+
 
 function RouterConfig({ history }) {
     return (
@@ -33,28 +33,4 @@ function RouterConfig({ history }) {
 
 export default RouterConfig;
 
-const PrivateRoute = ({ component: Component, ...rest }) => (
-    <Route
-        {...rest}
-        render={props =>{
-
-            if (!window.localStorage.getItem('token')) {
-                window.localStorage.setItem("token", "false");
-            }else {
-                /**
-                 * 是否过期
-                 * 1.是-----设置isAuthenticated为false
-                 */
-                const t = moment(+window.localStorage.getItem('expireTime'));
-                if(t.isValid() && t.diff(moment()) < 0) {
-                    window.localStorage.setItem("token", "false");
-                }
-            }
 
-            const isAuthenticated = window.localStorage.getItem('token') !== "false";
-            return <RootLayout location={props.location} isAuthenticated={isAuthenticated}>
-                <Component isAuthenticated={isAuthenticated} {...props} />
-            </RootLayout>
-        }}
-    />
-);