index.tsx 5.65 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';
ddcat1115's avatar
ddcat1115 committed
8
const { Tab, UserName, Password, Mobile, Captcha, Submit } = Login;
9

10 11 12
@connect(({ BLOCK_NAME_CAMEL_CASE, loading }) => ({
  BLOCK_NAME_CAMEL_CASE,
  submitting: loading.effects['BLOCK_NAME_CAMEL_CASE/login'],
13
}))
afc163's avatar
afc163 committed
14
class LoginPage extends Component {
15 16
  state = {
    type: 'account',
ddcat1115's avatar
ddcat1115 committed
17
    autoLogin: true,
jim's avatar
jim committed
18
  };
19

jim's avatar
jim committed
20
  onTabChange = type => {
afc163's avatar
afc163 committed
21
    this.setState({ type });
jim's avatar
jim committed
22
  };
陈帅's avatar
陈帅 committed
23
  loginForm: loginForm;
24 25
  onGetCaptcha = () =>
    new Promise((resolve, reject) => {
26 27 28 29
      this.loginForm.validateFields(['mobile'], {}, (err, values) => {
        if (err) {
          reject(err);
        } else {
陈帅's avatar
陈帅 committed
30 31
          const { dispatch } = this.props;
          dispatch({
32
            type: 'BLOCK_NAME_CAMEL_CASE/getCaptcha',
陈帅's avatar
陈帅 committed
33 34
            payload: values.mobile,
          })
35 36 37 38 39
            .then(resolve)
            .catch(reject);
        }
      });
    });
陈帅's avatar
陈帅 committed
40

ddcat1115's avatar
ddcat1115 committed
41 42 43
  handleSubmit = (err, values) => {
    const { type } = this.state;
    if (!err) {
陈帅's avatar
陈帅 committed
44 45
      const { dispatch } = this.props;
      dispatch({
46
        type: 'BLOCK_NAME_CAMEL_CASE/login',
ddcat1115's avatar
ddcat1115 committed
47 48 49 50 51 52
        payload: {
          ...values,
          type,
        },
      });
    }
jim's avatar
jim committed
53
  };
54

jim's avatar
jim committed
55
  changeAutoLogin = e => {
ddcat1115's avatar
ddcat1115 committed
56 57 58
    this.setState({
      autoLogin: e.target.checked,
    });
jim's avatar
jim committed
59
  };
60

61 62 63
  renderMessage = content => (
    <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
  );
64 65

  render() {
66 67
    const { BLOCK_NAME_CAMEL_CASE, submitting } = this.props;
    const { status, type: loginType } = BLOCK_NAME_CAMEL_CASE;
陈帅's avatar
陈帅 committed
68
    const { type, autoLogin } = this.state;
69 70
    return (
      <div className={styles.main}>
71 72 73 74 75 76 77 78
        <Login
          defaultActiveKey={type}
          onTabChange={this.onTabChange}
          onSubmit={this.handleSubmit}
          ref={form => {
            this.loginForm = form;
          }}
        >
79 80 81
          <Tab key="account" tab={formatMessage({ id: 'BLOCK_NAME.login.tab-login-credentials' })}>
            {status === 'error' &&
              loginType === 'account' &&
Amumu's avatar
Amumu committed
82
              !submitting &&
xiaohuoni's avatar
xiaohuoni committed
83 84 85
              this.renderMessage(
                formatMessage({ id: 'BLOCK_NAME.login.message-invalid-credentials' })
              )}
86 87
            <UserName
              name="userName"
88
              placeholder={`${formatMessage({ id: 'BLOCK_NAME.login.userName' })}: admin or user`}
89 90 91
              rules={[
                {
                  required: true,
92
                  message: formatMessage({ id: 'BLOCK_NAME.userName.required' }),
93 94
                },
              ]}
陈帅's avatar
陈帅 committed
95
            />{' '}
陈帅's avatar
陈帅 committed
96 97
            <Password
              name="password"
98
              placeholder={`${formatMessage({ id: 'BLOCK_NAME.login.password' })}: ant.design`}
99 100 101
              rules={[
                {
                  required: true,
102
                  message: formatMessage({ id: 'BLOCK_NAME.password.required' }),
103 104
                },
              ]}
陈帅's avatar
陈帅 committed
105 106
              onPressEnter={() => this.loginForm.validateFields(this.handleSubmit)}
            />
ddcat1115's avatar
ddcat1115 committed
107
          </Tab>
108 109 110
          <Tab key="mobile" tab={formatMessage({ id: 'BLOCK_NAME.login.tab-login-mobile' })}>
            {status === 'error' &&
              loginType === 'mobile' &&
Amumu's avatar
Amumu committed
111
              !submitting &&
Rayron Victor's avatar
Rayron Victor committed
112
              this.renderMessage(
113
                formatMessage({ id: 'BLOCK_NAME.login.message-invalid-verification-code' })
Rayron Victor's avatar
Rayron Victor committed
114
              )}
陈帅's avatar
陈帅 committed
115
            {/* <Mobile
116
              name="mobile"
117
              placeholder={formatMessage({ id: 'BLOCK_NAME.phone-number.placeholder' })}
118 119 120
              rules={[
                {
                  required: true,
121
                  message: formatMessage({ id: 'BLOCK_NAME.phone-number.required' }),
122 123 124
                },
                {
                  pattern: /^1\d{10}$/,
125
                  message: formatMessage({ id: 'BLOCK_NAME.phone-number.wrong-format' }),
126 127 128
                },
              ]}
            />
陈帅's avatar
陈帅 committed
129 130
            <Captcha
              name="captcha"
131
              placeholder={formatMessage({ id: 'BLOCK_NAME.verification-code.placeholder' })}
陈帅's avatar
陈帅 committed
132 133
              countDown={120}
              onGetCaptcha={this.onGetCaptcha}
134 135
              getCaptchaButtonText={formatMessage({ id: 'BLOCK_NAME.form.get-captcha' })}
              getCaptchaSecondText={formatMessage({ id: 'BLOCK_NAME.captcha.second' })}
136 137 138
              rules={[
                {
                  required: true,
139
                  message: formatMessage({ id: 'BLOCK_NAME.verification-code.required' }),
140 141
                },
              ]}
陈帅's avatar
陈帅 committed
142
            /> */}
ddcat1115's avatar
ddcat1115 committed
143 144
          </Tab>
          <div>
陈帅's avatar
陈帅 committed
145
            <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
146
              <FormattedMessage id="BLOCK_NAME.login.remember-me" />
jim's avatar
jim committed
147 148
            </Checkbox>
            <a style={{ float: 'right' }} href="">
149
              <FormattedMessage id="BLOCK_NAME.login.forgot-password" />
jim's avatar
jim committed
150
            </a>
ddcat1115's avatar
ddcat1115 committed
151
          </div>
152
          <Submit loading={submitting}>
153
            <FormattedMessage id="BLOCK_NAME.login.login" />
154
          </Submit>
ddcat1115's avatar
ddcat1115 committed
155
          <div className={styles.other}>
156
            <FormattedMessage id="BLOCK_NAME.login.sign-in-with" />
陈帅's avatar
陈帅 committed
157 158 159
            <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
160
            <Link className={styles.register} to="/user/register">
161
              <FormattedMessage id="BLOCK_NAME.login.signup" />
jim's avatar
jim committed
162
            </Link>
ddcat1115's avatar
ddcat1115 committed
163 164
          </div>
        </Login>
165 166 167 168
      </div>
    );
  }
}
陈帅's avatar
陈帅 committed
169 170

export default LoginPage;