89 lines
1.9 KiB
TypeScript

import env from './env';
// import uniChat from "./uniChat";
export default function requst(url : string, type : string, params : any, header : object = {}, noMsg : boolean = true) {
// #ifndef APP-PLUS
uni.showLoading({
mask: true
})
// #endif
// #ifdef APP-PLUS
plus.nativeUI.showWaiting();
// #endif
// console.log('检查URL' + env.API_URL + url,)
// console.log('检查TOKEN' + 'Bearer '+ uni.getStorageSync('TOKEN'),)
console.log('检查请求体', params);
var apiHeader = ''
var headerObj = {}
if (url.indexOf('http') !== -1) {
apiHeader = url
headerObj = {}
} else {
apiHeader = env.API_URL + url
headerObj = {
Authorization: 'Bearer ' + uni.getStorageSync('TOKEN') || ''
}
}
// console.log(apiHeader);
return new Promise((resolve, reject) => {
uni.request({
url: apiHeader,
method: type,
data: params,
sslVerify: false,
header: {
...headerObj
},
timeout: 5000,
success(result : any) {
console.log('检查返回', result);
let res = result.data;
if (res.code === 200) {
resolve(res);
} else {
// if (!noMsg) {
// setTimeout(() => {
// uni.showToast({
// icon: 'none',
// mask: true,
// title: res.msg
// })
// })
// }
console.log(res.msg,'========================================================');
setTimeout(() => {
uni.showToast({
title: res.msg,
icon: 'none',
mask: false,
duration:5000,
success: function () {
console.log('成功显示');
}
})
},1000)
uni.$u.toast(res.msg)
resolve(res)
}
},
fail(error : any) {
console.log(error);
uni.showToast({
icon: 'none',
mask: true,
title: '与服务器断开'
})
reject(error)
},
complete() {
// #ifndef APP-PLUS
uni.hideLoading();
// #endif
// #ifdef APP-PLUS
plus.nativeUI.closeWaiting();
// #endif
},
})
})
}