const CryptoJS = require('crypto-js') // 引用AES源码js

// const key = CryptoJS.enc.Utf8.parse('1234123412ABCDEF') // 十六位十六进制数作为密钥
// const iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412') // 十六位十六进制数作为密钥偏移量
const DEF_KEY = '2020020202020202'

// 解密方法
export function decrypt (word, key) {
  if (!key) {
    key = DEF_KEY
  }

  let ckey = CryptoJS.enc.Utf8.parse(key) // 十六位十六进制数作为密钥
  // let encryptedHexStr = CryptoJS.enc.Hex.parse(word)
  // let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)

  let decrypt = CryptoJS.AES.decrypt(word, ckey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  // console.log('decryptedStr', decryptedStr.toString())

  return decryptedStr.toString()
}

// 加密方法
export function encrypt (word, key) {
  if (!key) {
    key = DEF_KEY
  }
  let ckey = CryptoJS.enc.Utf8.parse(key)
  // let srcs = CryptoJS.enc.Utf8.parse(word)
  let encrypted = CryptoJS.AES.encrypt(word, ckey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  // console.log('encrypted', encrypted.toString())
  return encrypted.toString()
}

// 加密dataForm
function enf (dataForm, sf, st) {
  let dataForm1 = {
    ...dataForm,
    sf: sf,
    st: st
  }
  let sfs = sf.split(',')
  sfs.forEach((itm, ind) => {
    dataForm1[itm] = encrypt(dataForm1[itm])
  })
  return dataForm1
}

export default {
  decrypt,
  encrypt,
  enf
}
