xxx router

141 阅读1分钟

import React, { Component } from 'react'; import { Route, withRouter } from 'react-router-dom'; import { Redirect} from 'react-router'; // 创建route所需 import Config from "./utils/storage";

class PrivateRoute extends Component { constructor(props) { super(props); this.state = { isAuthenticated:'' } };

UNSAFE_componentWillMount() { const { history } = this.props; let agentToken = window.location.search.split('=')[1] || '' let toUrl = window.location.search.split('=')[2] || '' if(agentToken){ Config.set('USER_AUTHORIZATION', agentToken) window.location.search='' if (toUrl) { setTimeout(() => { history.replace(/${toUrl}); }, 1000) } } let token = Config.get('USER_AUTHORIZATION') if(!token && token !== 'undefined'){ setTimeout(() => { history.replace("/login"); }, 1000) } }; render() { let { component: Component, ...rest } = this.props; return( <Route {...rest} render={(props) => (<Component {...props} /> )} /> )

} }

import React from 'react' import { Switch, Route, Redirect, routerRedux } from 'dva/router' import dynamic from 'dva/dynamic' import App from './page/IndexPage' import { LocaleProvider } from 'antd'; import zhCN from 'antd/lib/locale-provider/zh_CN'; import Config from "./../src/utils/storage"; import AuthorizedRoute from './AuthorizedRoute'; import 'moment/locale/zh-cn';

const { ConnectedRouter } = routerRedux const Routers = function ({ history, app }) { const routes = [ { path: '/home', models: () => [import('./models/homeModel')], component: () => import('./page/home/Home'), }, { path: '/profitinfo', models: () => [import('./models/ad/profitInfoModel')], component: () => import('./page/ad/ProfitInfo'), }, ] // 登录验证 const requireAuth = (nextState, replace) => { // let token = Config.localItem('USER_AUTHORIZATION') console.log('token',"token") // 取其他地方传的token 进行免登录 let agentToken = window.location.search.split('=')[1] || '' console.log(agentToken,"获取token") agentToken = agentToken.replace(/&url/, '') let toUrl = window.location.search.split('=')[2] || '' console.log(toUrl,"toUrl") if (agentToken) { // 设置token 清除浏览器传入的token 避免重复设置 history.pushState(null, null, '?token=') if (toUrl) { replace({ pathname: toUrl, state: { nextPathname: nextState.location.pathname } }); } return Config.localItem('USER_AUTHORIZATION', agentToken) } // if (!token && token !== 'undefined') { // replace({ // pathname: '/login', // state: { nextPathname: nextState.location.pathname } // }); // } } return ( <Route exact path="/" render={() => ()} /> { routes.map(({ path, ...dynamics }, key) => ( <AuthorizedRoute key={key} path={path} component={dynamic({ app, ...dynamics, }) }

            />
            // <Route key={key}

            //   exact
            //   path={path}
            //   component={dynamic({
            //     app,
            //     ...dynamics,
            //   })
            // }
            // />
          ))
        }
          {/* <AuthorizedRoute path="/" component={home} /> */}
      </Switch>
    </App>
  </LocaleProvider>
</ConnectedRouter>

) }

export default Routers