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";
export default function requst(url : string, type : string, params : any, header : object = {}) {
// #ifndef APP-PLUS
uni.showLoading({
mask: true
})
// #endif
// #ifdef APP-PLUS
plus.nativeUI.showWaiting();
// #endif
console.log('检查URL' + env.API_URL + url,)
2025-04-29 13:26:19 +08:00
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);
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,
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-04-29 13:26:19 +08:00
console.log(result);
2025-04-09 18:55:14 +08:00
let res = result.data;
if (res.code === 200) {
console.log('请求成功')
resolve(res);
} else if (res.code == 401) {
console.log('请求失败')
// #ifdef APP-PLUS
plus.nativeUI.toast('登录状态失效,请重新登录');
// #endif
uni.showToast({
icon: 'none',
mask: true,
title: '登录状态失效,请重新登录',
duration: 2500,
})
// uni.removeStorageSync('TOKEN')
// uni.removeStorageSync('userInfo');
// uni.clearStorage();
// tools.jumpTo('/pages/client/account/login', {}, 0)
2025-04-29 13:26:19 +08:00
resolve(res)
2025-04-09 18:55:14 +08:00
} else {
setTimeout(() => {
uni.showToast({
icon: 'none',
mask: true,
title: 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
},
})
})
}