index.tsx 6.63 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 Login from './components/Login';
7
import styles from './style.less';
陈帅's avatar
陈帅 committed
8 9 10 11
import { Dispatch } from 'redux';
import { IStateType } from './model';
import { FormComponentProps } from 'antd/lib/form';
import { CheckboxChangeEvent } from 'antd/lib/checkbox';
ddcat1115's avatar
ddcat1115 committed
12
const { Tab, UserName, Password, Mobile, Captcha, Submit } = Login;
13

陈帅's avatar
陈帅 committed
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
interface BLOCK_NAME_CAMEL_CASEProps {
  dispatch: Dispatch;
  BLOCK_NAME_CAMEL_CASE: IStateType;
  submitting: boolean;
}
interface BLOCK_NAME_CAMEL_CASEState {
  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'],
  })
)
class BLOCK_NAME_CAMEL_CASE extends Component<
  BLOCK_NAME_CAMEL_CASEProps,
  BLOCK_NAME_CAMEL_CASEState
> {
  state: BLOCK_NAME_CAMEL_CASEState = {
51
    type: 'account',
ddcat1115's avatar
ddcat1115 committed
52
    autoLogin: true,
jim's avatar
jim committed
53
  };
54

陈帅's avatar
陈帅 committed
55
  onTabChange = (type: string) => {
afc163's avatar
afc163 committed
56
    this.setState({ type });
jim's avatar
jim committed
57
  };
陈帅's avatar
陈帅 committed
58
  loginForm: FormComponentProps['form'] | undefined | null;
59 60
  onGetCaptcha = () =>
    new Promise((resolve, reject) => {
陈帅's avatar
陈帅 committed
61 62 63 64
      if (!this.loginForm) {
        return;
      }
      this.loginForm.validateFields(['mobile'], {}, (err: any, values: FromDataType) => {
65 66 67
        if (err) {
          reject(err);
        } else {
陈帅's avatar
陈帅 committed
68
          const { dispatch } = this.props;
陈帅's avatar
陈帅 committed
69
          ((dispatch({
70
            type: 'BLOCK_NAME_CAMEL_CASE/getCaptcha',
陈帅's avatar
陈帅 committed
71
            payload: values.mobile,
陈帅's avatar
陈帅 committed
72
          }) as unknown) as Promise<any>)
73 74 75 76 77
            .then(resolve)
            .catch(reject);
        }
      });
    });
陈帅's avatar
陈帅 committed
78

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

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

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

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

陈帅's avatar
陈帅 committed
210
export default BLOCK_NAME_CAMEL_CASE;