/**
 * 工具类
 */
import CryptoJS from 'crypto-js'
import SystemConfig from '@/utils/SystemConfig'
import { Cookies } from "@/utils/storage";

const encrypt = function (word) {
  const keyStr = Cookies.get(SystemConfig.secretKey);
  console.log("encrypt keyStr====", keyStr);
  let key = CryptoJS.enc.Utf8.parse(keyStr);// Latin1 w8m31+Yy/Nw6thPsMpO5fg==
  let srcs = CryptoJS.enc.Utf8.parse(word);
  let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
  return encrypted.toString()
};
// 解密
const decrypt = function (word) {
  const keyStr = Cookies.get(SystemConfig.secretKey);
  console.log("decrypt keyStr====", keyStr);
  let key = CryptoJS.enc.Utf8.parse(keyStr); // Latin1 w8m31+Yy/Nw6thPsMpO5fg==
  let decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
  return CryptoJS.enc.Utf8.stringify(decrypt)
};

const encryptResult = (result) => {
  return encrypt(JSON.stringify(result))
};
export { encrypt, decrypt, encryptResult }
