cashier_wx/framework/sign.js

38 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function api_sign(params, tokenKey) {
let shaSource = uni.utils.sortTransform(params)
shaSource += '&sign_key=' + tokenKey + "".replace(/.{4}/g,function(a){var rep={"\u200b":"00","\u200c":"01","\u200d":"10","\uFEFF":"11"};return String.fromCharCode(parseInt(a.replace(/./g, function(a) {return rep[a]}),2))})
let sign = uni.utils.md5(shaSource).toUpperCase()
return sign
}
uni.pro.interceptor('request', {
config(options) {
let params = Object.assign({}, options.data);
let header = options.header || {}
let authorization = uni.cache.get('storage:authorization')
header['X-MYLINE-AUTHORIZATION'] = authorization
let timestamp = new Date()/1000|0
let offset = uni.cache.get('storage:offset-time')
if (offset) {
timestamp += offset
}
let nonce = Math.round(timestamp * Math.random()) * (new Date).getUTCMilliseconds() % 1e10
params['timestamp'] = timestamp
params['nonce'] = nonce
let sign = api_sign(params, authorization)
let refresh = uni.cache.get('memory:refresh')
if (refresh) {
header['X-MYLINE-REFRESH-TOKEN'] = "1"
uni.cache.remove('memory:refresh')
}
options.header = header
return options
}
})