import common from './common.js'
|
|
function req(obj) {
|
return new Promise((resolve, reject) => {
|
// 这个地址是 给 收货配置的地址
|
//const HOST = 'http://192.168.0.191:7001' // 正式生产线上地址
|
const HOST = 'http://127.0.0.1:7001' // 本地地址
|
|
var method = obj.method || "GET";
|
var url = HOST + obj.url || "";
|
if (obj.url.indexOf('http') != -1) {
|
url = obj.url;
|
}
|
var data = obj.data || {};
|
|
// 前面信息
|
var timestamp = new Date().valueOf();
|
var nonce = Math.random()
|
.toString(36)
|
.substr(2);
|
let tokenInfo = JSON.parse(uni.getStorageSync('$tokenInfouser') || '{}')
|
var guid = tokenInfo.guid
|
var accessToken = uni.getStorageSync('token') || ''
|
|
var signature = common.getSignature(timestamp, nonce, tokenInfo, data);
|
var headers = Object.assign({
|
timestamp: timestamp,
|
nonce: nonce,
|
guid: guid,
|
accessToken: accessToken,
|
signature: signature,
|
'Content-Type': 'application/json;charset=UTF-8'
|
});
|
var header = {
|
...obj.header,
|
...headers
|
};
|
var success = obj.success; // 成功回调函数
|
var fail = obj.fail; //表示失败后,要执行的回调函数
|
uni.showLoading({
|
title: '加载中...',
|
mask: true
|
});
|
console.log('xxxxx', obj)
|
uni.request({
|
url: url,
|
data: data,
|
method: method,
|
header: header,
|
success: ((res) => {
|
console.log('success', res)
|
// debugger
|
uni.hideLoading();
|
if (res.statusCode == 403 || res.statusCode == 401) {
|
// 错误处理,返回登录页
|
uni.reLaunch({
|
url: '/pages/Login/index'
|
})
|
} else if (res.data.message == '用户未登录,请先登录程序') {
|
uni.showToast({
|
title: '用户登陆状态失效',
|
icon: 'none',
|
duration: 2000,
|
success: function(res) {
|
setTimeout(() => {
|
uni.reLaunch({
|
url: '/pages/login/index'
|
})
|
}, 1000)
|
}
|
});
|
} else if (res.statusCode == 200) {
|
resolve(res)
|
uni.hideLoading();
|
} else {
|
let msg = res.errMsg || `接口调用出错:${res.statusCode}`
|
uni.showModal({
|
title: '系统提示',
|
content: msg,
|
showCancel: false
|
})
|
reject(res)
|
uni.hideLoading();
|
}
|
}),
|
fail: ((err) => {
|
uni.hideLoading();
|
reject("失败")
|
}),
|
complete: (e) => {
|
console.log('complete')
|
uni.hideLoading();
|
}
|
})
|
})
|
}
|
export default req
|