index.tsx 6.71 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
陈帅's avatar
陈帅 committed
3
import { formatMessage, FormattedMessage } from 'umi-plugin-react/locale';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
ddcat1115's avatar
ddcat1115 committed
5
import { Checkbox, Alert, Icon } from 'antd';
陈帅's avatar
陈帅 committed
6
import { Dispatch } from 'redux';
陈帅's avatar
陈帅 committed
7 8
import { FormComponentProps } from 'antd/es/form';
import { CheckboxChangeEvent } from 'antd/es/checkbox';
陈帅's avatar
陈帅 committed
9 10 11
import LoginComponents from './components/Login';
import styles from './style.less';
import { IStateType } from './model';
陈帅's avatar
陈帅 committed
12

陈帅's avatar
陈帅 committed
13
const { Tab, UserName, Password, Mobile, Captcha, Submit } = LoginComponents;
14

陈帅's avatar
陈帅 committed
15 16
interface PAGE_NAME_UPPER_CAMEL_CASEProps {
  dispatch: Dispatch<any>;
陈帅's avatar
陈帅 committed
17 18 19
  BLOCK_NAME_CAMEL_CASE: IStateType;
  submitting: boolean;
}
陈帅's avatar
陈帅 committed
20
interface PAGE_NAME_UPPER_CAMEL_CASEState {
陈帅's avatar
陈帅 committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  type: string;
  autoLogin: boolean;
}
export interface FromDataType {
  userName: string;
  password: string;
  mobile: string;
  captcha: string;
}

@connect(
  ({
    BLOCK_NAME_CAMEL_CASE,
    loading,
  }: {
    BLOCK_NAME_CAMEL_CASE: IStateType;
    loading: {
      effects: {
        [key: string]: string;
      };
    };
  }) => ({
    BLOCK_NAME_CAMEL_CASE,
    submitting: loading.effects['BLOCK_NAME_CAMEL_CASE/login'],
陈帅's avatar
陈帅 committed
45
  }),
陈帅's avatar
陈帅 committed
46
)
陈帅's avatar
陈帅 committed
47 48 49
class PAGE_NAME_UPPER_CAMEL_CASE extends Component<
  PAGE_NAME_UPPER_CAMEL_CASEProps,
  PAGE_NAME_UPPER_CAMEL_CASEState
陈帅's avatar
陈帅 committed
50
> {
陈帅's avatar
陈帅 committed
51
  state: PAGE_NAME_UPPER_CAMEL_CASEState = {
52
    type: 'account',
ddcat1115's avatar
ddcat1115 committed
53
    autoLogin: true,
jim's avatar
jim committed
54
  };
陈帅's avatar
陈帅 committed
55

陈帅's avatar
陈帅 committed
56
  loginForm: FormComponentProps['form'] | undefined | null;
57

陈帅's avatar
陈帅 committed
58
  onTabChange = (type: string) => {
afc163's avatar
afc163 committed
59
    this.setState({ type });
jim's avatar
jim committed
60
  };
陈帅's avatar
陈帅 committed
61

62 63
  onGetCaptcha = () =>
    new Promise((resolve, reject) => {
陈帅's avatar
陈帅 committed
64 65 66 67
      if (!this.loginForm) {
        return;
      }
      this.loginForm.validateFields(['mobile'], {}, (err: any, values: FromDataType) => {
68 69 70
        if (err) {
          reject(err);
        } else {
陈帅's avatar
陈帅 committed
71
          const { dispatch } = this.props;
陈帅's avatar
陈帅 committed
72
          ((dispatch({
73
            type: 'BLOCK_NAME_CAMEL_CASE/getCaptcha',
陈帅's avatar
陈帅 committed
74
            payload: values.mobile,
陈帅's avatar
陈帅 committed
75
          }) as unknown) as Promise<any>)
76 77 78 79 80
            .then(resolve)
            .catch(reject);
        }
      });
    });
陈帅's avatar
陈帅 committed
81

陈帅's avatar
陈帅 committed
82
  handleSubmit = (err: any, values: FromDataType) => {
ddcat1115's avatar
ddcat1115 committed
83 84
    const { type } = this.state;
    if (!err) {
陈帅's avatar
陈帅 committed
85 86
      const { dispatch } = this.props;
      dispatch({
87
        type: 'BLOCK_NAME_CAMEL_CASE/login',
ddcat1115's avatar
ddcat1115 committed
88 89 90 91 92 93
        payload: {
          ...values,
          type,
        },
      });
    }
jim's avatar
jim committed
94
  };
95

陈帅's avatar
陈帅 committed
96
  changeAutoLogin = (e: CheckboxChangeEvent) => {
ddcat1115's avatar
ddcat1115 committed
97 98 99
    this.setState({
      autoLogin: e.target.checked,
    });
jim's avatar
jim committed
100
  };
101

陈帅's avatar
陈帅 committed
102
  renderMessage = (content: string) => (
103 104
    <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
  );
105 106

  render() {
107 108
    const { BLOCK_NAME_CAMEL_CASE, submitting } = this.props;
    const { status, type: loginType } = BLOCK_NAME_CAMEL_CASE;
陈帅's avatar
陈帅 committed
109
    const { type, autoLogin } = this.state;
110 111
    return (
      <div className={styles.main}>
陈帅's avatar
陈帅 committed
112
        <LoginComponents
113 114 115
          defaultActiveKey={type}
          onTabChange={this.onTabChange}
          onSubmit={this.handleSubmit}
陈帅's avatar
陈帅 committed
116
          ref={(form: any) => {
117 118 119
            this.loginForm = form;
          }}
        >
120 121 122
          <Tab key="account" tab={formatMessage({ id: 'BLOCK_NAME.login.tab-login-credentials' })}>
            {status === 'error' &&
              loginType === 'account' &&
Amumu's avatar
Amumu committed
123
              !submitting &&
xiaohuoni's avatar
xiaohuoni committed
124
              this.renderMessage(
陈帅's avatar
陈帅 committed
125
                formatMessage({ id: 'BLOCK_NAME.login.message-invalid-credentials' }),
xiaohuoni's avatar
xiaohuoni committed
126
              )}
127 128
            <UserName
              name="userName"
129
              placeholder={`${formatMessage({ id: 'BLOCK_NAME.login.userName' })}: admin or user`}
130 131 132
              rules={[
                {
                  required: true,
133
                  message: formatMessage({ id: 'BLOCK_NAME.userName.required' }),
134 135
                },
              ]}
陈帅's avatar
陈帅 committed
136
            />
陈帅's avatar
陈帅 committed
137 138
            <Password
              name="password"
139
              placeholder={`${formatMessage({ id: 'BLOCK_NAME.login.password' })}: ant.design`}
140 141 142
              rules={[
                {
                  required: true,
143
                  message: formatMessage({ id: 'BLOCK_NAME.password.required' }),
144 145
                },
              ]}
陈帅's avatar
陈帅 committed
146 147 148
              onPressEnter={() =>
                this.loginForm && this.loginForm.validateFields(this.handleSubmit)
              }
陈帅's avatar
陈帅 committed
149
            />
ddcat1115's avatar
ddcat1115 committed
150
          </Tab>
151 152 153
          <Tab key="mobile" tab={formatMessage({ id: 'BLOCK_NAME.login.tab-login-mobile' })}>
            {status === 'error' &&
              loginType === 'mobile' &&
Amumu's avatar
Amumu committed
154
              !submitting &&
Rayron Victor's avatar
Rayron Victor committed
155
              this.renderMessage(
陈帅's avatar
陈帅 committed
156
                formatMessage({ id: 'BLOCK_NAME.login.message-invalid-verification-code' }),
Rayron Victor's avatar
Rayron Victor committed
157
              )}
陈帅's avatar
陈帅 committed
158
            <Mobile
159
              name="mobile"
160
              placeholder={formatMessage({ id: 'BLOCK_NAME.phone-number.placeholder' })}
161 162 163
              rules={[
                {
                  required: true,
164
                  message: formatMessage({ id: 'BLOCK_NAME.phone-number.required' }),
165 166 167
                },
                {
                  pattern: /^1\d{10}$/,
168
                  message: formatMessage({ id: 'BLOCK_NAME.phone-number.wrong-format' }),
169 170 171
                },
              ]}
            />
陈帅's avatar
陈帅 committed
172 173
            <Captcha
              name="captcha"
174
              placeholder={formatMessage({ id: 'BLOCK_NAME.verification-code.placeholder' })}
陈帅's avatar
陈帅 committed
175 176
              countDown={120}
              onGetCaptcha={this.onGetCaptcha}
177 178
              getCaptchaButtonText={formatMessage({ id: 'BLOCK_NAME.form.get-captcha' })}
              getCaptchaSecondText={formatMessage({ id: 'BLOCK_NAME.captcha.second' })}
179 180 181
              rules={[
                {
                  required: true,
182
                  message: formatMessage({ id: 'BLOCK_NAME.verification-code.required' }),
183 184
                },
              ]}
陈帅's avatar
陈帅 committed
185
            />
ddcat1115's avatar
ddcat1115 committed
186 187
          </Tab>
          <div>
陈帅's avatar
陈帅 committed
188
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
189
              <FormattedMessage id="BLOCK_NAME.login.remember-me" />
jim's avatar
jim committed
190 191
            </Checkbox>
            <a style={{ float: 'right' }} href="">
192
              <FormattedMessage id="BLOCK_NAME.login.forgot-password" />
jim's avatar
jim committed
193
            </a>
ddcat1115's avatar
ddcat1115 committed
194
          </div>
195
          <Submit loading={submitting}>
196
            <FormattedMessage id="BLOCK_NAME.login.login" />
197
          </Submit>
ddcat1115's avatar
ddcat1115 committed
198
          <div className={styles.other}>
199
            <FormattedMessage id="BLOCK_NAME.login.sign-in-with" />
陈帅's avatar
陈帅 committed
200 201 202
            <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" />
陈帅's avatar
陈帅 committed
203
            <Link className={styles.register} to="/user/register">
204
              <FormattedMessage id="BLOCK_NAME.login.signup" />
jim's avatar
jim committed
205
            </Link>
ddcat1115's avatar
ddcat1115 committed
206
          </div>
陈帅's avatar
陈帅 committed
207
        </LoginComponents>
208 209 210 211
      </div>
    );
  }
}
陈帅's avatar
陈帅 committed
212

陈帅's avatar
陈帅 committed
213
export default PAGE_NAME_UPPER_CAMEL_CASE;