index.tsx
6.64 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
import { Alert, Checkbox, Icon } from 'antd';
import { FormattedMessage, formatMessage } from 'umi-plugin-react/locale';
import React, { Component } from 'react';
import { CheckboxChangeEvent } from 'antd/es/checkbox';
import { Dispatch, AnyAction } from 'redux';
import { FormComponentProps } from 'antd/es/form';
import Link from 'umi/link';
import { connect } from 'dva';
import { StateType } from '@/models/login';
import LoginComponents from './components/Login';
import styles from './style.less';
import { LoginParamsType } from '@/services/login';
import { ConnectState } from '@/models/connect';
const { Tab, UserName, Password, Mobile, Captcha, Submit } = LoginComponents;
interface LoginProps {
dispatch: Dispatch<AnyAction>;
userLogin: StateType;
submitting: boolean;
}
interface LoginState {
type: string;
autoLogin: boolean;
}
@connect(({ login, loading }: ConnectState) => ({
userLogin: login,
submitting: loading.effects['login/login'],
}))
class Login extends Component<LoginProps, LoginState> {
loginForm: FormComponentProps['form'] | undefined | null = undefined;
state: LoginState = {
type: 'account',
autoLogin: true,
};
changeAutoLogin = (e: CheckboxChangeEvent) => {
this.setState({
autoLogin: e.target.checked,
});
};
handleSubmit = (err: unknown, values: LoginParamsType) => {
const { type } = this.state;
if (!err) {
const { dispatch } = this.props;
dispatch({
type: 'login/login',
payload: {
...values,
type,
},
});
}
};
onTabChange = (type: string) => {
this.setState({ type });
};
onGetCaptcha = () =>
new Promise<boolean>((resolve, reject) => {
if (!this.loginForm) {
return;
}
this.loginForm.validateFields(
['mobile'],
{},
async (err: unknown, values: LoginParamsType) => {
if (err) {
reject(err);
} else {
const { dispatch } = this.props;
try {
const success = await ((dispatch({
type: 'login/getCaptcha',
payload: values.mobile,
}) as unknown) as Promise<unknown>);
resolve(!!success);
} catch (error) {
reject(error);
}
}
},
);
});
renderMessage = (content: string) => (
<Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
);
render() {
const { userLogin, submitting } = this.props;
const { status, type: loginType } = userLogin;
const { type, autoLogin } = this.state;
return (
<div className={styles.main}>
<LoginComponents
defaultActiveKey={type}
onTabChange={this.onTabChange}
onSubmit={this.handleSubmit}
onCreate={(form?: FormComponentProps['form']) => {
this.loginForm = form;
}}
>
<Tab key="account" tab={formatMessage({ id: 'user-login.login.tab-login-credentials' })}>
{status === 'error' &&
loginType === 'account' &&
!submitting &&
this.renderMessage(
formatMessage({ id: 'user-login.login.message-invalid-credentials' }),
)}
<UserName
name="userName"
placeholder={`${formatMessage({ id: 'user-login.login.userName' })}: admin or user`}
rules={[
{
required: true,
message: formatMessage({ id: 'user-login.userName.required' }),
},
]}
/>
<Password
name="password"
placeholder={`${formatMessage({ id: 'user-login.login.password' })}: ant.design`}
rules={[
{
required: true,
message: formatMessage({ id: 'user-login.password.required' }),
},
]}
onPressEnter={e => {
e.preventDefault();
if (this.loginForm) {
this.loginForm.validateFields(this.handleSubmit);
}
}}
/>
</Tab>
<Tab key="mobile" tab={formatMessage({ id: 'user-login.login.tab-login-mobile' })}>
{status === 'error' &&
loginType === 'mobile' &&
!submitting &&
this.renderMessage(
formatMessage({ id: 'user-login.login.message-invalid-verification-code' }),
)}
<Mobile
name="mobile"
placeholder={formatMessage({ id: 'user-login.phone-number.placeholder' })}
rules={[
{
required: true,
message: formatMessage({ id: 'user-login.phone-number.required' }),
},
{
pattern: /^1\d{10}$/,
message: formatMessage({ id: 'user-login.phone-number.wrong-format' }),
},
]}
/>
<Captcha
name="captcha"
placeholder={formatMessage({ id: 'user-login.verification-code.placeholder' })}
countDown={120}
onGetCaptcha={this.onGetCaptcha}
getCaptchaButtonText={formatMessage({ id: 'user-login.form.get-captcha' })}
getCaptchaSecondText={formatMessage({ id: 'user-login.captcha.second' })}
rules={[
{
required: true,
message: formatMessage({ id: 'user-login.verification-code.required' }),
},
]}
/>
</Tab>
<div>
<Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
<FormattedMessage id="user-login.login.remember-me" />
</Checkbox>
<a style={{ float: 'right' }} href="">
<FormattedMessage id="user-login.login.forgot-password" />
</a>
</div>
<Submit loading={submitting}>
<FormattedMessage id="user-login.login.login" />
</Submit>
<div className={styles.other}>
<FormattedMessage id="user-login.login.sign-in-with" />
<Icon type="alipay-circle" className={styles.icon} theme="outlined" />
<Icon type="taobao-circle" className={styles.icon} theme="outlined" />
<Icon type="weibo-circle" className={styles.icon} theme="outlined" />
<Link className={styles.register} to="/user/register">
<FormattedMessage id="user-login.login.signup" />
</Link>
</div>
</LoginComponents>
</div>
);
}
}
export default Login;