http.js
1.23 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
/**
* @file ROP 请求签名;序列化依赖 lib/qs(indices / allowDots / encodeValuesOnly)。
* 签名算法与 store/usedcar/points 同源,收敛到 @uj/api-core 的 createSigner;
* 签完仍返回摊平后的对象,它同时是 wx.request 的请求体(与原实现逐字节一致)。
*/
import qs from '../lib/qs'
import { createSigner } from '@uj/api-core'
import environment from './environment.js'
const salesSigner = createSigner({ kind: 'rop', id: 'sales-sha1' })
const flatten = params => {
const objStr = qs.stringify(params, {
arrayFormat: 'indices',
allowDots: true,
encodeValuesOnly: true
})
const objVal = objStr.split('&')
const realObj = {}
objVal.forEach(item => {
const idx = item.indexOf('=')
realObj[item.split('=')[0]] = decodeURIComponent(item.slice(idx + 1))
})
return realObj
}
const sign = async (params, secret, noSignAttr = []) => {
delete params.sign
const flat = flatten(params)
const signed = await salesSigner.fields({
method: flat.method,
appKey: flat.appKey,
secret,
version: flat.v,
format: flat.format,
params: flat,
exclude: noSignAttr
})
return { ...flat, sign: signed.sign }
}
module.exports = {
...environment,
sign
}