sign.js
1.64 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @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 md5 from "md5"
import qs from "qs"
import { baseConfig } from "@/main"
import { deepClone } from "@/common/util"
// // 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 = [],way='sha1') => {
// console.log(baseConfig)
// 写里面取值解决一开始导入就被确定值导致后面无法改动
const secret = baseConfig?.axios?.secret
// console.log(secret)
delete params.sign
var objStr = qs.stringify(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))
})
// console.log(realObj)
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,md5}[way](secret + str.join("") + secret).toUpperCase()
return realObj
}
export default sign