ChangePasswordModal.jsx 4.39 KB
Newer Older
1
/* eslint-disable import/no-unresolved */
2 3
import React, { useEffect } from 'react';
import { Modal, Form, Input, notification, message } from 'antd';
邓超's avatar
邓超 committed
4
import { updateUserPassword } from '@/services/userManage/api';
5 6

const ChangePasswordModal = props => {
7
  const { visible, currentUser, currentSelectOrg, submitSearchUser, onSelect, onCancel } = props;
8 9 10
  const [passwordForm] = Form.useForm(); // 修改密码

  useEffect(() => {
11 12 13 14 15 16 17 18 19
    console.log(currentUser);
    if (visible) {
      console.log(currentUser.password);
      passwordForm.setFieldsValue({
        oldPassword: currentUser.password,
        newPassword: '',
        passwordConfirm: '',
      });
    }
20
  }, [visible]);
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

  // 提交-修改密码
  const submitChangePassword = () => {
    const oldPassword = passwordForm.getFieldValue('oldPassword');
    const newPassword = passwordForm.getFieldValue('newPassword');
    const passwordConfirm = passwordForm.getFieldValue('passwordConfirm');
    if (
      newPassword &&
      newPassword.length >= 6 &&
      passwordConfirm &&
      newPassword.length >= 6 &&
      newPassword === passwordConfirm
    ) {
      updateUserPassword({
        UserId: +currentUser.userID,
        OldPassWord: oldPassword,
        NewPassWord: newPassword,
      })
        .then(res => {
          if (res.code === 0) {
            onCancel();
42
            // eslint-disable-next-line no-unused-expressions
43
            currentSelectOrg === '-1' ? submitSearchUser() : onSelect([currentSelectOrg]);
44 45 46 47 48 49 50 51 52 53
            notification.success({
              message: '提交成功',
              duration: 2,
            });
          } else {
            notification.error({
              message: '提交失败',
              description: res.message,
            });
          }
tianfen's avatar
tianfen committed
54 55 56 57 58
          passwordForm.setFieldsValue({
            oldPassword: currentUser.password,
            newPassword: '',
            passwordConfirm: '',
          });
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        })
        .catch(err => {
          message.error(err);
        });
    } else if (newPassword === '' || passwordConfirm === '') {
      notification.error({
        message: '提交失败',
        description: '带*号为必填项,不能为空',
      });
    } else if (
      (newPassword && newPassword.length < 6) ||
      (passwordConfirm && passwordConfirm < 6)
    ) {
      notification.error({
        message: '提交失败',
        description: '密码至少为6位!',
      });
    } else if (newPassword !== passwordConfirm) {
      notification.error({
        message: '提交失败',
        description: '确认密码不一致!',
      });
    }
  };
83 84 85 86 87 88 89 90 91
  const title = (
    <span>
      修改用户
      <span style={{ fontWeight: 'bold', color: 'rgb(24, 144, 255)' }}>
{currentUser.userName}
      </span>
      的密码
    </span>
  );
92 93
  return (
    <Modal
94
      title={title}
95 96
      visible={visible}
      onOk={submitChangePassword}
97 98 99 100 101
      maskClosable={false}
      destroyOnClose
      afterClose={() => {
        passwordForm.resetFields();
      }}
tianfen's avatar
tianfen committed
102 103 104 105 106 107 108 109
      onCancel={() => {
        passwordForm.setFieldsValue({
          oldPassword: currentUser.password,
          newPassword: '',
          passwordConfirm: '',
        });
        onCancel();
      }}
110 111 112 113 114 115 116 117 118 119
      okText="确认"
      cancelText="取消"
    >
      <Form form={passwordForm} labelCol={{ span: 4 }}>
        <Form.Item name="oldPassword" label="原始密码">
          <Input disabled />
        </Form.Item>
        <Form.Item
          name="newPassword"
          label="新密码"
120 121 122 123 124 125 126
          rules={[
            {
              pattern: /^(?=.*[a-zA-Z\d])(?=.*([a-zA-Z].*))(?=.*[0-9].*)[!-~]{6,16}$/,
              message: '密码长度6-16位,且必须包含数字和字母,不能存在空格',
            },
            { required: true },
          ]}
127
        >
128
          <Input placeholder="请输入新密码" autoComplete="off" maxLength="16" />
129 130 131 132
        </Form.Item>
        <Form.Item
          name="passwordConfirm"
          label="确认密码"
133 134 135 136 137 138 139
          rules={[
            {
              pattern: /^(?=.*[a-zA-Z\d])(?=.*([a-zA-Z].*))(?=.*[0-9].*)[!-~]{6,16}$/,
              message: '密码长度6-16位,且必须包含数字和字母,不能存在空格',
            },
            { required: true },
          ]}
140
        >
141
          <Input placeholder="再次确认新密码" autoComplete="off" maxLength="16" />
142 143 144 145 146 147 148
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default ChangePasswordModal;