sign.js
1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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