微信小程序request请求封装,验签代码实例
(编辑:jimmy 日期: 2025/10/30 浏览:3 次 )
这篇文章主要介绍了微信小程序request请求封装,验签代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.公共文件util添加
request请求
//简单封装请求
function request(params, path, isShowLoading = true, goBack = false, type = 'none', obj={}) {
try {
console.log(path)
let NowTime = Date.parse(new Date()) / 1000;
let defaultParams = {
platform: app.globalData.platform,
appVersion: app.globalData.version,
deviceId: uuId(),
timestamp: params.timestamp "accessToken_v1")}`, {
cookie
} = obj.header;
console.log(cookie)
obj.header.cookie = cookie "htmlcode">
//生成签名
function signature(params) {
let keyArr = []
for (let key in params) {
keyArr.push(key)
}
let arr = keyArr.sort();
let result = "";
arr.map((item, index) => {
if (params[item] == null || params[item] == undefined) {
delete params[item];
arr.splice(index, 1);
}
})
console.log("arr")
console.log(arr)
arr.map((item, index) => {
if (Array.isArray(params[item]) == true) {
let res = ''
let arr = params[item]
arr.map(obj => {
console.log(typeof(obj))
if (typeof(obj) != 'object') {
res += obj
} else {
for (let key in obj) {
if (Array.isArray(obj[key])) {
let val = '';
obj[key].map(item => {
val += item
})
res += key + val
} else {
res += key + obj[key]
}
}
}
})
result += '' + item + '' + res
} else {
result += '' + item + '' + params[item]
}
})
console.log("result")
console.log(result)
let pos;
if (isEmpty(getToken())) {
pos = result
} else {
pos = result + getToken()
}
console.log(pos)
console.log("signature")
let signature = sha256.hmac.create('pos_secret').update(pos).hex().toLowerCase(); //需要引入sha256.min.js
console.log(signature)
let obj = {
'signature': signature
}
return Object.assign(params, obj);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:vue中使用极验验证码的方法(附demo)