-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (84 loc) · 2.47 KB
/
index.js
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
import React, { PureComponent } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as userInfoActionsFromOtherFile from '~actions/userinfo';
import Header from '~components/Header';
import LoginComponent from '~components/LoginComponent'
class Login extends PureComponent {
constructor(props) {
super(props);
this.state = {
checking: true
}
};
render() {
return (
<div>
<Header title="登录" />
{
// 等待验证之后,再显示登录信息
this.state.checking
? <div>等待中...</div>
: <LoginComponent loginHandle={this.loginHandle} />
// : ''
}
</div>
)
};
componentDidMount() {
this.doCheck();
};
doCheck = () => {
// 获取redux中的userinfo
const userinfo = this.props.userinfo;
if (userinfo.username) {
// 已经登录
this.goUserPage();
} else {
// 尚未登录
this.setState({
checking: false
})
}
};
// 跳转界面到用户中心页user
goUserPage = () => {
this.props.history.push('/user');
};
// 登录成功之后的业务处理
loginHandle = username => {
// 保存用户名
const actions = this.props.userInfoActions;
// 获取redux中的userinfo
let userinfo = this.props.userinfo;
userinfo.username = username;
actions.update(userinfo);
// 跳转链接
const params = this.props.match.params;
const router = params.router;
console.log(router)
if (router) {
// 如果router存在,就跳回登录前的界面,router就是登录前的界面
console.log(router)
this.props.history.push(router);
} else {
// 不存在的话就到用户中心页
this.goUserPage();
}
};
};
// ==========================redux react 绑定==========================
const mapStateToProps = state => {
return {
userinfo: state.userinfo
};
};
const mapDispatchToProps = dispatch => {
return {
userInfoActions: bindActionCreators(userInfoActionsFromOtherFile, dispatch)
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(Login);