添加签名

This commit is contained in:
zpc 2025-04-07 22:54:57 +08:00
parent 913d604d3e
commit 4124407f52
4 changed files with 63 additions and 16 deletions

View File

@ -4,6 +4,7 @@
*/
import EnvConfig from '@/common/env.js'
import md5 from 'js-md5'
class RequestManager {
/**
@ -28,7 +29,7 @@ class RequestManager {
}
uni.getNetworkType({
success: function(res) {
success: function (res) {
if (res.networkType == 'none') {
uni.showToast({
title: '网络连接异常,请检查网络',
@ -81,8 +82,46 @@ class RequestManager {
clickid: uni.getStorageSync('_click_id')
}
} else {
// GET请求添加签名
if (data) {
// 添加时间戳参数
data.timestamp = Math.floor(Date.now() / 1000);
// 按照键名对参数进行排序
const sortedParams = {};
Object.keys(data).sort().forEach(key => {
sortedParams[key] = data[key];
});
// 组合参数为字符串
let signStr = '';
for (const key in sortedParams) {
signStr += key + '=' + sortedParams[key] + '&';
}
// 获取当前请求的域名和时间戳,组合为密钥
// 使用正则表达式从URL中提取主机名
const hostRegex = /^(?:https?:\/\/)?([^\/]+)/i;
const matches = requestUrl.match(hostRegex);
const host = matches && matches[1] ? matches[1] : 'localhost';
const timestamp = data.timestamp;
const appSecret = host + timestamp;
// 添加密钥并去除最后的&
signStr = signStr.substring(0, signStr.length - 1) + appSecret;
console.log(signStr);
// 使用MD5生成签名
const sign = md5(signStr);
// 添加签名到请求参数
data.sign = sign;
}
header = {
'content-type': 'application/json'
'content-type': 'application/json',
token: token,
client: client,
}
}
@ -132,7 +171,7 @@ class RequestManager {
}, 100)
reject(res.data)
} else if (res.data.status == 0) {
setTimeout(function() {
setTimeout(function () {
uni.showToast({
title: res.data.msg,
icon: 'none'

6
package-lock.json generated
View File

@ -5,9 +5,15 @@
"packages": {
"": {
"dependencies": {
"js-md5": "^0.8.3",
"uqrcodejs": "^4.0.7"
}
},
"node_modules/js-md5": {
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/js-md5/-/js-md5-0.8.3.tgz",
"integrity": "sha512-qR0HB5uP6wCuRMrWPTrkMaev7MJZwJuuw4fnwAzRgP4J4/F8RwtodOKpGp4XpqsLBFzzgqIO42efFAyz2Et6KQ=="
},
"node_modules/uqrcodejs": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/uqrcodejs/-/uqrcodejs-4.0.7.tgz",

View File

@ -1,5 +1,6 @@
{
"dependencies": {
"js-md5": "^0.8.3",
"uqrcodejs": "^4.0.7"
}
}

View File

@ -342,6 +342,7 @@
},
getCoupon() {
this.req({
url: "coupon",
data: {},