2017-07-10 19:50:11 +08:00
|
|
|
import React, { Component } from 'react'
|
2017-07-20 19:59:49 +08:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { Route, HashRouter, Redirect, Switch } from 'react-router-dom'
|
2017-07-19 16:24:37 +08:00
|
|
|
import { Home, ProjectGroups, Interface, News } from './containers/index'
|
|
|
|
import User from './containers/User/User.js'
|
|
|
|
import Header from './components/Header/Header'
|
2017-07-20 19:59:49 +08:00
|
|
|
import { checkLoginState } from './actions/login'
|
|
|
|
|
2017-07-19 16:24:37 +08:00
|
|
|
|
|
|
|
const LOADING_STATUS = 0;
|
|
|
|
const GUEST_STATUS = 1;
|
|
|
|
const MEMBER_STATUS = 2;
|
2017-07-10 19:50:11 +08:00
|
|
|
|
|
|
|
class App extends Component {
|
2017-07-19 16:24:37 +08:00
|
|
|
|
|
|
|
constructor(props) {
|
2017-07-20 19:59:49 +08:00
|
|
|
super(props);
|
2017-07-19 16:24:37 +08:00
|
|
|
this.state = {
|
|
|
|
login: LOADING_STATUS
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 19:59:49 +08:00
|
|
|
static propTypes = {
|
|
|
|
checkLoginState:PropTypes.func
|
|
|
|
}
|
2017-07-19 16:24:37 +08:00
|
|
|
route = (status) => {
|
|
|
|
let r;
|
|
|
|
if (status === LOADING_STATUS) {
|
|
|
|
return <span>loading...</span>
|
|
|
|
} else if (status === GUEST_STATUS) {
|
|
|
|
r = (
|
|
|
|
<HashRouter>
|
|
|
|
<div className="router-main">
|
|
|
|
<Header />
|
|
|
|
<Route path="/" component={Home} exact />
|
2017-07-20 19:59:49 +08:00
|
|
|
<Switch>
|
|
|
|
<Route
|
|
|
|
path="/"
|
|
|
|
component={Home}/>
|
|
|
|
<Redirect from="(/:str)" to="/" />
|
|
|
|
</Switch>
|
2017-07-19 16:24:37 +08:00
|
|
|
</div>
|
|
|
|
</HashRouter>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
r = (
|
|
|
|
<HashRouter>
|
|
|
|
<div className="router-main">
|
|
|
|
<Header />
|
2017-07-20 19:59:49 +08:00
|
|
|
<Route path="/" component={Home} exact />
|
2017-07-19 16:24:37 +08:00
|
|
|
<Route path="/ProjectGroups" component={ProjectGroups} />
|
|
|
|
<Route path="/Interface" component={Interface} />
|
|
|
|
<Route path="/user" component={User} />
|
|
|
|
<Route path="/News" component={News} />
|
|
|
|
</div>
|
|
|
|
</HashRouter>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-07-20 19:59:49 +08:00
|
|
|
this.props.checkLoginState().then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
if (res.payload.data.errcode === 0 && res.payload.data.data._id > 0) {
|
2017-07-19 16:24:37 +08:00
|
|
|
this.setState({
|
|
|
|
login: MEMBER_STATUS
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
login: GUEST_STATUS
|
|
|
|
})
|
|
|
|
}
|
2017-07-20 19:59:49 +08:00
|
|
|
}).catch((err) => {
|
2017-07-19 16:24:37 +08:00
|
|
|
this.setState({
|
|
|
|
login: GUEST_STATUS
|
2017-07-20 19:59:49 +08:00
|
|
|
});
|
|
|
|
console.log(err)
|
|
|
|
});
|
2017-07-19 16:24:37 +08:00
|
|
|
}
|
|
|
|
|
2017-07-10 19:50:11 +08:00
|
|
|
render() {
|
2017-07-19 16:24:37 +08:00
|
|
|
return this.route(this.state.login)
|
2017-07-10 19:50:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:59:49 +08:00
|
|
|
export default connect(
|
|
|
|
state => {
|
|
|
|
return{
|
|
|
|
login:state.login.isLogin
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
checkLoginState
|
|
|
|
}
|
|
|
|
)(App)
|