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 (res.code == 401) { // #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) resolve(res) } else { if(!noMsg){ setTimeout(() => { uni.showToast({ icon: 'none', mask: true, title: 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 }, }) }) }