updatePassword.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { message, Form, Input, Modal } from 'antd';
  2. import { history, useModel } from 'umi';
  3. import md5 from 'js-md5';
  4. import { updatePwd } from '@/services/ant-design-pro/api';
  5. /**
  6. * 修改密码
  7. * @param props
  8. * @constructor
  9. */
  10. const UpdatePassword = (props: any) => {
  11. const { initialState, setInitialState } = useModel('@@initialState');
  12. const { visible, onCancel } = props;
  13. const [form] = Form.useForm();
  14. const handleCancel = () => {
  15. if (onCancel) {
  16. onCancel();
  17. }
  18. };
  19. const onFinish = () => {
  20. form.validateFields().then((values) => {
  21. // const reg = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\da-zA-Z~!@#$%^&*]{8,}$/;
  22. // if (!reg.test(values.new_password)) {
  23. // message.warning('密码必须为数字、字母和特殊字符(!@#$%^&*)的组合,且不能小于8位');
  24. // return;
  25. // }
  26. const formData = {
  27. old_password: md5(values.old_password),
  28. new_password: md5(values.new_password),
  29. };
  30. updatePwd(formData).then((res: any) => {
  31. if (res.status === 'OK') {
  32. message.success('密码更新成功!');
  33. console.log(initialState);
  34. setInitialState((s) => ({ ...s, currentUser: undefined }));
  35. history.push(`/user/login`);
  36. }
  37. });
  38. });
  39. };
  40. const formItemLayout = {
  41. labelCol: {
  42. span: 6,
  43. },
  44. wrapperCol: {
  45. span: 16,
  46. },
  47. };
  48. return (
  49. <Modal title="修改密码" open={visible} width={500} onOk={onFinish} onCancel={handleCancel}>
  50. <Form form={form}>
  51. <Form.Item
  52. {...formItemLayout}
  53. name="old_password"
  54. label="旧密码"
  55. hasFeedback
  56. rules={[{ required: true, message: '请输入旧密码' }]}
  57. >
  58. <Input type="password" placeholder="请输入旧密码" />
  59. </Form.Item>
  60. <Form.Item
  61. {...formItemLayout}
  62. name="new_password"
  63. label="新密码"
  64. hasFeedback
  65. rules={[{ required: true, message: '请输入新密码' }]}
  66. >
  67. <Input type="password" placeholder="请输入新密码" />
  68. </Form.Item>
  69. <Form.Item
  70. {...formItemLayout}
  71. name="confirm_new_password"
  72. label="确认新密码"
  73. hasFeedback
  74. rules={[
  75. { required: true, message: '请输入确认新密码' },
  76. ({ getFieldValue }) => ({
  77. validator(_, value) {
  78. if (!value || getFieldValue('new_password') === value) {
  79. return Promise.resolve();
  80. }
  81. return Promise.reject(new Error('两次密码输入不一致!'));
  82. },
  83. }),
  84. ]}
  85. >
  86. <Input type="password" placeholder="请再次输入新密码" />
  87. </Form.Item>
  88. </Form>
  89. </Modal>
  90. );
  91. };
  92. export default UpdatePassword;