relogin.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React from 'react'
  2. import { Modal, Icon, Button, Input, Checkbox } from 'antd'
  3. import { connect } from 'dva'
  4. import './relogin.less'
  5. class Relogin extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. const { main } = props;
  9. const { currentUser, autoLogin } = main;
  10. const { account, password } = currentUser;
  11. this.state = {
  12. currentUserName: account,
  13. currentPassword: password,
  14. autoLogin: autoLogin,
  15. fetching: false
  16. }
  17. }
  18. onUsernameChange = (e) => {
  19. this.setState({ currentUserName: e.target.value });
  20. }
  21. onPasswordChange = (e) => {
  22. this.setState({ currentPassword: e.target.value });
  23. }
  24. onRemanberChange = (e) => {
  25. this.setState({ autoLogin: e.target.checked });
  26. }
  27. onLogin = () => {
  28. const { currentUserName, currentPassword, autoLogin } = this.state;
  29. const { dispatch } = this.props;
  30. this.setState({
  31. fetching: true
  32. })
  33. dispatch({ type: 'main/login', username: currentUserName, password: currentPassword, autoLogin })
  34. .then((d) => {
  35. this.setState({
  36. fetching: false
  37. })
  38. }).catch((r) => {
  39. console.error(r)
  40. })
  41. }
  42. render() {
  43. const { main, visibleBox, dispatch } = this.props;
  44. const { autoLogin, fetching } = this.state;
  45. const { currentUser } = main;
  46. const { account, password: defaultPassword } = currentUser;
  47. return <Modal
  48. title="重新登录"
  49. className='relogin-box'
  50. visible={visibleBox}
  51. footer={null}
  52. closable={false}
  53. centered={true}
  54. width={380}
  55. >
  56. <div className="relogin-item">距离上次登录已经超过30分钟,请重新登录</div>
  57. <Input
  58. placeholder="用户名"
  59. prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
  60. defaultValue={account}
  61. disabled={true}
  62. className="relogin-item"
  63. onChange={this.onUsernameChange}
  64. ></Input>
  65. <Input.Password
  66. placeholder="请输入登录密码"
  67. prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
  68. className="relogin-item"
  69. defaultValue={autoLogin ? defaultPassword : ''}
  70. rules={[{
  71. required: true,
  72. whitespace: true,
  73. message: "密码不能为空!",
  74. }]}
  75. onChange={this.onPasswordChange}
  76. onPressEnter={(e) => {
  77. this.onLogin()
  78. }}
  79. ></Input.Password>
  80. <div className="relogin-item remanber">
  81. <Checkbox defaultChecked={autoLogin} onChange={this.onRemanberChange}>记住密码</Checkbox>
  82. </div>
  83. <div className="relogin-item buttons">
  84. <Button disabled={fetching} type="primary" onClick={this.onLogin}>
  85. {fetching && <Icon type="loading" theme="outlined" />}
  86. 重新登录
  87. </Button>
  88. </div>
  89. <div style={{ textAlign: 'end' }}>
  90. <a style={{ textDecoration: 'underline' }} onClick={() => {
  91. dispatch({ type: 'main/logout' });
  92. dispatch({ type: 'main/redirect', path: '/login' });
  93. }}>
  94. 切换用户
  95. </a>
  96. </div>
  97. </Modal>
  98. }
  99. }
  100. function mapStateToProps({ main }) {
  101. return { main };
  102. }
  103. export default connect(mapStateToProps)(Relogin)