sign.js 1.17 KB
import sha1 from 'sha1'
import md5 from 'md5'
import qs from 'qs'
import _ from 'lodash'
const secret = window.location.href.includes('dj.glsx.com.cn')
  ? '4bc07f8868374c49a4661d603377e3106fc9a45bccaaa353'
  : 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b'
const sign = (params, noSignAttr = [], isMd5) => {
  delete params.sign
  var objStr = qs.stringify(_.cloneDeep(params), {
    arrayFormat: 'indices',
    allowDots: true,
    encodeValuesOnly: true,
  })
  var objVal = objStr.split('&')
  var realObj = {}
  objVal.forEach((item) => {
    const idx = item.indexOf('=')
    realObj[item.split('=')[0]] = decodeURIComponent(item.slice(idx + 1))
  })
  const keys = Object.keys(realObj).sort()
  var str = []
  var obj = null
  for (const key in realObj) {
    obj = realObj[key]
    if (obj === null) {
      obj = ''
    } else if (typeof obj === 'object') {
      obj = JSON.stringify(obj)
    }
    realObj[key] = obj
  }
  keys.forEach((e) => {
    !noSignAttr.includes(e) && str.push(e + realObj[e])
  })
  realObj.sign = isMd5
    ? md5(secret + str.join('') + secret).toUpperCase()
    : sha1(secret + str.join('') + secret).toUpperCase()
  return realObj
}
export default sign