index.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, withRouter } 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 request, { 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. import api from 'utils/api'
  41. type MappedStates = ReturnType<typeof mapStateToProps>
  42. type MappedDispatches = ReturnType<typeof mapDispatchToProps>
  43. type AppProps = MappedStates & MappedDispatches & RouteComponentWithParams
  44. export class App extends React.PureComponent<AppProps> {
  45. constructor(props: AppProps) {
  46. super(props)
  47. this.checkTokenLink()
  48. props.onGetServerConfigurations()
  49. }
  50. private getQs = () => {
  51. const search = this.props.history.location.search
  52. const qs = search ? search.substr(1) : ''
  53. if (qs) {
  54. return qs
  55. .split('&')
  56. .reduce((rdc, val) => {
  57. const pair = val.split('=')
  58. rdc[pair[0]] = pair[1]
  59. return rdc
  60. }, {})
  61. } else {
  62. return false
  63. }
  64. }
  65. private checkTokenLink = () => {
  66. const {
  67. history,
  68. onGetLoginUser
  69. } = this.props
  70. const qs = this.getQs()
  71. const token = qs['usertoken']
  72. // TODO allow take other parameters
  73. // const dashboard = qs['dashboard']
  74. // @FIXME login with token from url query
  75. // if (token) {
  76. // setToken(token)
  77. // // onGetLoginUser(() => {
  78. // history.replace('/projects')
  79. // // if (dashboard) {
  80. // // router.replace(`/project/${this.props.params.projectId}/dashboard/${dashboard}`)
  81. // // } else {
  82. // // }
  83. // // })
  84. // } else {
  85. this.checkNormalLogin()
  86. // }
  87. }
  88. private checkNormalLogin = () => {
  89. if (checkLogin()) {
  90. const token = localStorage.getItem('TOKEN')
  91. const loginUser = localStorage.getItem('loginUser')
  92. setToken(token)
  93. this.props.onLogged(JSON.parse(loginUser))
  94. statistic.sendPrevDurationRecord()
  95. } else {
  96. const qs = this.getQs()
  97. const ticket = qs['ticket']
  98. if (ticket) {
  99. request(api.getUserInfo + `?ticket=${ticket}`, { method: 'get' })
  100. .then((data) => {
  101. if (data?.code === 200 && data.data) {
  102. setToken(data.data?.token)
  103. const loginUser = data.data.userInfo
  104. this.props.onLogged(JSON.parse(loginUser))
  105. statistic.sendPrevDurationRecord()
  106. }
  107. })
  108. } else {
  109. console.log('onLogout')
  110. this.props.onLogout()
  111. }
  112. }
  113. }
  114. private renderRoute = () => {
  115. const { logged } = this.props
  116. return (
  117. // logged ? (
  118. <Redirect to='/project/1/dataManager' />
  119. // ) : (
  120. // <Redirect to='/login' />
  121. // )
  122. )
  123. }
  124. public render() {
  125. const { logged } = this.props
  126. if (typeof logged !== 'boolean') {
  127. return null
  128. }
  129. return (
  130. <div>
  131. <Helmet
  132. titleTemplate='%s - 聚合平台'
  133. defaultTitle='聚合平台'
  134. meta={[
  135. {
  136. name: 'description',
  137. content: '聚合平台'
  138. }
  139. ]}
  140. />
  141. <Router>
  142. <Switch>
  143. <Route path='/activate' component={Activate} />
  144. <Route path='/joinOrganization' exact component={Background} />
  145. <Route path='/findPassword' component={FindPassword} />
  146. <Route path='/' exact render={this.renderRoute} />
  147. <Route path='/' component={logged ? Main : Background} />
  148. </Switch>
  149. </Router>
  150. </div>
  151. )
  152. }
  153. }
  154. const withReducer = injectReducer({ key: 'global', reducer })
  155. const withSaga = injectSaga({ key: 'global', saga })
  156. const mapStateToProps = createStructuredSelector({
  157. logged: makeSelectLogged()
  158. })
  159. const mapDispatchToProps = (dispatch) => ({
  160. onLogged: (user) => dispatch(logged(user)),
  161. onLogout: () => dispatch(logout()),
  162. onGetLoginUser: (token: string) => dispatch(getUserByToken(token)),
  163. onGetServerConfigurations: () => dispatch(getServerConfigurations())
  164. })
  165. const withConnect = connect(
  166. mapStateToProps,
  167. mapDispatchToProps
  168. )
  169. export default compose(
  170. withReducer,
  171. withSaga,
  172. withConnect,
  173. withRouter
  174. )(App)