index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React from 'react'
  21. import Helmet from 'react-helmet'
  22. import { connect } from 'react-redux'
  23. import { createStructuredSelector } from 'reselect'
  24. import { Route, HashRouter as Router, Switch, Redirect } from 'react-router-dom'
  25. import { RouteComponentWithParams } from 'utils/types'
  26. import { compose } from 'redux'
  27. import { logged, logout, getServerConfigurations, getUserByToken } from './actions'
  28. import injectReducer from 'utils/injectReducer'
  29. import reducer from './reducer'
  30. import injectSaga from 'utils/injectSaga'
  31. import saga from './sagas'
  32. import { makeSelectLogged } from './selectors'
  33. import checkLogin from 'utils/checkLogin'
  34. import { setToken } from 'utils/request'
  35. import { statistic } from 'utils/statistic/statistic.dv'
  36. import FindPassword from 'containers/FindPassword'
  37. import { Background } from 'containers/Background/Loadable'
  38. import { Main } from 'containers/Main/Loadable'
  39. import { Activate } from 'containers/Register/Loadable'
  40. type MappedStates = ReturnType<typeof mapStateToProps>
  41. type MappedDispatches = ReturnType<typeof mapDispatchToProps>
  42. type AppProps = MappedStates & MappedDispatches & RouteComponentWithParams
  43. export class App extends React.PureComponent<AppProps> {
  44. constructor(props: AppProps) {
  45. super(props)
  46. props.onGetServerConfigurations()
  47. this.checkTokenLink()
  48. }
  49. private getQs = () => {
  50. const search = location.search
  51. const qs = search ? search.substr(1) : ''
  52. if (qs) {
  53. return qs
  54. .split('&')
  55. .reduce((rdc, val) => {
  56. const pair = val.split('=')
  57. rdc[pair[0]] = pair[1]
  58. return rdc
  59. }, {})
  60. } else {
  61. return false
  62. }
  63. }
  64. private checkTokenLink = () => {
  65. const {
  66. history,
  67. onGetLoginUser
  68. } = this.props
  69. const qs = this.getQs()
  70. const token = qs['usertoken']
  71. // TODO allow take other parameters
  72. // const dashboard = qs['dashboard']
  73. // @FIXME login with token from url query
  74. // if (token) {
  75. // setToken(token)
  76. // // onGetLoginUser(() => {
  77. // history.replace('/projects')
  78. // // if (dashboard) {
  79. // // router.replace(`/project/${this.props.params.projectId}/dashboard/${dashboard}`)
  80. // // } else {
  81. // // }
  82. // // })
  83. // } else {
  84. this.checkNormalLogin()
  85. // }
  86. }
  87. private checkNormalLogin = () => {
  88. if (checkLogin()) {
  89. const token = localStorage.getItem('TOKEN')
  90. const loginUser = localStorage.getItem('loginUser')
  91. setToken(token)
  92. this.props.onLogged(JSON.parse(loginUser))
  93. statistic.sendPrevDurationRecord()
  94. } else {
  95. this.props.onLogout()
  96. }
  97. }
  98. private renderRoute = () => {
  99. const { logged } = this.props
  100. return (
  101. logged ? (
  102. <Redirect to='/project/1/dataManager' />
  103. ) : (
  104. <Redirect to='/login' />
  105. )
  106. )
  107. }
  108. public render() {
  109. const { logged } = this.props
  110. if (typeof logged !== 'boolean') {
  111. return null
  112. }
  113. return (
  114. <div>
  115. <Helmet
  116. titleTemplate='%s - 聚合平台'
  117. defaultTitle='聚合平台'
  118. meta={[
  119. {
  120. name: 'description',
  121. content: '聚合平台'
  122. }
  123. ]}
  124. />
  125. <Router>
  126. <Switch>
  127. <Route path='/activate' component={Activate} />
  128. <Route path='/joinOrganization' exact component={Background} />
  129. <Route path='/findPassword' component={FindPassword} />
  130. <Route path='/' exact render={this.renderRoute} />
  131. <Route path='/' component={logged ? Main : Background} />
  132. </Switch>
  133. </Router>
  134. </div>
  135. )
  136. }
  137. }
  138. const withReducer = injectReducer({ key: 'global', reducer })
  139. const withSaga = injectSaga({ key: 'global', saga })
  140. const mapStateToProps = createStructuredSelector({
  141. logged: makeSelectLogged()
  142. })
  143. const mapDispatchToProps = (dispatch) => ({
  144. onLogged: (user) => dispatch(logged(user)),
  145. onLogout: () => dispatch(logout()),
  146. onGetLoginUser: (token: string) => dispatch(getUserByToken(token)),
  147. onGetServerConfigurations: () => dispatch(getServerConfigurations())
  148. })
  149. const withConnect = connect(
  150. mapStateToProps,
  151. mapDispatchToProps
  152. )
  153. export default compose(
  154. withReducer,
  155. withSaga,
  156. withConnect
  157. )(App)