login.jsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React from 'react'
  2. import Login from 'ant-design-pro/lib/Login'
  3. import { Alert, Checkbox, Icon } from 'antd'
  4. import { Redirect } from 'dva/router'
  5. import { connect } from 'dva'
  6. import './login.less'
  7. const { UserName, Password, Submit } = Login;
  8. class LoginComponent extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. const { main } = props;
  12. const { currentUser, autoLogin } = main;
  13. const { account, password } = currentUser;
  14. this.state = {
  15. notice: '',
  16. currentUserName: account,
  17. currentPassword: password,
  18. autoLogin: autoLogin,
  19. redirectToReferrer: false,
  20. fetching: false
  21. }
  22. }
  23. onSubmit = (err, values) => {
  24. this.setState({
  25. notice: '',
  26. }, () => {
  27. if (!err && !!values.username && !!values.password) {
  28. this.login(values.username, values.password);
  29. }
  30. });
  31. }
  32. changeAutoLogin = (e) => {
  33. this.setState({
  34. autoLogin: e.target.checked,
  35. });
  36. }
  37. login = (username, password) => {
  38. const { dispatch } = this.props;
  39. const { autoLogin } = this.state;
  40. this.setState({
  41. fetching: true
  42. })
  43. dispatch({ type: 'main/login', username, password, autoLogin })
  44. .then((d) => {
  45. this.setState({
  46. notice: '',
  47. redirectToReferrer: d,
  48. fetching: false
  49. })
  50. }).catch((r) => {
  51. this.setState({
  52. notice: r.err+'' || r.data.msg+'',
  53. fetching: false
  54. });
  55. })
  56. };
  57. render() {
  58. const { from } = this.props.location.state || { from: { pathname: "/" } };
  59. const { currentUserName, currentPassword, notice, autoLogin, fetching, redirectToReferrer } = this.state;
  60. if (redirectToReferrer) {
  61. return <Redirect to={from} />;
  62. }
  63. return (
  64. <div className='login-container'>
  65. <div className='login-body'>
  66. <div className='login-header'></div>
  67. <div className='login-text'>登录</div>
  68. <Login
  69. className='login-content'
  70. onSubmit={this.onSubmit}
  71. >
  72. {
  73. notice &&
  74. <Alert style={{ marginBottom: 24 }} message={notice} type="error" showIcon closable />
  75. }
  76. <UserName
  77. name="username"
  78. disabled={fetching}
  79. placeholder='输入用户名'
  80. // defaultValue={autoLogin ? defaultAccount : ''}
  81. defaultValue={currentUserName}
  82. onChange={() => {
  83. this.setState({
  84. notice: ''
  85. });
  86. }}
  87. rules={[{
  88. required: true,
  89. whitespace: true,
  90. message: '用户名不能为空!'
  91. }]}
  92. onPressEnter={(e) => {
  93. document.getElementsByClassName('antd-pro-login-submit')[0].click();
  94. }}
  95. />
  96. <Password
  97. name="password"
  98. disabled={fetching}
  99. placeholder='输入密码'
  100. defaultValue={autoLogin ? currentPassword : ''}
  101. onChange={() => {
  102. this.setState({
  103. notice: ''
  104. });
  105. }}
  106. rules={[{
  107. required: true,
  108. whitespace: true,
  109. message: "密码不能为空!",
  110. }]}
  111. onPressEnter={(e) => {
  112. document.getElementsByClassName('antd-pro-login-submit')[0].click();
  113. }}
  114. />
  115. <div>
  116. <Checkbox className='checkbox-remember' defaultChecked={autoLogin} onChange={this.changeAutoLogin}>记住密码</Checkbox>
  117. {/* <a style={{ float: 'right' }} href="">忘记密码</a> */}
  118. </div>
  119. <Submit disabled={fetching}>
  120. {fetching && <Icon type="loading" theme="outlined" />}
  121. {fetching ? '登录中...' : '登录'}
  122. </Submit>
  123. <div>
  124. {/* <Link to="/register">
  125. <span style={{ float: 'right' }}>注册</span>
  126. </Link> */}
  127. </div>
  128. </Login>
  129. </div>
  130. </div>
  131. );
  132. }
  133. }
  134. export default connect(({ main }) => ({ main }))(LoginComponent);