Login.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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('账户或密码错误(admin/888888)')}
  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. <Mobile name="mobile" />
  89. <Captcha name="captcha" countDown={120} onGetCaptcha={this.onGetCaptcha} />
  90. </Tab>
  91. <div>
  92. <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
  93. <FormattedMessage id="app.login.remember-me" />
  94. </Checkbox>
  95. <a style={{ float: 'right' }} href="">
  96. <FormattedMessage id="app.login.forgot-password" />
  97. </a>
  98. </div>
  99. <Submit loading={submitting}>
  100. <FormattedMessage id="app.login.login" />
  101. </Submit>
  102. <div className={styles.other}>
  103. <FormattedMessage id="app.login.sign-in-with" />
  104. <Icon type="alipay-circle" className={styles.icon} theme="outlined" />
  105. <Icon type="taobao-circle" className={styles.icon} theme="outlined" />
  106. <Icon type="weibo-circle" className={styles.icon} theme="outlined" />
  107. <Link className={styles.register} to="/User/Register">
  108. <FormattedMessage id="app.login.signup" />
  109. </Link>
  110. </div>
  111. </Login>
  112. </div>
  113. );
  114. }
  115. }
  116. export default LoginPage;