123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /*
- * <<
- * Davinci
- * ==
- * Copyright (C) 2016 - 2017 EDP
- * ==
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * >>
- */
- import React from 'react'
- import Helmet from 'react-helmet'
- import { connect } from 'react-redux'
- import { createStructuredSelector } from 'reselect'
- import { Route, HashRouter as Router, Switch, Redirect, withRouter } from 'react-router-dom'
- import { RouteComponentWithParams } from 'utils/types'
- import { compose } from 'redux'
- import { logged, logout, getServerConfigurations, getUserByToken } from './actions'
- import injectReducer from 'utils/injectReducer'
- import reducer from './reducer'
- import injectSaga from 'utils/injectSaga'
- import saga from './sagas'
- import { makeSelectLogged } from './selectors'
- import checkLogin from 'utils/checkLogin'
- import request, { setToken } from 'utils/request'
- import { statistic } from 'utils/statistic/statistic.dv'
- import FindPassword from 'containers/FindPassword'
- import { Background } from 'containers/Background/Loadable'
- import { Main } from 'containers/Main/Loadable'
- import { Activate } from 'containers/Register/Loadable'
- import api from 'utils/api'
- type MappedStates = ReturnType<typeof mapStateToProps>
- type MappedDispatches = ReturnType<typeof mapDispatchToProps>
- type AppProps = MappedStates & MappedDispatches & RouteComponentWithParams
- export class App extends React.PureComponent<AppProps> {
- constructor(props: AppProps) {
- super(props)
- this.checkTokenLink()
- props.onGetServerConfigurations()
- }
- private getQs = () => {
- const search = this.props.history.location.search
- const qs = search ? search.substr(1) : ''
- if (qs) {
- return qs
- .split('&')
- .reduce((rdc, val) => {
- const pair = val.split('=')
- rdc[pair[0]] = pair[1]
- return rdc
- }, {})
- } else {
- return false
- }
- }
- private checkTokenLink = () => {
- const {
- history,
- onGetLoginUser
- } = this.props
- const qs = this.getQs()
- const token = qs['usertoken']
- // TODO allow take other parameters
- // const dashboard = qs['dashboard']
- // @FIXME login with token from url query
- // if (token) {
- // setToken(token)
- // // onGetLoginUser(() => {
- // history.replace('/projects')
- // // if (dashboard) {
- // // router.replace(`/project/${this.props.params.projectId}/dashboard/${dashboard}`)
- // // } else {
- // // }
- // // })
- // } else {
- this.checkNormalLogin()
- // }
- }
- private checkNormalLogin = () => {
- if (checkLogin()) {
- const token = localStorage.getItem('TOKEN')
- const loginUser = localStorage.getItem('loginUser')
- setToken(token)
- this.props.onLogged(JSON.parse(loginUser))
- statistic.sendPrevDurationRecord()
- } else {
- const qs = this.getQs()
- const ticket = qs['ticket']
- if (ticket) {
- request(api.getUserInfo + `?ticket=${ticket}`, { method: 'get' })
- .then((data) => {
- if (data?.code === 200 && data.data) {
- setToken(data.data?.token)
- const loginUser = data.data.userInfo
- this.props.onLogged(JSON.parse(loginUser))
- statistic.sendPrevDurationRecord()
- }
- })
- } else {
- console.log('onLogout')
- this.props.onLogout()
- }
- }
- }
- private renderRoute = () => {
- const { logged } = this.props
- return (
- // logged ? (
- <Redirect to='/project/1/dataManager' />
- // ) : (
- // <Redirect to='/login' />
- // )
- )
- }
- public render() {
- const { logged } = this.props
- if (typeof logged !== 'boolean') {
- return null
- }
- return (
- <div>
- <Helmet
- titleTemplate='%s - 聚合平台'
- defaultTitle='聚合平台'
- meta={[
- {
- name: 'description',
- content: '聚合平台'
- }
- ]}
- />
- <Router>
- <Switch>
- <Route path='/activate' component={Activate} />
- <Route path='/joinOrganization' exact component={Background} />
- <Route path='/findPassword' component={FindPassword} />
- <Route path='/' exact render={this.renderRoute} />
- <Route path='/' component={logged ? Main : Background} />
- </Switch>
- </Router>
- </div>
- )
- }
- }
- const withReducer = injectReducer({ key: 'global', reducer })
- const withSaga = injectSaga({ key: 'global', saga })
- const mapStateToProps = createStructuredSelector({
- logged: makeSelectLogged()
- })
- const mapDispatchToProps = (dispatch) => ({
- onLogged: (user) => dispatch(logged(user)),
- onLogout: () => dispatch(logout()),
- onGetLoginUser: (token: string) => dispatch(getUserByToken(token)),
- onGetServerConfigurations: () => dispatch(getServerConfigurations())
- })
- const withConnect = connect(
- mapStateToProps,
- mapDispatchToProps
- )
- export default compose(
- withReducer,
- withSaga,
- withConnect,
- withRouter
- )(App)
|