添加签名
This commit is contained in:
parent
913d604d3e
commit
4124407f52
|
|
@ -4,6 +4,7 @@
|
|||
*/
|
||||
|
||||
import EnvConfig from '@/common/env.js'
|
||||
import md5 from 'js-md5'
|
||||
|
||||
class RequestManager {
|
||||
/**
|
||||
|
|
@ -26,9 +27,9 @@ class RequestManager {
|
|||
reject(new Error('请求参数错误'))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
uni.getNetworkType({
|
||||
success: function(res) {
|
||||
success: function (res) {
|
||||
if (res.networkType == 'none') {
|
||||
uni.showToast({
|
||||
title: '网络连接异常,请检查网络',
|
||||
|
|
@ -39,24 +40,24 @@ class RequestManager {
|
|||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
const url = param.url || ''
|
||||
const method = param.method || 'POST'
|
||||
const data = param.data || {}
|
||||
const Loading = param.Loading || false
|
||||
const token = uni.getStorageSync('token')
|
||||
let client = ""
|
||||
|
||||
|
||||
// #ifdef H5
|
||||
client = "h5"
|
||||
// #endif
|
||||
|
||||
|
||||
// 获取API基础URL
|
||||
const apiBaseUrl = EnvConfig.apiBaseUrl
|
||||
|
||||
|
||||
// 拼接完整请求地址,确保URL格式正确
|
||||
let requestUrl = ''
|
||||
|
||||
|
||||
if (url.startsWith('http://') || url.startsWith('https://')) {
|
||||
// 如果是完整的URL,直接使用
|
||||
requestUrl = url
|
||||
|
|
@ -67,11 +68,11 @@ class RequestManager {
|
|||
const path = url.startsWith('/') ? url.substring(1) : url
|
||||
requestUrl = baseUrlWithSlash + path
|
||||
}
|
||||
|
||||
|
||||
console.log('请求URL:', requestUrl)
|
||||
|
||||
|
||||
let header = {}
|
||||
|
||||
|
||||
if (method.toUpperCase() == 'POST') {
|
||||
header = {
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
|
|
@ -81,18 +82,56 @@ 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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 显示加载提示
|
||||
if (!Loading) {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// 发起网络请求
|
||||
uni.request({
|
||||
url: requestUrl,
|
||||
|
|
@ -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'
|
||||
|
|
@ -179,7 +218,7 @@ class RequestManager {
|
|||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送GET请求
|
||||
* @param {String} url 请求地址
|
||||
|
|
@ -195,7 +234,7 @@ class RequestManager {
|
|||
Loading: !showLoading
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送POST请求
|
||||
* @param {String} url 请求地址
|
||||
|
|
|
|||
6
package-lock.json
generated
6
package-lock.json
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"js-md5": "^0.8.3",
|
||||
"uqrcodejs": "^4.0.7"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -342,6 +342,7 @@
|
|||
},
|
||||
|
||||
getCoupon() {
|
||||
|
||||
this.req({
|
||||
url: "coupon",
|
||||
data: {},
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user