AddModal.jsx 5.46 KB
Newer Older
1 2
import React, { useState, useEffect } from 'react';
import { Form, Modal, Input, Select, notification } from 'antd';
3
import { AddConnString, EditConnString } from '@/services/database/api';
皮倩雯's avatar
皮倩雯 committed
4
import CryptoJS from 'crypto-js';
5 6 7 8 9 10 11

const { Item } = Form;
const { Option } = Select;
const AddModal = props => {
  const { callBackSubmit = () => {}, type, formObj, visible } = props;
  const [loading, setLoading] = useState(false);
  const [form] = Form.useForm();
皮倩雯's avatar
皮倩雯 committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

  const key = CryptoJS.enc.Utf8.parse('1p2a3n4d5a6o7m8s9a10n1e2t3c4o5re'); // 十六位十六进制数作为密钥
  const iv = CryptoJS.enc.Utf8.parse('1234567890000000');

  // 解密
  const Decrypt = word => {
    let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    let decrypt = CryptoJS.AES.decrypt(srcs, key, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
  };

  // 加密
  const Encrypt = word => {
    let srcs = CryptoJS.enc.Utf8.parse(word);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });
    return encrypted.ciphertext.toString().toUpperCase();
  };
皮倩雯's avatar
皮倩雯 committed
39 40 41 42 43

  const isAlphanumeric = str => {
    return /^[A-Za-z0-9]+$/.test(str);
  };

44 45 46 47 48 49
  // 提交
  const onSubmit = () => {
    form.validateFields().then(validate => {
      if (validate) {
        setLoading(true);
        let obj = form.getFieldsValue();
皮倩雯's avatar
皮倩雯 committed
50 51 52 53
        let pwd = obj.password;
        if (obj.password.length < 32 || !isAlphanumeric(obj.password)) {
          pwd = Encrypt(obj.password);
        }
54
        if (type === 'add') {
皮倩雯's avatar
皮倩雯 committed
55
          AddConnString({
56
            ...obj,
57
            type: 'sqlserver',
皮倩雯's avatar
皮倩雯 committed
58
            password: pwd,
59 60 61
          })
            .then(res => {
              setLoading(false);
62
              if (res.code == 0) {
63 64 65 66 67
                form.resetFields();
                callBackSubmit();
                notification.success({
                  message: '提示',
                  duration: 3,
68
                  description: '新增成功',
69 70 71 72 73
                });
              } else {
                notification.error({
                  message: '提示',
                  duration: 3,
74
                  description: res.msg,
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
                });
              }
            })
            .catch(err => {
              setLoading(false);
              console.error(err);
            });
        } else if (type === 'edit') {
          handleEdit();
        }
      }
    });
  };
  const handleEdit = () => {
    let obj = form.getFieldsValue();
皮倩雯's avatar
皮倩雯 committed
90 91 92 93
    let pwd = obj.password;
    if (obj.password.length < 32 || !isAlphanumeric(obj.password)) {
      pwd = Encrypt(obj.password);
    }
皮倩雯's avatar
皮倩雯 committed
94
    EditConnString({
95 96
      ...obj,
      oldName: formObj.name,
97
      type: 'sqlserver',
皮倩雯's avatar
皮倩雯 committed
98
      password: pwd,
99 100 101
    })
      .then(res => {
        setLoading(false);
102
        if (res.code == 0) {
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
          form.resetFields();
          callBackSubmit();
          notification.success({
            message: '提示',
            duration: 3,
            description: res.message || '编辑成功',
          });
        } else {
          notification.error({
            message: '提示',
            duration: 3,
            description: res.message || '编辑失败',
          });
        }
      })
      .catch(err => setLoading(false));
  };
Maofei94's avatar
Maofei94 committed
120
  const onFinish = value => {};
121 122 123
  useEffect(() => {
    switch (type) {
      case 'add':
124
        form.resetFields();
125 126
        break;
      case 'edit':
皮倩雯's avatar
皮倩雯 committed
127
        form.setFieldsValue({ ...formObj, password: formObj.password });
128 129 130 131 132 133 134 135 136 137 138
        break;
      default:
        break;
    }
  }, [visible]);
  const layout = {
    layout: 'horizontal',
    labelCol: {
      span: 4,
    },
    wrapperCol: {
139
      span: 19,
140 141 142 143
    },
  };
  return (
    <Modal
144
      title={`${type === 'add' ? '添加' : '编辑'}SQL数据库配置连接`}
145 146 147 148 149 150 151 152 153 154 155 156 157
      bodyStyle={{ width: '100%', minHeight: '100px' }}
      style={{ top: '150px' }}
      width="700px"
      destroyOnClose
      maskClosable={false}
      cancelText="取消"
      okText="确认"
      {...props}
      onOk={() => onSubmit()}
      confirmLoading={loading}
    >
      {visible && (
        <Form form={form} {...layout} onFinish={onFinish}>
158
          <Item label="标签" name="name" rules={[{ required: true, message: '请输入标签' }]}>
159 160 161 162 163
            {/* <Select>
            <Option value={item.value}>{item.title}</Option>
          </Select> */}
            <Input placeholder="请输入标签" allowClear />
          </Item>
164
          <Item label="IP" name="ip" rules={[{ required: true, message: '请输入IP' }]}>
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            <Input placeholder="请输入IP" allowClear />
          </Item>
          <Item
            label="数据库名"
            name="dbName"
            rules={[{ required: true, message: '请输入数据库名' }]}
          >
            <Input placeholder="请输入数据库名" allowClear />
          </Item>
          <Item
            label="用户名"
            name="userName"
            rules={[{ required: true, message: '请输入用户名' }]}
          >
            <Input placeholder="请输入用户名" allowClear />
          </Item>
181
          <Item label="密码" name="password" rules={[{ required: true, message: '请输入密码' }]}>
邓超's avatar
邓超 committed
182
            <Input.Password placeholder="请输入密码" allowClear />
183 184 185 186 187 188 189
          </Item>
        </Form>
      )}
    </Modal>
  );
};
export default AddModal;