// signatureUtil.js
import md5 from "js-md5";
export function signatureGenerate({
  data,
  url,
  headers
}) {
  // 参数签名 密钥 + 时间戳 + header参数 + url

  // 密钥
  let secret = '3g7Xn1yl'
  // 时间戳
  let timestamp = new Date().getTime()

  console.log(data)
  console.log(url)

  // post参数
  let dataStr = dataSerialize(dataSort(data))
  // 生成签名
  let str = url+dataStr +"&timestamp=" + timestamp  + secret 


  console.log(str.toUpperCase())
  const sign = md5(str.toUpperCase())

  console.log(sign)

  return {
    signature: sign.toUpperCase(), // 将签名字母转为大写
    timestamp,
    secret
  }
}

// 参数排序
function dataSort(obj) {
  if (JSON.stringify(obj) == "{}" || obj == null) {
    return {}
  }
  let key = Object.keys(obj).sort()
  let newObj = {}
  for (let i = 0; i < key.length; i++) {
    newObj[key[i]] = obj[key[i]]
  }
  return newObj
}

// 参数序列化
function dataSerialize(sortObj) {
  let strJoin = ''
  for (let key in sortObj) {
    strJoin += key + "=" + sortObj[key] + "&"
  }

  // return strJoin.substring(0, strJoin.length - 1)
  return strJoin
}