changePassword.jsx 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React from 'react';
  2. import ajax from 'jquery/src/ajax/xhr.js'
  3. import $ from 'jquery/src/ajax';
  4. import { Modal, Button, Icon, Input, message } from 'antd';
  5. import './changeModal.less';
  6. const ChangePw = React.createClass({
  7. getInitialState() {
  8. return { visible: false };
  9. },
  10. showModal() {
  11. this.setState({
  12. visible: true,
  13. });
  14. },
  15. logOut() {
  16. $.ajax({
  17. method: "get",
  18. dataType: "json",
  19. url: globalConfig.context + "/login",
  20. }).done(function (data) {
  21. window.location.href = globalConfig.context + "/user/login.html"
  22. });
  23. },
  24. handleOk() {
  25. if (this.state.newPw.length < 6 || this.state.newPw.length > 20) {
  26. message.warning('请使用长度为6-20位的密码!');
  27. } else if (this.state.confirmPw !== this.state.newPw) {
  28. message.warning('两次密码输入不相同!');
  29. } else if (this.state.confirmPw === this.state.newPw) {
  30. $.ajax({
  31. method: "POST",
  32. dataType: "json",
  33. crossDomain: false,
  34. url: globalConfig.context + "/api/user/pwd",
  35. data: {
  36. "password": this.state.oldPw,
  37. "newPassword": this.state.newPw
  38. }
  39. }).done(function (data) {
  40. if (!data.error.length) {
  41. message.success('修改成功!');
  42. this.setState({
  43. visible: false,
  44. });
  45. this.logOut();
  46. } else {
  47. message.warning(data.error[0].message);
  48. }
  49. }.bind(this));
  50. };
  51. },
  52. handleCancel(e) {
  53. this.setState({
  54. visible: false,
  55. });
  56. },
  57. oldPwChange(e) {
  58. this.state.oldPw = e.target.value;
  59. },
  60. newPwChange(e) {
  61. this.state.newPw = e.target.value;
  62. },
  63. confirmPwChange(e) {
  64. this.state.confirmPw = e.target.value;
  65. },
  66. render() {
  67. return (
  68. <div style={{ 'float': 'left', 'marginTop': '0' }}>
  69. <Button type="primary" onClick={this.showModal}>修改密码</Button>
  70. <Modal maskClosable={false} className="change-modal" title="设置新密码" visible={this.state.visible}
  71. onOk={this.handleOk} onCancel={this.handleCancel}
  72. width={360}
  73. footer={[
  74. <Button key="submit" type="primary" size="large" loading={this.state.loading} onClick={this.handleOk}>确认</Button>,
  75. <Button key="back" type="ghost" size="large" onClick={this.handleCancel}>取消</Button>
  76. ]}
  77. >
  78. <p className="modal-intro"><Icon type="info-circle" />密码长度为6-20个字符,区分大小写</p>
  79. <p><span>原密码:</span><Input type="password" onChange={this.oldPwChange} /></p>
  80. <p><span>新密码:</span><Input type="password" onChange={this.newPwChange} /></p>
  81. <p><span>确认密码:</span><Input type="password" onChange={this.confirmPwChange} /></p>
  82. </Modal>
  83. </div>
  84. );
  85. },
  86. });
  87. export default ChangePw;