InitDataBase.jsx 16.7 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1 2
/* eslint-disable spaced-comment */
/* eslint-disable eqeqeq */
3
import React, { useEffect, useRef, useState } from 'react';
张烨's avatar
张烨 committed
4 5 6 7 8 9 10 11 12
import {
  Card,
  Form,
  Input,
  Button,
  Select,
  Table,
  Space,
  notification,
13 14
  Modal,
  Row,
张烨's avatar
张烨 committed
15
  Col,
Maofei94's avatar
Maofei94 committed
16
  Popconfirm,
17
  Spin,
18 19
  Tabs,
  Radio,
张烨's avatar
张烨 committed
20
} from 'antd';
21
import PageContainer from '@/components/BasePageContainer';
22
import RadioBox from '@/components/RadioGroup';
23
import { connect } from 'react-redux';
Maofei94's avatar
Maofei94 committed
24 25
import {
  setTableSQLDirName,
26 27 28 29 30 31
  deleteConnNew,
  initDBv4new,
  getInitDBLogNew,
  getConnRecordNew,
  getDataBaseConfigNew,
  saveConnectionNew,
Maofei94's avatar
Maofei94 committed
32
  getDataBaseList,
33 34
  updateConnDescNew,
  deleteInitDBLogNew,
Maofei94's avatar
Maofei94 committed
35
  connectionTest,
36 37 38
  GetProductList, // 获取产品列表
  GetDbProduct, // 获取产品方案配置
  InitAddDataBase, // 数据库初始化
39
  InitEditDataBase, // 二次初始化
Maofei94's avatar
Maofei94 committed
40
} from '@/services/database/api';
皮倩雯's avatar
皮倩雯 committed
41
import CryptoJS from 'crypto-js';
张烨's avatar
张烨 committed
42
import styles from './InitDataBase.less';
皮倩雯's avatar
皮倩雯 committed
43
import InitModal from './initModal';
44

45
const { TabPane } = Tabs;
张烨's avatar
张烨 committed
46
const { Option } = Select;
赵吉's avatar
赵吉 committed
47
const formLables = {
张烨's avatar
张烨 committed
48 49 50 51 52
  ip: '服务器名或IP地址',
  userName: '数据库用户名称',
  password: '数据库用户密码',
  dbName: '数据库名称',
};
53
let time = null;
张烨's avatar
张烨 committed
54 55
const InitDataBase = props => {
  const [form] = Form.useForm();
张烨's avatar
张烨 committed
56
  const [tableLoading, setTableLoading] = useState(false); // 连接记录
张烨's avatar
张烨 committed
57 58 59 60 61
  const [dbForm, setDbForm] = useState({
    ip: '',
    dbName: '',
    password: '',
    userName: '',
皮倩雯's avatar
皮倩雯 committed
62
    providerType: '',
张烨's avatar
张烨 committed
63 64
    inUse: '',
  });
张烨's avatar
张烨 committed
65 66 67
  const [data, setData] = useState([]); // 数据库链接记录
  const [option, setOption] = useState([]); // 下拉列表数据
  const [desc, setDesc] = useState(''); // 修改描述
68 69 70 71 72
  const [modalVisible, setModalVisible] = useState({
    describeVisible: false, // 描述弹窗
    versionVisible: false, // 检查版本弹窗
    initVisible: false, // 初始化选择产品弹窗
  }); // 修改弹窗
皮倩雯's avatar
皮倩雯 committed
73

74
  const [cardLoading, setCardLoading] = useState(false); // 初始化card Loading
皮倩雯's avatar
皮倩雯 committed
75 76
  const [flag, setFlag] = useState(0);
  const [keepData, setKeepData] = useState({});
77
  const chooseDb = useRef({});
皮倩雯's avatar
皮倩雯 committed
78 79
  const key = CryptoJS.enc.Utf8.parse('1p2a3n4d5a6o7m8s9a10n1e2t3c4o5re'); //十六位十六进制数作为密钥
  const iv = CryptoJS.enc.Utf8.parse('1234567890000000');
皮倩雯's avatar
皮倩雯 committed
80
  const [pickItem, setPickItem] = useState();
81 82

  // 获取数据库配置信息
张烨's avatar
张烨 committed
83
  useEffect(() => {
84 85 86 87 88 89
    setCardLoading(true);
    // 数据库连接记录初始化
    getConnRecordData();
    getDataBaseConfigNew().then(resnew => {
      setCardLoading(false);
      let res = resnew.data;
90
      if (resnew.code === 0) {
91 92 93 94 95
        const { inUse } = res;

        let obj = {};
        Object.keys(dbForm).forEach(k => {
          obj[k] = res[k];
96
        });
97 98
        form.setFieldsValue({ ...obj });
        setDbForm(val => ({ ...val, ...obj }));
99 100
      }
    });
101 102
    return () => {
      if (time) {
103 104
        clearTimeout(time);
        time = null;
105
      }
106 107
    };
  }, []);
皮倩雯's avatar
皮倩雯 committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  // 解密
  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();
  };

133 134 135 136
  // 弹出模态框
  const handleShowModal = (key, value) => {
    setModalVisible({ ...modalVisible, [key]: value });
  };
137 138 139 140 141 142
  // 获取数据库连接记录
  const getConnRecordData = () => {
    setTableLoading(true);
    getConnRecordNew()
      .then(resnew => {
        setTableLoading(false);
143
        if (resnew.code === 0) {
144 145 146 147
          let res = resnew.data;
          let arr = res.map((item, index) => {
            item.key = index;
            return item;
张烨's avatar
张烨 committed
148
          });
149
          setData(arr);
张烨's avatar
张烨 committed
150
        }
151 152 153
      })
      .catch(err => {
        setTableLoading(false);
tianfen's avatar
tianfen committed
154
      });
Maofei94's avatar
Maofei94 committed
155
  };
皮倩雯's avatar
皮倩雯 committed
156

张烨's avatar
张烨 committed
157 158 159 160
  const onValuesChange = (value, b) => {
    form.setFieldsValue(value);
  };
  const onChange = value => {
161
    const { length } = value;
张烨's avatar
张烨 committed
162
    form.setFieldsValue({
163
      dbName: value[length - 1],
张烨's avatar
张烨 committed
164 165
    });
  };
张烨's avatar
张烨 committed
166
  // 保存连接
167
  const onFinish = values => {
168
    setCardLoading(true);
张烨's avatar
张烨 committed
169
    const obj = values;
170 171 172 173
    let pwd =
      Decrypt(obj.password) === '' || Decrypt(obj.password).indexOf('\u0004') !== -1
        ? Encrypt(obj.password)
        : obj.password;
174
    saveConnectionNew({
张烨's avatar
张烨 committed
175 176 177
      ip: obj.ip,
      dbName: obj.dbName,
      userName: obj.userName,
178
      password: pwd,
皮倩雯's avatar
皮倩雯 committed
179
      providerType: obj.providerType,
180
      desc: chooseDb.current.desc,
181 182
    })
      .then(resnew => {
183
        setCardLoading(false);
184
        if (resnew.code === 0) {
皮倩雯's avatar
皮倩雯 committed
185 186 187 188 189
          setFlag(flag + 1);
          setKeepData({
            ip: obj.ip,
            dbName: obj.dbName,
            userName: obj.userName,
190
            password: pwd,
皮倩雯's avatar
皮倩雯 committed
191
          });
192 193
          // setUpData(upData + 1);
          getConnRecordData();
张烨's avatar
张烨 committed
194
          notification.success({
Maofei94's avatar
Maofei94 committed
195
            message: '提示',
张烨's avatar
张烨 committed
196 197 198 199 200 201
            duration: 3,
            description: '保存成功',
          });
        } else {
          notification.error({
            message: '提示',
Maofei94's avatar
Maofei94 committed
202
            duration: 15,
203
            description: resnew.msg,
张烨's avatar
张烨 committed
204 205
          });
        }
206 207 208
      })
      .catch(err => {
        setCardLoading(false);
皮倩雯's avatar
皮倩雯 committed
209 210 211 212 213
        notification.error({
          message: '提示',
          duration: 15,
          description: err,
        });
214
      });
215
  };
216
  // 测试连接
张烨's avatar
张烨 committed
217
  const onCheck = e => {
218
    setCardLoading(true);
张烨's avatar
张烨 committed
219
    const obj = form.getFieldsValue();
220 221 222 223
    let pwd =
      Decrypt(obj.password) === '' || Decrypt(obj.password).indexOf('\u0004') !== -1
        ? Encrypt(obj.password)
        : obj.password;
224 225 226 227
    connectionTest({
      ip: obj.ip,
      dbName: obj.dbName,
      userName: obj.userName,
皮倩雯's avatar
皮倩雯 committed
228
      providerType: obj.providerType,
229
      password: pwd,
张烨's avatar
张烨 committed
230 231
    })
      .then(res => {
232
        setCardLoading(false);
皮倩雯's avatar
皮倩雯 committed
233
        if (res.code == 0 && res.data == true) {
张烨's avatar
张烨 committed
234
          notification.success({
Maofei94's avatar
Maofei94 committed
235
            message: '提示',
张烨's avatar
张烨 committed
236 237 238 239 240 241
            duration: 3,
            description: '连接成功',
          });
        } else {
          notification.error({
            message: '提示',
Maofei94's avatar
Maofei94 committed
242
            duration: 15,
皮倩雯's avatar
皮倩雯 committed
243
            description: res.msg,
张烨's avatar
张烨 committed
244 245 246 247
          });
        }
      })
      .catch(err => {
248
        setCardLoading(false);
张烨's avatar
张烨 committed
249
        console.log(err);
250
      });
张烨's avatar
张烨 committed
251
  };
Maofei94's avatar
Maofei94 committed
252
  // 获取数据库列表
张烨's avatar
张烨 committed
253 254
  const selectFocus = e => {
    let params = form.getFieldsValue();
255 256 257 258
    let pwd =
      Decrypt(params.password) === '' || Decrypt(params.password).indexOf('\u0004') !== -1
        ? Encrypt(params.password)
        : params.password;
Maofei94's avatar
Maofei94 committed
259
    getDataBaseList({
张烨's avatar
张烨 committed
260
      userName: params.userName || '',
261
      password: pwd || '',
张烨's avatar
张烨 committed
262
      ip: params.ip || '',
皮倩雯's avatar
皮倩雯 committed
263
      providerType: params.providerType || '',
张烨's avatar
张烨 committed
264 265
    })
      .then(res => {
皮倩雯's avatar
皮倩雯 committed
266 267
        if (res.code == 0) {
          setOption(res.data.root);
张烨's avatar
张烨 committed
268 269
        } else {
          notification.error({
Maofei94's avatar
Maofei94 committed
270
            message: '提示',
Maofei94's avatar
Maofei94 committed
271
            duration: 15,
皮倩雯's avatar
皮倩雯 committed
272
            description: res.msg,
张烨's avatar
张烨 committed
273 274 275 276 277 278 279 280
          });
          setOption([]);
        }
      })
      .catch(err => {
        console.error(err);
      });
  };
张烨's avatar
张烨 committed
281
  // 点击表格回显到表单
张烨's avatar
张烨 committed
282 283
  const tableClick = item => {
    let obj = { ...dbForm };
张烨's avatar
张烨 committed
284
    Object.keys(obj).forEach(k => {
张烨's avatar
张烨 committed
285
      obj[k] = item[k];
张烨's avatar
张烨 committed
286
    });
287
    chooseDb.current = item;
288
    form.setFieldsValue({ ...obj });
张烨's avatar
张烨 committed
289
  };
皮倩雯's avatar
皮倩雯 committed
290

张烨's avatar
张烨 committed
291
  // 展示修改描述
Maofei94's avatar
Maofei94 committed
292 293
  const changeDesc = val => {
    setDesc(val);
294
    handleShowModal('describeVisible', true);
张烨's avatar
张烨 committed
295 296 297 298 299
  };
  const descChange = e => {
    const { value } = e.target;
    setDesc(value);
  };
皮倩雯's avatar
皮倩雯 committed
300

张烨's avatar
张烨 committed
301 302 303
  // 弹窗确认回调
  const modalOkCallback = () => {
    // 更新描述
304
    updateConnDescNew({
皮倩雯's avatar
皮倩雯 committed
305 306 307 308 309
      ip: pickItem.ip,
      dbName: pickItem.dbName,
      userName: pickItem.userName,
      password: pickItem.password,
      providerType: pickItem.providerType,
张烨's avatar
张烨 committed
310
      desc,
311
    })
312 313
      .then(res => {
        if (res.code === 0) {
皮倩雯's avatar
皮倩雯 committed
314
          handleShowModal('describeVisible', false);
315
          getConnRecordData();
Maofei94's avatar
Maofei94 committed
316 317 318
        } else {
          notification.error({
            message: '提示',
皮倩雯's avatar
皮倩雯 committed
319
            duration: 5,
320
            description: res.msg,
Maofei94's avatar
Maofei94 committed
321 322 323 324
          });
        }
      })
      .catch(err => {
皮倩雯's avatar
皮倩雯 committed
325
        handleShowModal('describeVisible', false);
Maofei94's avatar
Maofei94 committed
326 327
      });
  };
皮倩雯's avatar
皮倩雯 committed
328

张烨's avatar
张烨 committed
329 330 331 332 333
  const columns = [
    {
      title: '服务器名或IP地址',
      dataIndex: 'ip',
      key: 'ip',
Maofei94's avatar
Maofei94 committed
334 335
      width: 180,
      ellipsis: true,
皮倩雯's avatar
皮倩雯 committed
336
      align: 'center',
张烨's avatar
张烨 committed
337 338 339 340 341
    },
    {
      title: '数据库名称',
      dataIndex: 'dbName',
      key: 'dbName',
Maofei94's avatar
Maofei94 committed
342 343
      width: 180,
      ellipsis: true,
皮倩雯's avatar
皮倩雯 committed
344
      align: 'center',
张烨's avatar
张烨 committed
345 346 347 348 349
    },
    {
      title: '数据库用户名称',
      dataIndex: 'userName',
      key: 'userName',
Maofei94's avatar
Maofei94 committed
350 351
      width: 180,
      ellipsis: true,
皮倩雯's avatar
皮倩雯 committed
352
      align: 'center',
张烨's avatar
张烨 committed
353 354 355 356 357
    },
    {
      title: '保存时间',
      dataIndex: 'saveTime',
      key: 'saveTime',
Maofei94's avatar
Maofei94 committed
358 359
      width: 180,
      ellipsis: true,
皮倩雯's avatar
皮倩雯 committed
360 361 362 363 364 365 366 367 368
      align: 'center',
    },
    {
      title: '数据库类型',
      dataIndex: 'providerType',
      key: 'providerType',
      width: 180,
      ellipsis: true,
      align: 'center',
张烨's avatar
张烨 committed
369 370 371 372 373
    },
    {
      title: '描述',
      dataIndex: 'desc',
      key: 'desc',
Maofei94's avatar
Maofei94 committed
374
      ellipsis: true,
皮倩雯's avatar
皮倩雯 committed
375
      align: 'center',
张烨's avatar
张烨 committed
376 377
    },
    {
Maofei94's avatar
Maofei94 committed
378 379 380
      title: '操作',
      dataIndex: 'action',
      key: 'action',
Maofei94's avatar
Maofei94 committed
381 382
      width: 250,
      ellipsis: true,
皮倩雯's avatar
皮倩雯 committed
383
      align: 'center',
Maofei94's avatar
Maofei94 committed
384 385 386 387 388
      render: (text, record) => (
        <Space>
          <Button
            type="primary"
            size="small"
皮倩雯's avatar
皮倩雯 committed
389 390
            onClick={e => {
              e.stopPropagation();
Maofei94's avatar
Maofei94 committed
391
              changeDesc(record.desc);
皮倩雯's avatar
皮倩雯 committed
392
              setPickItem(record);
Maofei94's avatar
Maofei94 committed
393 394 395 396 397
            }}
          >
            修改描述
          </Button>
        </Space>
张烨's avatar
张烨 committed
398
      ),
张烨's avatar
张烨 committed
399 400
    },
  ];
皮倩雯's avatar
皮倩雯 committed
401

张烨's avatar
张烨 committed
402 403
  return (
    <>
404
      <PageContainer className={styles.InitDataBaseContainer}>
皮倩雯's avatar
皮倩雯 committed
405
        <Card className={styles.cardTop}>
406
          <Spin tip="loading..." spinning={cardLoading}>
407
            {/* <div className={styles.tableTitle}>数据库初始化</div> */}
408
            <Form
皮倩雯's avatar
皮倩雯 committed
409
              className={styles.cardBottom}
410 411 412 413 414 415
              layout="horizontal"
              labelAlign="left"
              labelCol={{ span: 3 }}
              form={form}
              onFinish={onFinish}
              onValuesChange={onValuesChange}
416
              autoComplete="off"
417
            >
皮倩雯's avatar
皮倩雯 committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
              <Form.Item
                label="数据库类型"
                name="providerType"
                rules={[
                  {
                    validator: (rule, value) => {
                      if (form.getFieldValue().providerType == '') {
                        return Promise.reject('ip必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
                <Select placeholder="请选择数据库类型">
                  <Select.Option value="SqlServer">SqlServer</Select.Option>
                  <Select.Option value="Dm">Dm</Select.Option>
                </Select>
              </Form.Item>
皮倩雯's avatar
皮倩雯 committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450
              <Form.Item
                label={`${formLables.ip}:`}
                name="ip"
                rules={[
                  {
                    validator: (rule, value) => {
                      if (form.getFieldValue().ip == '') {
                        return Promise.reject('ip必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
451 452
                <Input placeholder="请输入服务器名或IP地址" />
              </Form.Item>
皮倩雯's avatar
皮倩雯 committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466
              <Form.Item
                label={`${formLables.userName}:`}
                name="userName"
                rules={[
                  {
                    validator: (rule, value) => {
                      if (form.getFieldValue().userName == '') {
                        return Promise.reject('用户名称必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
467
                <Input placeholder="请输入用户名称" autoComplete="off" />
468
              </Form.Item>
皮倩雯's avatar
皮倩雯 committed
469
              <Form.Item
470
                label="数据库用户密码"
皮倩雯's avatar
皮倩雯 committed
471 472 473 474 475 476 477 478 479 480 481 482 483
                name="password"
                rules={[
                  {
                    validator: (rule, value) => {
                      console.log(form.getFieldValue().password);
                      if (form.getFieldValue().password == '') {
                        return Promise.reject('用户密码必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
484 485 486 487 488 489
                <Input
                  placeholder="请输入用户密码"
                  type="password"
                  autoComplete="new-password"
                  id="pwd"
                />
490
              </Form.Item>
皮倩雯's avatar
皮倩雯 committed
491
              <Form.Item
492
                label="数据库名称"
皮倩雯's avatar
皮倩雯 committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
                name="dbName"
                rules={[
                  {
                    validator: (rule, value) => {
                      if (
                        form.getFieldValue().dbName == '' ||
                        form.getFieldValue().dbName == undefined
                      ) {
                        return Promise.reject('数据库名称必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
                <Select
                  showSearch
                  mode="tags"
                  placeholder="请选择数据库"
                  optionFilterProp="children"
                  onFocus={() => {
                    selectFocus();
                  }}
                  onChange={e => {
                    onChange(e);
                  }}
                  // eslint-disable-next-line no-shadow
                  filterOption={(input, option) => {
                    return (
                      option.children &&
523
                      option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
524 525 526 527 528 529 530 531 532 533 534 535
                    );
                  }}
                >
                  {option &&
                    option.length > 0 &&
                    option.map((item, index) => (
                      <Option value={item.value} key={item.value + index}>
                        {item.value}
                      </Option>
                    ))}
                </Select>
              </Form.Item>
Maofei94's avatar
Maofei94 committed
536
              <Form.Item wrapperCol={{ offset: 3 }}>
537 538 539 540
                <div className={styles.tCenter}>
                  <Space size="large" className={styles.btnBox}>
                    <Space>
                      <Button onClick={onCheck}>测试连接</Button>
541
                      <Button type="primary" htmlType="submit" loading={tableLoading}>
542 543 544
                        保存连接
                      </Button>
                    </Space>
Maofei94's avatar
Maofei94 committed
545
                  </Space>
546 547 548 549
                </div>
              </Form.Item>
            </Form>
          </Spin>
张烨's avatar
张烨 committed
550 551
        </Card>

皮倩雯's avatar
皮倩雯 committed
552
        <Card className={styles.cardBottom}>
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
          <div className={styles.recordBox}>
            <div className={styles.tableTitle}>近期保存的数据库连接</div>
            <Table
              style={{ marginTop: '20px', marginBottom: '20px' }}
              scroll={{ x: 'max-content', y: 'calc(100vh - 550px)' }}
              columns={columns}
              dataSource={data}
              pagination={false}
              bordered
              loading={tableLoading}
              size="small"
              onRow={record => ({
                onClick: () => {
                  tableClick(record);
                  setFlag(0);
                }, // 点击行
              })}
            />
          </div>
张烨's avatar
张烨 committed
572
        </Card>
573
        <Modal
张烨's avatar
张烨 committed
574
          title="修改链接描述"
575
          visible={modalVisible.describeVisible}
张烨's avatar
张烨 committed
576
          onOk={() => modalOkCallback()}
577
          onCancel={() => handleShowModal('describeVisible', false)}
578
          width="800px"
579
          bodyStyle={{
张烨's avatar
张烨 committed
580
            minHeight: '100px',
581
          }}
张烨's avatar
张烨 committed
582
          cancelText="取消"
Maofei94's avatar
Maofei94 committed
583
          okText="确认"
Maofei94's avatar
Maofei94 committed
584
          destroyOnClose
585
        >
张烨's avatar
张烨 committed
586
          <Row>
587
            <Col span={2} className={styles.decsBox}>
Maofei94's avatar
Maofei94 committed
588
              描述:
张烨's avatar
张烨 committed
589
            </Col>
Maofei94's avatar
Maofei94 committed
590

591
            <Col span={22}>
张烨's avatar
张烨 committed
592 593
              <Input
                placeholder="请输入描述"
Maofei94's avatar
Maofei94 committed
594 595
                allowClear
                defaultValue={desc}
张烨's avatar
张烨 committed
596 597 598 599 600
                onChange={value => {
                  descChange(value);
                }}
              />
            </Col>
601
          </Row>
602
        </Modal>
张烨's avatar
张烨 committed
603 604 605 606
      </PageContainer>
    </>
  );
};
607

张烨's avatar
张烨 committed
608
export default connect()(InitDataBase);