LoginForm.js
6.0 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React, { PropTypes } from 'react';
import { Field, reduxForm, SubmissionError, getFormValues } from 'redux-form';
import InputField from './widget/reduxForm/InputField';
import { Button, Tabs, Card } from 'antd';
import { connect } from 'react-redux';
const TabPane = Tabs.TabPane;
import cx from 'classnames';
import l from './loginFormLess.les';
import { doLogin, sendQuickLoginSmsCode } from '../redux/actions/authed';
import { required, mobile, minValue, maxLength } from './widget/reduxForm/validate';
let i = '';
class LoginForm extends React.Component {
constructor(props) {
super(props);
this.changTab = this.changTab.bind(this);
this.renderAccountPassword = this.renderAccountPassword.bind(this);
this.renderComPanyAccount = this.renderComPanyAccount.bind(this);
this.handleLogin = this.handleLogin.bind(this);
this.myLogin = this.myLogin.bind(this);
this.sendSmsCode = this.sendSmsCode.bind(this);
this.loop = this.loop.bind(this);
this.state = {
clickStatus: false,
countDown: 60
}
}
componentDidMount() {
}
componentWillReceiveProps(nextProps) {
}
componentWillUnmount() {
window.clearInterval(i);
}
changTab(key) { //切换tabs标签
const { dispatch, initialize } = this.props;
if (key == 1) {
dispatch(initialize({
'tabKey': key,
'scope': 'global_access:tenant_admin',
'username': '',
'password': '',
'scopeValue': ' '
}));
} else {
dispatch(initialize({
'tabKey': key,
'scope': 'global_access:tenant_admin',
'username': '',
'password': 'anything',//手机号和验证码登录时,密码随便给个值就行
'scopeValue': '',
'smscode': ''
}));
}
}
handleLogin() { //立即登录
const { dispatch, submit } = this.props;
dispatch(submit());
window.clearInterval(i)
this.setState({
clickStatus: false,
countDown: 60
})
}
myLogin(e) {
const { dispatch, submit } = this.props;
if (e.keyCode == 13) {
dispatch(submit())
}
}
sendSmsCode() {
const { dispatch, formValues, touch, valid, submit } = this.props;
// console.log(formValues.username);
if (formValues.username) {
const params = {
mobile: formValues.username,
type: 'login',
scope: 'global_access:tenant_admin'
}
dispatch(sendQuickLoginSmsCode(params));
this.setState({
clickStatus: true
})
i = setInterval(this.loop, 1000);
} else {
dispatch(submit())
}
}
loop() {
const { countDown } = this.state;
if (countDown == 0) {
this.setState({
clickStatus: false,
countDown: 60
})
window.clearInterval(i);
} else {
let temp = countDown - 1;
this.setState({
countDown: temp
})
}
}
renderAccountPassword() { //账号密码登录
return (
<div className={cx(l.login_tab_content, 'login_input')}>
<InputField type='hidden' name='scope' placeholder='请输入企业账号' onKeyUp={this.myLogin} />
<InputField type='text' name='username' placeholder='请输入手机号' onKeyUp={this.myLogin} />
<InputField type='password' name='password' placeholder='请输入密码' onKeyUp={this.myLogin} />
</div>
)
}
renderComPanyAccount() { //手机验证码登录
const { clickStatus, countDown } = this.state;
return (
<div className={cx(l.login_tab_content, 'login_input')}>
<InputField type='text' name='username' placeholder='请输入手机号' onKeyUp={this.myLogin} />
<div style={{ position: 'relative' }}>
<InputField type='text' name='smscode' placeholder='请输入验证码' onKeyUp={this.myLogin}></InputField>
{
!clickStatus ?
<div className={cx(l.login_verfication_code_active)} onClick={this.sendSmsCode}>获取验证码</div>
:
<div className={cx(l.login_verfication_code_inactive)}>重新发送({countDown})</div>
}
</div>
</div>
)
}
render() {
return (
<div className={cx('login_container')}>
<Card>
<div>
<Tabs className={cx('login_tab')} defaultActiveKey="1" onChange={this.changTab}>
<TabPane tab="账号密码登录" key="1">{this.renderAccountPassword()}</TabPane>
<TabPane tab="快速登陆" key="2">{this.renderComPanyAccount()}</TabPane>
</Tabs>
<InputField type='hidden' name='tabKey' />
<div style={{ color: '#f04134', marginTop: '8px' }}>{this.props.error}</div>
<Button type="primary" onClick={this.handleLogin}>立即登录</Button>
<ul className={cx(l.login_foot)}>
<li>还没有账号?<a href='#/login/register'>免费注册</a></li>
<li><a href="#/login/forget-password">忘记密码</a></li>
</ul>
</div>
</Card>
</div>
)
}
}
const mapState = (state) => {
return {
formValues: getFormValues('login_form')(state),
}
};
const submitForm = (values, dispatch, props) => {
if (values.tabKey == 1) {
values.grant_type = 'password';
} else {
values.grant_type = 'smscode'
}
values.scope = 'global_access:tenant_admin';
return dispatch(doLogin(values));
}
const validate = (values, props) => {
const errors = {};
if (values.tabKey == 1) {
if (required(values.username)) {
errors.username = required(values.username);
} else if (mobile(values.username)) {
errors.username = mobile(values.username);
} else if (required(values.password)) {
errors.password = required(values.sms_code);
} else if (minValue(6)(values.password)) {
errors.password = minValue(6)(values.password);
} else if (maxLength(16)(values.password)) {
errors.password = maxLength(16)(values.password);
}
} else {
if (required(values.username)) {
errors.username = required(values.username);
} else if (mobile(values.username)) {
errors.username = mobile(values.username);
} else if (required(values.sms_code)) {
errors.sms_code = required(values.sms_code);
}
}
return errors;
}
LoginForm = reduxForm({
form: 'login_form',
onSubmit: submitForm,
validate: validate,
initialValues: {
'grant_type': 'password',
'tabKey': 1,
'scope': 'global_access:tenant_admin',
'username': '',
'password': '',
'smscode': ''
}
})(LoginForm);
export default connect(mapState)(LoginForm);