king
2020-08-28 71a0e75ecb56ae643fe1e86188d45f93f48388c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, {Component} from 'react'
import {HashRouter, Switch, Route, Redirect} from 'react-router-dom'
import md5 from 'md5'
import moment from 'moment'
import options from '@/store/options.js'
import asyncComponent from '@/utils/asyncComponent'
import asyncLoadComponent from '@/utils/asyncLoadComponent'
 
const Pay = asyncLoadComponent(() => import('@/views/pay'))
const Main = asyncLoadComponent(() => import('@/views/main'))
const Login = asyncLoadComponent(() => import('@/views/login'))
const NotFound = asyncComponent(() => import('@/views/404'))
const MobManage = asyncLoadComponent(() => import('@/views/mobmanage'))
const MobDesign = asyncLoadComponent(() => import('@/views/mobdesign'))
const MenuDesign = asyncLoadComponent(() => import('@/views/menudesign'))
const PrintT = asyncLoadComponent(() => import('@/views/printTemplate'))
 
const routers = [
  {path: '/login', name: 'login', component: Login, auth: false},
  {path: '/pay/:param', name: 'pay', component: Pay, auth: false},
  {path: '/print/:param', name: 'print', component: PrintT, auth: false},
  {path: '/ssologin/:param', name: 'ssologin', auth: true},
  {path: '/main', name: 'main', component: Main, auth: true},
  {path: '/mobmanage', name: 'mobmanage', component: MobManage, auth: true},
  {path: '/mobdesign/:appId/:appType/:appCode/:appName', name: 'mobdesign', component: MobDesign, auth: true},
  {path: '/menudesign/:MenuId/:ParentId/:MenuName/:MenuNo', name: 'menudesign', component: MenuDesign, auth: true},
  {path: '/paramsmain/: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)
      let p = {}
      _param.split('&').forEach(cell => {
        let _cell = cell.split('=')
        p[_cell[0]] = _cell[1]
      })
      !sessionStorage.getItem('UserID') && sessionStorage.setItem('UserID', p.ud)
      !sessionStorage.getItem('LoginUID') && sessionStorage.setItem('LoginUID', p.ld)
      !sessionStorage.getItem('User_Name') && sessionStorage.setItem('User_Name', p.un)
 
      if (p.mlogo) {
        window.GLOB.mainlogo = p.mlogo
      }
      if (options.styles[p.mstyle]) {
        document.getElementById('root').className = options.styles[p.mstyle]
      }
      
      sessionStorage.setItem('MainMenu', p.mm)
      sessionStorage.setItem('SubMenu', p.sm)
      sessionStorage.setItem('ThirdMenu', p.tm)
 
      return (<item.component {...props}/>)
    } else if (item.name === 'ssologin') {
      let _param = window.atob(props.match.params.param)
      let p = {}
      _param.split('&').forEach(cell => {
        let _cell = cell.split('=')
        p[_cell[0]] = _cell[1]
      })
      sessionStorage.setItem('UserID', p.ud)
      sessionStorage.setItem('LoginUID', p.ld)
      sessionStorage.setItem('User_Name', p.un)
 
      return (<Redirect to={{ pathname: '/main'}}/>)
    }
    
    let userId = sessionStorage.getItem('UserID') // 判断是否存在userid
 
    let authCode = localStorage.getItem(window.location.href.split('#')[0] + 'AuthCode') // 判断系统是否在授权期限内
    let _s = md5('mksoft' + moment().format('YYYYMMDD'))
    let isauth = authCode && authCode.includes(_s)
 
    if (userId && isauth) {
      return (<item.component {...props}/>)
    } else {
      // return (<Redirect to={{ pathname: '/login', state: {from: props.location}}}/>)
      return (<Redirect to={{ pathname: '/login'}}/>)
    }
  }
 
  render () {
    return (
      <HashRouter>
        <Switch>
          {
            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="login"/>
          <Route component= {NotFound}/>
        </Switch>
      </HashRouter>
    )
  }
}