You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
994 B
37 lines
994 B
|
1 year ago
|
import CryptoJS from "crypto-js";
|
||
|
|
|
||
|
|
const secretKey = "0p88k7EksjnEqcmrWSHawRp7CV";
|
||
|
|
|
||
|
|
|
||
|
|
// 参数加密 dataStr 是JSON.stringify()后的字符串
|
||
|
|
const encodeData = (dataStr) => {
|
||
|
|
try {
|
||
|
|
// let data = JSON.parse(dataStr)
|
||
|
|
// 对参数进行排序并拼接成字符串
|
||
|
|
// const sortedParams = Object.keys(data).sort().reduce((acc, key) => {
|
||
|
|
// acc.push(`${key}=${data[key]}`);
|
||
|
|
// return acc;
|
||
|
|
// }, []).join('&');
|
||
|
|
|
||
|
|
// console.log("sign", sortedParams)
|
||
|
|
|
||
|
|
// 使用CryptoJS库基于HMAC-SHA256算法计算签名
|
||
|
|
const signature = CryptoJS.HmacSHA256(dataStr, secretKey).toString(CryptoJS.enc.Base64);
|
||
|
|
|
||
|
|
// 将签名添加到请求参数中
|
||
|
|
const requestData = {
|
||
|
|
data: dataStr,
|
||
|
|
signature: signature
|
||
|
|
};
|
||
|
|
console.log(requestData)
|
||
|
|
return requestData
|
||
|
|
} catch(e) {
|
||
|
|
console.log(e)
|
||
|
|
return {data: dataStr}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = encodeData
|
||
|
|
|