ShowPassword.jsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.form.resetFields()
  25. props.onSuccess()
  26. }).catch(error => {
  27. // openNotificationWithIcon('error', error.message)
  28. })
  29. }
  30. });
  31. }
  32. const { getFieldDecorator } = props.form
  33. return (
  34. <>
  35. <Form labelCol={{ span: 7 }} wrapperCol={{ span: 12 }} onSubmit={(e) => handleSubmit(e)}>
  36. <Form.Item label="请输入旧密码">
  37. {getFieldDecorator('originalPassword', {
  38. rules: [{ required: true, message: '请输入旧密码' }],
  39. })(
  40. <Input
  41. prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
  42. type="password"
  43. placeholder="Password"
  44. />,
  45. )}
  46. </Form.Item>
  47. <Form.Item label="新密码">
  48. {getFieldDecorator('newPassword', {
  49. rules: [{ required: true, message: '请输入新密码' }],
  50. })(
  51. <Input
  52. prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
  53. type="password"
  54. placeholder="Password"
  55. />,
  56. )}
  57. </Form.Item>
  58. <Form.Item label="确认新密码">
  59. {getFieldDecorator('newPasswordToo', {
  60. rules: [{ required: true, message: '请确认新密码' }],
  61. })(
  62. <Input
  63. prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
  64. type="password"
  65. placeholder="Password"
  66. />,
  67. )}
  68. </Form.Item>
  69. <Form.Item>
  70. <Button type="primary" htmlType="submit">
  71. 确定
  72. </Button>
  73. </Form.Item>
  74. </Form>
  75. </>
  76. )
  77. }
  78. const WrappedShowPasswordForm = Form.create({ name: 'passwodForm' })(passwodForm)
  79. export default class ShowPassword extends Component {
  80. constructor(props) {
  81. super(props)
  82. this.state = { visible: false }
  83. }
  84. componentDidUpdate(prevProps) {
  85. // 典型用法(不要忘记比较 props):
  86. console.log('接收值: ', this.props.visible, prevProps.visible)
  87. if (this.props.visible !== prevProps.visible) {
  88. // eslint-disable-next-line react/no-did-update-set-state
  89. this.setState({ visible: this.props.visible })
  90. }
  91. }
  92. handleOk = e => {
  93. console.log('关闭了')
  94. this.props.onCancel()
  95. };
  96. handleCancel = e => {
  97. console.log('关闭了')
  98. this.props.onCancel()
  99. };
  100. render() {
  101. return (
  102. <>
  103. <Modal
  104. title="修改密码"
  105. visible={this.state.visible}
  106. onCancel={this.handleCancel}
  107. footer={null}>
  108. <WrappedShowPasswordForm onSuccess={(e) => this.handleCancel(e)} />
  109. </Modal>
  110. </>
  111. );
  112. }
  113. }