115 lines
2.4 KiB
JavaScript
115 lines
2.4 KiB
JavaScript
export default (params) => {
|
|
let url = params.url;
|
|
let method = params.method || "get";
|
|
let data = params.data || {};
|
|
let type = params.type || 1;
|
|
let toast = params.toast || true;
|
|
let header = {
|
|
version: uni.conf.version,
|
|
type: uni.getSystemInfoSync().platform,
|
|
// #ifdef APP-PLUS
|
|
environment: 'app',
|
|
// #endif
|
|
// #ifdef H5
|
|
environment: 'H5',
|
|
// #endif
|
|
// #ifdef MP-WEIXIN
|
|
environment: 'wx',
|
|
// #endif
|
|
// #ifdef MP-ALIPAY
|
|
environment: 'alipay',
|
|
// #endif
|
|
token: uni.cache.get('token'),
|
|
openId: uni.cache.get('miniAppOpenId'),
|
|
id: uni.cache.get('userInfo').id,
|
|
shopId: uni.cache.get('shopId'),
|
|
userId: uni.cache.get('userInfo').id,
|
|
}
|
|
if (toast) {
|
|
uni.showLoading({
|
|
title: '加载中',
|
|
mask: true
|
|
})
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: uni.conf.baseUrl + url,
|
|
method: method,
|
|
header: header,
|
|
data: data,
|
|
success(response) {
|
|
const res = response.data
|
|
// 根据返回的状态码做出对应的操作
|
|
//获取成功
|
|
if (res.code == 0) {
|
|
uni.hideLoading();
|
|
uni.hideToast();
|
|
resolve(res);
|
|
} else {
|
|
switch (res.code) {
|
|
case '-4':
|
|
resolve(res)
|
|
// uni.showToast({
|
|
// title: res.message || res.msg || res.error,
|
|
// icon: "none",
|
|
// success: () => {
|
|
// setTimeout(() => {
|
|
// uni.reLaunch({
|
|
// url: "/pages/login/index",
|
|
// })
|
|
// }, 1000);
|
|
// }
|
|
// })
|
|
break;
|
|
case 404:
|
|
uni.showToast({
|
|
title: '请求地址不存在...',
|
|
duration: 2000,
|
|
})
|
|
break;
|
|
default:
|
|
// 是否提示
|
|
console.log(res)
|
|
if (toast) {
|
|
uni.showToast({
|
|
title: res.message || res.msg || res.error,
|
|
icon: "none",
|
|
success: () => {
|
|
setTimeout(res => {
|
|
|
|
}, 2000)
|
|
}
|
|
})
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
},
|
|
fail(err) {
|
|
console.log(err)
|
|
if (err.errMsg.indexOf('request:fail') !== -1) {
|
|
uni.showToast({
|
|
title: '网络异常',
|
|
icon: "error",
|
|
duration: 2000
|
|
})
|
|
} else {
|
|
uni.showToast({
|
|
title: '未知异常',
|
|
duration: 2000
|
|
})
|
|
}
|
|
reject(err);
|
|
|
|
},
|
|
complete() {
|
|
// 不管成功还是失败都会执行
|
|
setTimeout(res => {
|
|
uni.hideLoading();
|
|
uni.hideToast();
|
|
}, 10000)
|
|
|
|
}
|
|
});
|
|
}).catch((e) => {});
|
|
}; |