/**
* 封装wx:request请求的工具
*/
const baseUrl = "";
const Ajax = options => {
const { url, method, param,showLoad = true,other } = options;
if (showLoad){
wx.showLoading({
title: '请稍等...'
});
}
return new Promise((resolve, reject) => {
wx.request({
url:getUrl(url),
data: param,
method,
...other,
success: (res) => {
if (showLoad)wx.hideLoading();
if (res.data.succ){
resolve(res.data.data)
}else{
wx.showToast({
title: res.data.errMsg,
icon: 'none',
duration: 2500
})
}
},
fail:(err)=>{
console.log(err)
if (showLoad) wx.hideLoading();
wx.showModal({
content: '请求超时!请检查当前网络!',
showCancel: false,
})
}
})
})
}
//判断传递过来的请求路径是否是完整的url地址
const getUrl = (url) => {
if (!url.includes('://')) {
url = baseUrl + url;
}
return url
}
export default Ajax;
使用
/**
* 分离所有请求
* **/
import Ajax from "./Ajax.js";
export const getsomeThing= param => Ajax({
url:"/api/getsomething",
method:'POST',
param
})