ShowPassword.jsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { Component } from 'react'
  2. import { Modal, Button, Form, Input, Icon, notification } from 'antd'
  3. import request from '../../utils/request'
  4. import apis from '../../services/apis'
  5. function passwodForm(props) {
  6. const openNotificationWithIcon = (type, message) => {
  7. notification[type]({
  8. message,
  9. description:
  10. '',
  11. });
  12. }
  13. function handleSubmit(e) {
  14. e.preventDefault();
  15. props.form.validateFieldsAndScroll((err, values) => {
  16. // 两次密码比较
  17. if (values.newPassword !== values.newPasswordToo) {
  18. openNotificationWithIcon('error', '两次密码输入不一样,请重新输入')
  19. return
  20. }
  21. if (!err) {
  22. request({ ...apis.user.changePassword, params: { ...values } }).then(() => {
  23. openNotificationWithIcon('success', '操作成功!')
  24. props.onSuccess()
  25. }).catch(error => {
  26. // openNotificationWithIcon('error', error.message)
  27. })
  28. }
  29. });
  30. }
  31. // 弹框取消按钮
  32. function handlePopCancel(e) {
  33. props.onCancel()
  34. }
  35. const { getFieldDecorator } = props.form
  36. return (
  37. <>
  38. <Form labelCol={{ span: 7 }} wrapperCol={{ span: 12 }} onSubmit={(e) => handleSubmit(e)}>
  39. <Form.Item label="请输入旧密码">
  40. {getFieldDecorator('originalPassword', {
  41. rules: [{ required: true, message: '请输入旧密码' }],
  42. })(
  43. <Input
  44. prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
  45. type="password"
  46. placeholder="Password"
  47. />,
  48. )}
  49. </Form.Item>
  50. <Form.Item label="新密码">
  51. {getFieldDecorator('newPassword', {
  52. rules: [{ required: true, message: '请输入新密码' }],
  53. })(
  54. <Input
  55. prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
  56. type="password"
  57. placeholder="Password"
  58. />,
  59. )}
  60. </Form.Item>
  61. <Form.Item label="确认新密码">
  62. {getFieldDecorator('newPasswordToo', {
  63. rules: [{ required: true, message: '请确认新密码' }],
  64. })(
  65. <Input
  66. prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
  67. type="password"
  68. placeholder="Password"
  69. />,
  70. )}
  71. </Form.Item>
  72. <Form.Item>
  73. <Button type="primary" htmlType="submit">保存</Button>
  74. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  75. <Button onClick={(e) => handlePopCancel(e)}>取消</Button>
  76. </Form.Item>
  77. </Form>
  78. </>
  79. )
  80. }
  81. const WrappedShowPasswordForm = Form.create({ name: 'passwodForm' })(passwodForm)
  82. export default class ShowPassword extends Component {
  83. constructor(props) {
  84. super(props)
  85. this.state = { visible: false }
  86. }
  87. componentDidUpdate(prevProps) {
  88. // 典型用法(不要忘记比较 props):
  89. console.log('接收值: ', this.props.visible, prevProps.visible)
  90. if (this.props.visible !== prevProps.visible) {
  91. // eslint-disable-next-line react/no-did-update-set-state
  92. this.setState({ visible: this.props.visible })
  93. }
  94. }
  95. handleOk = e => {
  96. console.log('修改密码成功')
  97. this.props.onReset()
  98. };
  99. handleCancel = e => {
  100. console.log('关闭了')
  101. this.props.onCancel()
  102. };
  103. render() {
  104. return (
  105. <>
  106. <Modal
  107. title="修改密码"
  108. visible={this.state.visible}
  109. onCancel={this.handleCancel}
  110. footer={null}>
  111. <WrappedShowPasswordForm onSuccess={(e) => this.handleOk(e)} onCancel={(e) => this.handleCancel(e)}/>
  112. </Modal>
  113. </>
  114. );
  115. }
  116. }