89 lines
1.9 KiB
TypeScript
Raw Normal View History

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