import {
apiBaseUrl,
jhghUrl,
apiSalt,
userCheckList,
jhghUrlrenzheng
} from ‘@/common/config.js’;
import * as sign from ‘@/common/sign.js’
import md5 from ‘@/js/md5.js’
//封装请求
const request = (url = ‘’, date = {}, type = ‘post’, header = {
‘content-type’: ‘application/x-www-form-urlencoded’,
}) => {
return new Promise((resolve, reject) => {
//统一验签
let thistime = sign.timestamp()
date.timestamp = thistime
date.sign = md5(apiSalt + thistime).toUpperCase()
//不在白名单中,需要加openid的url
if(!userCheckList.find(item=>item==url)){
//不需验证的方法也可传,需要验的也可以为空
date.openid = uni.getStorageSync(‘openid’)
date.token = uni.getStorageSync(‘token’)
}
uni.request({
method: type,
url: apiBaseUrl + url,
data: date,
header: header,
dataType: ‘json’,
}).then((response) => {
setTimeout(function() {
uni.hideLoading();
}, 200);
let [error, res] = response;
// 无用户信息
if (res.data.code == 9999) {
/*
uni.showModal({
title:‘提示’,
content: res.data.msg,
confirmText: “重新获取”,//这块是确定按钮的文字
cancelText:“关闭”,//这块是取消的文字
success: function(res) {
if (res.confirm) {//重新获取 返回主程序
window.location.href=jhghUrl
} else if (res.cancel) {//彻底退出
weixinClosePage()
}
}
});
*/
//延迟跳转时间
// window.setTimeout(function(){ window.location.href=jhghUrl } ,2000);
window.location.href=jhghUrl
//购买过一单,直接退出
}else if(res.data.code==8888){
alert(res.data.msg) //提示退出
weixinClosePage()
}
//用户未认证
// else if(res.data.code==7777){
// // alert(res.data.msg) 示退出
// uni.showModal({
// title:'提示',
// content: res.data.msg+uni.getStorageSync('openid')+"::"+uni.getStorageSync('token'),
// confirmText: "去认证",//这块是确定按钮的文字
// cancelText:"关闭",//这块是取消的文字
// success: function(res) {
// if (res.confirm) {//去认证 返回主程认证
// window.location.href=jhghUrlrenzheng
// } else if (res.cancel) {//彻底退出
// weixinClosePage()
// }
// }
// });
// }
else{
resolve(res.data);
}
}).catch(error => {
// alert(error)
uni.showToast({
title:"您网络不佳,请下拉刷新",
icon:'none',
duration: 1500
})
let [err, res] = error;
reject(err)
})
});
}
function weixinClosePage() {
document.addEventListener(‘WeixinJSBridgeReady’, function(){ WeixinJSBridge.call(‘closeWindow’); }, false)
if (typeof WeixinJSBridge == “undefined”) {
if (document.addEventListener) {
document.addEventListener(‘WeixinJSBridgeReady’,weixin_ClosePage1(), false);
} else if (document.attachEvent) {
document.attachEvent(‘WeixinJSBridgeReady’, weixin_ClosePage1());
document.attachEvent(‘onWeixinJSBridgeReady’, weixin_ClosePage1());
}
} else {
weixin_ClosePage1();
}
}
function weixin_ClosePage1() {
window.WeixinJSBridge.call(‘closeWindow’);
}
export default request