| | |
| | | import React, {Component} from 'react' |
| | | import {HashRouter, Switch, Route, Redirect} from 'react-router-dom' |
| | | import asyncComponent from '@/utils/asyncComponent' |
| | | const personal = asyncComponent(() => import('@/views/personal/personal')) |
| | | const main = asyncComponent(() => import('@/views/main')) |
| | | const login = asyncComponent(() => import('@/views/login')) |
| | | const NotFound = asyncComponent(() => import('@/views/404')) |
| | | |
| | | const routers = [ |
| | | {path: '/login', name: 'login', component: login, auth: false}, |
| | | {path: '/main', name: 'main', component: main, auth: true}, |
| | | {path: '/main/:param', name: 'pmain', component: main, auth: true} |
| | | ] |
| | | |
| | | export default class RouteConfig extends Component { |
| | | controlRoute (item, props) { |
| | | if (!item.auth) { // 不需要授权,直接跳转(登录页) |
| | | return (<item.component {...props}/>) |
| | | } |
| | | |
| | | if (item.name === 'pmain') { // 新窗口打开,取url参数放入sessionStorage |
| | | let _param = window.atob(props.match.params.param) |
| | | sessionStorage.setItem('view_param', _param) |
| | | return (<Redirect to={{ pathname: '/main'}}/>) |
| | | } |
| | | |
| | | let userId = sessionStorage.getItem('UserID') |
| | | if (userId) { |
| | | return (<item.component {...props}/>) |
| | | } else { |
| | | let param = sessionStorage.getItem('view_param') |
| | | if (param) { |
| | | param = param.split('&') |
| | | sessionStorage.setItem('UserID', param[3]) |
| | | sessionStorage.setItem('lang', param[4]) |
| | | return (<item.component {...props}/>) |
| | | } else { |
| | | return (<Redirect to={{ pathname: '/login', state: {from: props.location}}}/>) |
| | | } |
| | | } |
| | | } |
| | | |
| | | render () { |
| | | return ( |
| | | <HashRouter> |
| | | <Switch> |
| | | <Route path="/main" exact component={personal}/> |
| | | <Route path="/main/:param" exact component={personal}/> |
| | | { |
| | | routers.map((item, index) => { |
| | | return ( |
| | | <Route key={index} path={item.path} name={item.name} exact render={ props => { |
| | | return this.controlRoute(item, props) |
| | | }}/> |
| | | ) |
| | | }) |
| | | } |
| | | <Redirect exact from="/" to="main"/> |
| | | <Route component= {personal}/> |
| | | <Route component= {NotFound}/> |
| | | </Switch> |
| | | </HashRouter> |
| | | ) |
| | | } |
| | | } |
| | | |
| | | // import React, { lazy } from 'react'; |
| | | // import { Route } from 'react-router-dom'; |
| | | |
| | | // const RouteLis = [ |
| | | // { |
| | | // component: lazy(() => import ('../views/home')), |
| | | // path: '/' |
| | | // } |
| | | // ]; |
| | | |
| | | // const RouterList = () => ( |
| | | // RouteLis.map((item, key) => { |
| | | // return <Route key={key} exact path={item.path} component={item.component}/>; |
| | | // }) |
| | | // ); |
| | | |
| | | // export default RouterList; |
| | | } |