messageConfig.jsx 9.03 KB
Newer Older
1 2 3 4 5 6 7 8
import React, { useEffect, useState } from 'react';
import { Card, Form, Input, Button, Select, message, Divider, Spin, Row, Col } from 'antd';
import {
  GetMessageConfigInfo,
  SaveSystemInfo,
  ConnectMessPlatform,
  GetBasicInfo,
  GetDataBaseConfig,
邓超's avatar
邓超 committed
9
} from '@/services/hostmanager/hostmanager';
10 11
import { GetMessageVersion } from '@/services/messagemanage/messagemanage';
import { CloseCircleFilled } from '@ant-design/icons';
皮倩雯's avatar
皮倩雯 committed
12
import CryptoJS from 'crypto-js';
13
import message11 from '../../../../assets/images/icons/消息.svg';
14
import Yes from '../../../../assets/images/icons/正确.svg';
15
import styles from './messageConfig.less';
16 17

const layout = {
18 19
  labelCol: { span: 2 },
  wrapperCol: { span: 21 },
20 21
};
const tailLayout = {
22
  wrapperCol: { offset: 11, span: 13 },
23 24
};
const MessageConfig = () => {
25 26 27 28 29 30
  const [loading, setLoading] = useState(false); // 加载
  const [currentAddress, setCurrentAddress] = useState('');
  const [currentDataBase, setCurrentDataBase] = useState({});
  const [currentSiteInfo, setcurrentSiteInfo] = useState('');
  const [show1, setShow1] = useState('none');
  const [show2, setShow2] = useState('none');
31 32
  const [messageVersion, setMessageVersion] = useState('');
  const [flag, setFlag] = useState(0);
33 34

  const [form] = Form.useForm();
皮倩雯's avatar
皮倩雯 committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

  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();
  };
62
  const onFinish = values => {
63 64
    // 先测试连接再保存
    // 1.测试链接
65 66 67 68 69
    setLoading(true);
    ConnectMessPlatform({
      messAddress: values.messageAddress,
      sqlServerIP: currentDataBase.ip,
      loginName: currentDataBase.userName,
皮倩雯's avatar
皮倩雯 committed
70
      password: Encrypt(currentDataBase.password),
71 72 73 74 75 76 77
      sqlName: currentDataBase.dbName,
      siteCode: currentSiteInfo,
    }).then(res => {
      setLoading(false);
      if (res.code === 0) {
        setShow1('block');
        setShow2('none');
78
        // 2.保存连接
79 80 81
        SaveSystemInfo({
          configName: '消息平台连接地址',
          configValue: values.messageAddress,
82 83
        }).then(resdata => {
          if (resdata.code === 0) {
84 85
            message.info('配置保存成功');
          } else {
86
            message.error(resdata.msg);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
          }
        });
      } else {
        setShow1('none');
        setShow2('block');
        message.error(res.msg);
      }
    });
  };

  const getMessageConfig = (CurrentAddress, CurrentDataBase, currentSiteInfo) => {
    setLoading(true);
    ConnectMessPlatform({
      messAddress: CurrentAddress,
      sqlServerIP: CurrentDataBase.ip,
      loginName: CurrentDataBase.userName,
皮倩雯's avatar
皮倩雯 committed
103
      password: Encrypt(CurrentDataBase.password),
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      sqlName: CurrentDataBase.dbName,
      siteCode: currentSiteInfo,
    }).then(res => {
      setLoading(false);
      if (res.code === 0) {
        setShow1('block');
        setShow2('none');
      } else {
        setShow1('none');
        setShow2('block');
        message.error(res.msg);
      }
    });
  };

  const onFinishFailed = errorInfo => {
    console.log('Failed:', errorInfo);
  };

  useEffect(() => {
124
    form.setFieldsValue({ messageAddress: '127.0.0.1:8231' });
125 126 127
    GetMessageConfigInfo().then(res => {
      if (res.code == 0) {
        setCurrentAddress(res.data);
128 129 130 131 132 133
        if (!res.data || res.data == null) {
          form.setFieldsValue({ messageAddress: '127.0.0.1:8231' });
        } else {
          form.setFieldsValue({ messageAddress: res.data });
        }

134 135
        GetDataBaseConfig().then(resdata => {
          if (resdata.code === 0) {
皮倩雯's avatar
皮倩雯 committed
136
            setCurrentDataBase({ ...resdata.data, password: Decrypt(resdata.data.password) });
137 138 139
            GetBasicInfo().then(reslist => {
              if (reslist.code === 0) {
                setcurrentSiteInfo(reslist.data);
140 141 142 143 144 145 146 147 148
                // getMessageConfig(CurrentAddress, CurrentDataBase, currentSiteInfo);
              }
            });
          }
        });
      } else {
        message.info('获取消息平台配置失败!');
      }
    });
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    getMessageVersion();
  }, [flag]);

  const getMessageVersion = () => {
    GetMessageVersion().then(res => {
      console.log(res.data);
      setMessageVersion(res.data);
    });
  };

  const butn = () => {
    console.log(messageVersion);
    let data = '';
    if (messageVersion == '1.0') {
      data = '2.0';
    } else {
      data = '1.0';
    }
    SaveSystemInfo({
      configName: '消息解析规则版本',
      configValue: data,
    }).then(resdata => {
      if (resdata.code === 0) {
        message.info('配置保存成功');
        getMessageVersion();
      } else {
        message.error(resdata.msg);
      }
    });
  };
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

  return (
    <div className={styles.message_container}>
      <Card style={{ width: '100%', height: 'calc(100vh - 130px)' }}>
        <Spin spinning={loading} tip="loading">
          <Form
            form={form}
            name="basic"
            initialValues={{ remember: true }}
            onFinish={onFinish}
            onFinishFailed={onFinishFailed}
          >
            <div
              style={{
                marginTop: '10px',
                display: 'flex',
                alignItems: 'center',
              }}
            >
              <img src={message11} style={{ height: '16px' }} alt="" />
              <span style={{ marginLeft: '10px', fontWeight: 'bold' }}>消息平台</span>
            </div>
            <Divider />
            <Form.Item
203 204 205 206 207 208 209 210 211 212 213 214 215
              style={{ marginLeft: '20px', marginBottom: '0px' }}
              // label="服务地址(平台)"
              // name="messageAddress"
              // rules={[
              //   {
              //     required: true,
              //     pattern: new RegExp(
              //       /^(([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])(:\d*)$/,
              //       'g',
              //     ),
              //     message: '请输入正确的IP例如:192.168.12.231:8231',
              //   },
              // ]}
216 217
            >
              <div style={{ display: 'flex', justifyContent: 'flex-start' }}>
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                <Form.Item
                  label="服务地址(平台)"
                  name="messageAddress"
                  rules={[
                    {
                      required: true,
                      pattern: new RegExp(
                        /^(([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])(:\d*)$/,
                        'g',
                      ),
                      message: '请输入正确的IP例如:192.168.12.231:8231',
                    },
                  ]}
                >
                  <Input
                    style={{ width: '300px', marginLeft: '15px' }}
                    placeholder="请输入服务地址"
                  />
                </Form.Item>
237 238 239
                <span style={{ display: show1 }}>
                  <img
                    src={Yes}
240
                    style={{ height: '24px', marginLeft: '26px', marginTop: '10px' }}
241 242 243 244 245
                    alt=""
                  />
                </span>
                <span style={{ display: show2 }}>
                  <CloseCircleFilled
246
                    style={{ fontSize: '24px', marginLeft: '26px', marginTop: '10px' }}
247 248 249 250 251 252 253 254 255 256
                  />
                </span>
              </div>
            </Form.Item>

            <Form.Item>
              <Button type="primary" htmlType="submit" style={{ marginLeft: '152px' }}>
                保存连接
              </Button>
            </Form.Item>
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
            <Form.Item label="消息版本" style={{ marginLeft: '67px', marginBottom: '0px' }}>
              <span style={{ marginLeft: '15px', color: 'red', fontWeight: 'bold' }}>
                {messageVersion}
              </span>
            </Form.Item>
            <Form.Item>
              <Button
                type="primary"
                style={{ marginLeft: '152px', marginTop: '13px', width: '88px' }}
                onClick={butn}
              >
                {messageVersion == '1.0' ? '升级' : '回退'}
              </Button>
              <br />
              <br />
              <span style={{ color: 'red', fontWeight: 'bold', marginLeft: '152px' }}>
                提示:web4请使用1.0版本,web5请使用2.0版本,请根据项目的实际情况决定
              </span>
            </Form.Item>
276 277 278 279 280 281 282
          </Form>
        </Spin>
      </Card>
    </div>
  );
};
export default MessageConfig;