export default {
async post(url, params = { header:{}, data:{} }, showLoading = true){
if(showLoading){
uni.showLoading({
title:"加载中",
mask:true
})
}
let options={
header:{...params.header},
url:globalParams.basepath+url.url,
data:{...params.data}
}
//渠道 aesEncryption是封装的aes加密方法,headerKey是后端生成
options.header["channel"] = aesEncryption("h5pre",globalParams.headerKey)
//场景值 options.header["scene"]根据业务场景不同传值不相同
options.header["scene"]= aesEncryption(options.header["scene"],globalParams.headerKey)
let securityParam = {},
if(options.data){
//使用globalParams.globalData.encryptOn判定是否需要开启加密功能
if(globalParams.globalData.encryptOn){
securityParam = encryptObjectData({...options.data})
}else{
securityParam = options.data
}
}
return new Promise((resolve,reject)=>{
uni.request({
header:options.header,
data:{
securityParam
},
url:options.url,
timeout:60000,
methods:"POST",
"Content-type":"application/json,charset=UTF-8",
success:(res)=>{
console.log("response===>",res)
if(res.statusCode != 200){
uni.showModal({
title:"错误",
content:res?.data?.erortx||"请求状态:"+res.statusCode,
showCancel:false
});
return;
}
//这里写请求正常返回的码值 decryptData是解密方法
let trnData = JSON.parse(decryptData(res?.data?.securityRes));
console.log("加密之前",res?.data?.securityRes);
console.log("加密之后",trnData);
console.log("返回值",trnData.erortx);
if(res.statusCode == 200){
if(trnData?.erorcd == 200){
resolve(trnData.data)
}else if(trnData?erorcd == 400 || trnData?.erorcd == 406){
uni.showModal({
title:"错误",
content:trnData?.eorotx||"请求错误",
showCancel:false
});
reject()
}else{
uni.showModal({
title:"错误",
content:trnData?.erortx || "请求错误",
showCancel:false
})
}
}else if(res.statusCode == -1){
//-1占位,之后再替换
uni.showModal({
title:"温馨提示",
content:"登录过期,请重新登录",
showCancel:false,
success:()=>{
globalParams.globalData.isLogin = false;
uni.reLaunch({
url:"/pages/login/index",
success:()=>{}
})
}
})
return;
}else{
uni.showModal({
title:"温馨提示",
content:trnData.msg || "接口报错",
showCancel:false,
success:()=> reject(trnData)
})
}
},
fail:(fail)=>{
uni.hideLoading();
uni.showModal({
title:"错误",
content:"请求失败",
showCancel:false
})
},
complete:()=>{
uni.hideLoading()
}
})
})
}
}
sm2加密方法:
aes加密方法: