sign.js 1.63 KB
/*
 * @Author: limin
 * @Date: 2019-08-26 18:02:49
 * @Last Modified by: chenwq
 * @Last Modified time: 2021-06-18 10:04:32
 */
import sha1 from 'sha1'
import qs from 'qs'
import { baseConfig } from '@/main'
import { util } from '@/utils'
// console.log(util)
// // 等后面再处理
// console.log(baseConfig)

// // const secret =
// //   window.location.href.includes('dj.glsx.com.cn') ||
// //   window.location.href.includes('dj.beta.glsx.com.cn')
// //     ? '4bc07f8868374c49a4661d603377e3106fc9a45bccaaa353'
// //     : 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b'

const sign = (params, noSignAttr = []) => {
  // console.log(baseConfig)
  // 写里面取值解决一开始导入就被确定值导致后面无法改动
  const secret = baseConfig?.axios?.secret
  // console.log(secret)
  delete params.sign
  var objStr = qs.stringify(util.deepClone(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 = sha1(secret + str.join('') + secret).toUpperCase()
  return realObj
}
export default sign