uni-app封装请求方法和loading样式
// 接口请求域名const baseUrl = 'http://192.168.1.20:8080/'// 同时发送异步代码的次数,防止一次点击中有多次请求let ajaxTimes=0;// 封装请求方法function myRequest = (options)=>{var header = {...options.header};// 当前请求不是登录时请求,在header中加上后端
·
// 接口请求域名
const baseUrl = 'http://192.168.1.20:8080/'
// 同时发送异步代码的次数,防止一次点击中有多次请求
let ajaxTimes=0;
// 封装请求方法
function myRequest = (options)=>{
var header = {...options.header};
// 当前请求不是登录时请求,在header中加上后端返回的token
if(options.url != 'login'){
header["token"] = uni.getStorageSync('token');
}
ajaxTimes++;
// 显示加载中 效果
uni.showLoading({
title: "加载中",
mask: true,
});
return new Promise((resolve,reject)=>{
uni.request({
url:baseUrl +options.url,
method: options.method || 'POST',
data: options.data || {},
header,
success: (res)=>{
resolve(res)
},
fail: (err)=>{
reject(err)
},
// 完成之后关闭加载效果
complete:()=>{
ajaxTimes--;
if(ajaxTimes===0){
// 关闭正在等待的图标
uni.hideLoading();
}
}
})
})
}
export default myRequest
更多推荐
已为社区贡献1条内容
所有评论(0)