AppendModal.jsx 13.4 KB
Newer Older
1
/* eslint-disable indent */
皮倩雯's avatar
皮倩雯 committed
2 3 4
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
import React, { useEffect, useState } from 'react';
皮倩雯's avatar
皮倩雯 committed
5
import { Button, Space, notification, Modal, Popconfirm, Empty, Spin, Tooltip } from 'antd';
6 7 8 9 10 11 12
import {
  BarcodeOutlined,
  UserOutlined,
  CalendarOutlined,
  PlusOutlined,
  CheckOutlined,
} from '@ant-design/icons';
皮倩雯's avatar
皮倩雯 committed
13
import CryptoJS from 'crypto-js';
14
import styles from './DatabaseInitialization.less';
皮倩雯's avatar
皮倩雯 committed
15
import { GetLicenseDifference, InitEditDataBase, GetProductList } from '@/services/database/api';
16
import New from '../../../assets/images/icons/new.png';
皮倩雯's avatar
皮倩雯 committed
17 18

const AppendModal = props => {
19
  const { callBackSubmit = () => {}, visible, onCancel, value, keepHistroy, appendType } = props;
皮倩雯's avatar
皮倩雯 committed
20 21 22 23
  const [data, setData] = useState([]);
  const [allLength, setAllLength] = useState('');
  const [flag, setFlag] = useState();
  const [loading, setLoading] = useState(false);
皮倩雯's avatar
皮倩雯 committed
24 25 26 27 28 29 30 31 32
  const [code, setCode] = useState('');
  const [project, setProject] = useState('');
  const [name, setName] = useState('');
  const [number, setNumber] = useState('');
  const [much, setMuch] = useState([]);
  const [code1, setCode1] = useState('');
  const [project1, setProject1] = useState('');
  const [name1, setName1] = useState('');
  const [number1, setNumber1] = useState('');
33
  const [allValue, setAllValue] = useState([]);
34
  const [keepData, setKeepData] = useState([]);
皮倩雯's avatar
皮倩雯 committed
35

皮倩雯's avatar
皮倩雯 committed
36 37 38
  const key = CryptoJS.enc.Utf8.parse('1p2a3n4d5a6o7m8s9a10n1e2t3c4o5re'); // 十六位十六进制数作为密钥
  const iv = CryptoJS.enc.Utf8.parse('1234567890000000');

皮倩雯's avatar
皮倩雯 committed
39 40 41
  useEffect(() => {
    if (visible) {
      getProduct();
皮倩雯's avatar
皮倩雯 committed
42 43
      let aa = [];
      keepHistroy.map((i, j) => {
44
        if (i.license !== '已使用' && j !== keepHistroy.length - 1) {
皮倩雯's avatar
皮倩雯 committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
          aa.push(i);
        }
      });
      if (aa.length > 0) {
        let a = [];
        let b = [];
        let c = [];
        aa.map(k => {
          a.push(k.license);
          b.push(k.projectName);
          c.push(`${k.applicantName}${k.jobNumber})`);
        });
        setCode1(a.toString());
        setProject1(b.toString());
        setName1(c.toString());
      }
      setMuch(aa);
      setCode(keepHistroy[keepHistroy.length - 1].license);
      setProject(keepHistroy[keepHistroy.length - 1].projectName);
      setName(keepHistroy[keepHistroy.length - 1].applicantName);
      setNumber(keepHistroy[keepHistroy.length - 1].jobNumber);
皮倩雯's avatar
皮倩雯 committed
66 67 68 69 70 71 72
    } else {
      setData([]);
      setFlag();
      setAllLength('');
    }
  }, [visible]);

皮倩雯's avatar
皮倩雯 committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  // 解密
  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
97 98
  const getProduct = () => {
    setLoading(true);
皮倩雯's avatar
皮倩雯 committed
99
    GetLicenseDifference({ ...value, password: Encrypt(value.password) }).then(res => {
皮倩雯's avatar
皮倩雯 committed
100 101
      setLoading(false);
      if (res.code === 0) {
102
        setKeepData(res.data);
103 104 105 106 107 108 109
        let aab = [];
        res.data.map(k => {
          if (k.status == 0) {
            aab.push(k.name);
          }
        });
        setAllValue(aab);
皮倩雯's avatar
皮倩雯 committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 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
        let arr = formateArrDataA(res.data, 'productName');
        let aa = Object.keys(arr);
        aa.map(i => {
          let list = [];
          arr[i].map(j => {
            list.push(j.name);
          });
          arr[i] = list;
        });
        setData(arr);
        if (res.data.length == 0) {
          setFlag(1);
        } else {
          setFlag(0);
        }
        setAllLength(res.data.length);
      } else {
        notification.error({
          message: '提示',
          duration: 3,
          description: res.msg,
        });
      }
    });
  };

  const formateArrDataA = (initialArr, name) => {
    // 判定传参是否符合规则
    if (!(initialArr instanceof Array)) {
      return '请传入正确格式的数组';
    }
    if (!name) {
      return '请传入对象属性';
    }
    // 先获取一下这个数组中有多少个"name"
    let nameArr = [];
    for (let i in initialArr) {
      if (nameArr.indexOf(initialArr[i][`${name}`]) === -1) {
        nameArr.push(initialArr[i][`${name}`]);
      }
    }
    // 新建一个包含多个list的结果对象
    let tempObj = {};
    // 根据不同的"name"生成多个数组
    for (let k in nameArr) {
      for (let j in initialArr) {
        if (initialArr[j][`${name}`] == nameArr[k]) {
          // 每次外循环时新建一个对应"name"的数组, 内循环时当前数组不变
          tempObj[nameArr[k]] = tempObj[nameArr[k]] || [];
          tempObj[nameArr[k]].push(initialArr[j]);
        }
      }
    }
    for (let keys in tempObj) {
      let arr = [];
      tempObj[keys].map((item, index) => {
        tempObj[keys] = arr;
        item.key = index;
        arr.push(item);
      });
    }
    return tempObj;
  };

  const Submit = () => {
皮倩雯's avatar
皮倩雯 committed
175 176 177
    if (allValue.length > 0) {
      callBackSubmit();
    }
皮倩雯's avatar
皮倩雯 committed
178 179
  };

180 181 182 183 184 185 186 187 188 189
  const title = e => {
    let aa = [];
    data[e].map(i => {
      if (allValue.indexOf(i) != -1) {
        aa.push(i);
      }
    });
    return aa.length;
  };

皮倩雯's avatar
皮倩雯 committed
190 191 192 193
  const title1 = () => {
    if (allValue.length == 0) {
      return <span style={{ color: 'red' }}>license下产品已全被初始化,无需再次追加!!!</span>;
    }
194 195 196 197 198 199 200 201 202 203 204
    return (
      <span style={{ color: 'rgb(24 144 255)' }}>
        {appendType === 'yes' ? (
          <div style={{ width: '150px' }}>
            当前数据库为非授权初始化数据库,是否使用授权码升级到授权码模式?
          </div>
        ) : (
          '是否确认追加!'
        )}
      </span>
    );
皮倩雯's avatar
皮倩雯 committed
205 206
  };

皮倩雯's avatar
皮倩雯 committed
207 208 209
  return (
    <Modal
      title="产品追加"
皮倩雯's avatar
皮倩雯 committed
210
      width="1000px"
皮倩雯's avatar
皮倩雯 committed
211 212 213 214 215 216 217 218 219 220 221 222 223
      visible={visible}
      onCancel={onCancel}
      destroyOnClose
      maskClosable={false}
      bodyStyle={{
        height: '500px',
        overflowY: 'scroll',
      }}
      footer={
        <Space>
          <Button onClick={onCancel}>取消</Button>
          <Popconfirm
            placement="topLeft"
皮倩雯's avatar
皮倩雯 committed
224
            title={title1}
皮倩雯's avatar
皮倩雯 committed
225 226 227 228 229 230 231 232 233 234 235 236
            okText="确认"
            cancelText="取消"
            onConfirm={() => {
              Submit();
            }}
          >
            <Button type="primary">追加</Button>
          </Popconfirm>
        </Space>
      }
    >
      <Spin spinning={loading}>
237
        {flag === 1 ? (
皮倩雯's avatar
皮倩雯 committed
238 239 240 241 242 243 244
          <Empty
            image={Empty.PRESENTED_IMAGE_SIMPLE}
            description={<span>当前授权码下产品已全部初始化,无需再次追加</span>}
          />
        ) : (
          <>
            {allLength > 0 && (
皮倩雯's avatar
皮倩雯 committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
              <div
                style={{
                  height: '50px',
                  display: 'flex',
                  justifyContent: 'space-between',
                  alignItems: 'center',
                  marginBottom: '15px',
                }}
              >
                {much.length > 0 ? (
                  <Tooltip title={code1}>
                    <div style={{ display: 'inline-block', backgroundColor: 'aliceblue' }}>
                      <BarcodeOutlined style={{ color: 'rgb(227 168 96)', marginRight: '5px' }} />
                      授权码:
                      {code ? <>{code}</> : <span>--</span>}
                      <span>...</span>
                    </div>
                  </Tooltip>
                ) : (
                  <div style={{ display: 'inline-block', backgroundColor: 'aliceblue' }}>
                    <BarcodeOutlined style={{ color: 'rgb(227 168 96)', marginRight: '5px' }} />
                    授权码:
                    {code ? <>{code}</> : <span>--</span>}
                  </div>
                )}
                {much.length > 0 ? (
                  <Tooltip title={project1}>
                    <div style={{ display: 'inline-block', backgroundColor: 'aliceblue' }}>
                      <CalendarOutlined style={{ color: '#8466cb', marginRight: '5px' }} />
                      项目名:{project ? <>{project}</> : '--'}
                      <span>...</span>
                    </div>
                  </Tooltip>
                ) : (
                  <div style={{ display: 'inline-block', backgroundColor: 'aliceblue' }}>
                    <CalendarOutlined style={{ color: '#8466cb', marginRight: '5px' }} />
                    项目名:{project ? <>{project}</> : '--'}
                  </div>
                )}
                {much.length > 0 ? (
                  <Tooltip title={name1}>
                    <div style={{ display: 'inline-block', backgroundColor: 'aliceblue' }}>
                      <UserOutlined style={{ color: 'green', marginRight: '5px' }} />
                      授权人:{name ? <>{name}</> : '-'}
                      {number ? <>{number}</> : '-'}
                      {much.length > 0 ? <span>...</span> : <></>}
                    </div>
                  </Tooltip>
                ) : (
                  <div style={{ display: 'inline-block', backgroundColor: 'aliceblue' }}>
                    <UserOutlined style={{ color: 'green', marginRight: '5px' }} />
                    授权人:{name ? <>{name}</> : '-'}
                    {number ? <>{number}</> : '-'}
                  </div>
                )}
                <div style={{ display: 'inline-block', backgroundColor: 'aliceblue' }}>
                  <div style={{ float: 'right' }}>
                    <strong>
303 304 305 306 307 308
                      追加产品列表(共
                      <span style={{ color: 'rgb(24 144 255)' }}>
                        {allValue && allValue.length > 0 ? allValue.length : 0}
                      </span>
                      <span>/</span>
                      <span style={{ color: 'rgb(24 144 255)' }}>{allLength}</span>
皮倩雯's avatar
皮倩雯 committed
309 310 311 312
                      个)
                    </strong>
                  </div>
                </div>
皮倩雯's avatar
皮倩雯 committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
              </div>
            )}
            {data &&
              Object.keys(data).map((i, j) => (
                <div
                  style={{
                    border: '2px solid #c2cdfd',
                    borderRadius: '5px',
                    marginBottom: '20px',
                    position: 'relative',
                    paddingTop: '30px',
                  }}
                >
                  <div
                    style={{
                      position: 'absolute',
                      left: '13px',
                      top: '-12px',
                      backgroundColor: 'white',
                      paddingLeft: '10px',
                      paddingRight: '10px',
                    }}
                  >
                    <>
                      <span>{i}</span>
                      <span>
339 340 341 342
                        (共
                        <span style={{ color: 'rgb(24, 144, 255)' }}>{title(i)}</span>
                        <span>/</span>
                        <span style={{ color: 'rgb(24, 144, 255)' }}>{data[i].length}</span>个)
皮倩雯's avatar
皮倩雯 committed
343 344 345 346 347 348 349 350 351 352 353 354
                      </span>
                    </>
                  </div>
                  {data[i].map(j => (
                    <span
                      style={{
                        width: '150px',
                        marginLeft: '24px',
                        display: 'inline-block',
                        marginBottom: '20px',
                      }}
                    >
355
                      {allValue.indexOf(j) !== -1 ? (
356
                        <span>
357 358 359 360 361 362 363 364 365 366 367 368
                          {keepData.length > 0 &&
                            keepData.find(item => item.name === j).version !== 2022 && (
                              <img
                                src={New}
                                style={{
                                  height: '24px',
                                  marginTop: '-12px',
                                  marginRight: '3px',
                                }}
                                alt=""
                              />
                            )}
369 370 371 372 373 374 375
                          <PlusOutlined
                            style={{ color: 'rgb(24, 144, 255)', marginRight: '5px' }}
                          />
                          {j}
                        </span>
                      ) : (
                        <span>
376 377 378 379 380 381 382 383 384 385 386 387
                          {keepData.length > 0 &&
                            keepData.find(item => item.name === j).version !== 2022 && (
                              <img
                                src={New}
                                style={{
                                  height: '24px',
                                  marginTop: '-12px',
                                  marginRight: '3px',
                                }}
                                alt=""
                              />
                            )}
388 389 390 391
                          <CheckOutlined style={{ color: 'green', marginRight: '5px' }} />
                          {j}
                        </span>
                      )}
皮倩雯's avatar
皮倩雯 committed
392 393 394 395 396 397 398 399 400 401 402
                    </span>
                  ))}
                </div>
              ))}
          </>
        )}
      </Spin>
    </Modal>
  );
};
export default AppendModal;