Login.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React, { Component } from 'react';
  2. import { connect } from 'dva';
  3. import { formatMessage, FormattedMessage } from 'umi/locale';
  4. import Link from 'umi/link';
  5. import { Checkbox, Alert, Icon } from 'antd';
  6. import Login from '@/components/Login';
  7. import styles from './Login.less';
  8. const { Tab, UserName, Password, Mobile, Captcha, Submit } = Login;
  9. @connect(({ login, loading }) => ({
  10. login,
  11. submitting: loading.effects['login/login'],
  12. }))
  13. class LoginPage extends Component {
  14. state = {
  15. type: 'account',
  16. autoLogin: true,
  17. };
  18. onTabChange = type => {
  19. this.setState({ type });
  20. };
  21. onGetCaptcha = () =>
  22. new Promise((resolve, reject) => {
  23. this.loginForm.validateFields(['mobile'], {}, (err, values) => {
  24. if (err) {
  25. reject(err);
  26. } else {
  27. const { dispatch } = this.props;
  28. dispatch({
  29. type: 'login/getCaptcha',
  30. payload: values.mobile,
  31. })
  32. .then(resolve)
  33. .catch(reject);
  34. }
  35. });
  36. });
  37. handleSubmit = (err, values) => {
  38. const { type } = this.state;
  39. if (!err) {
  40. const { dispatch } = this.props;
  41. dispatch({
  42. type: 'login/login',
  43. payload: {
  44. ...values,
  45. type,
  46. },
  47. });
  48. }
  49. };
  50. changeAutoLogin = e => {
  51. this.setState({
  52. autoLogin: e.target.checked,
  53. });
  54. };
  55. renderMessage = content => (
  56. <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
  57. );
  58. render() {
  59. const { login, submitting } = this.props;
  60. const { type, autoLogin } = this.state;
  61. return (
  62. <div className={styles.main}>
  63. <Login
  64. defaultActiveKey={type}
  65. onTabChange={this.onTabChange}
  66. onSubmit={this.handleSubmit}
  67. ref={form => {
  68. this.loginForm = form;
  69. }}
  70. >
  71. <Tab key="account" tab={formatMessage({ id: 'app.login.tab-login-credentials' })}>
  72. {login.status === 'error' &&
  73. login.type === 'account' &&
  74. !submitting &&
  75. this.renderMessage(formatMessage({ id: 'app.login.message-invalid-credentials' }))}
  76. <UserName name="userName" placeholder="admin/user" />
  77. <Password
  78. name="password"
  79. placeholder="888888/123456"
  80. onPressEnter={() => this.loginForm.validateFields(this.handleSubmit)}
  81. />
  82. </Tab>
  83. <Tab key="mobile" tab={formatMessage({ id: 'app.login.tab-login-mobile' })}>
  84. {login.status === 'error' &&
  85. login.type === 'mobile' &&
  86. !submitting &&
  87. this.renderMessage(
  88. formatMessage({ id: 'app.login.message-invalid-verification-code' })
  89. )}
  90. <Mobile name="mobile" />
  91. <Captcha name="captcha" countDown={120} onGetCaptcha={this.onGetCaptcha} />
  92. </Tab>
  93. <div>
  94. <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
  95. <FormattedMessage id="app.login.remember-me" />
  96. </Checkbox>
  97. <a style={{ float: 'right' }} href="">
  98. <FormattedMessage id="app.login.forgot-password" />
  99. </a>
  100. </div>
  101. <Submit loading={submitting}>
  102. <FormattedMessage id="app.login.login" />
  103. </Submit>
  104. <div className={styles.other}>
  105. <FormattedMessage id="app.login.sign-in-with" />
  106. <Icon type="alipay-circle" className={styles.icon} theme="outlined" />
  107. <Icon type="taobao-circle" className={styles.icon} theme="outlined" />
  108. <Icon type="weibo-circle" className={styles.icon} theme="outlined" />
  109. <Link className={styles.register} to="/User/Register">
  110. <FormattedMessage id="app.login.signup" />
  111. </Link>
  112. </div>
  113. </Login>
  114. </div>
  115. );
  116. }
  117. }
  118. export default LoginPage;