微信小程序 wx.request 封装

**

// 域名
let req_url = “https://********”;

// get请求
let http_get = function (url, show = true, data = {}) {
if (show) {
wx.showLoading({
title: “加载中”,
});
}
return new Promise(function (resolve, reject) {
wx.request({
url: req_url + url,
method: “GET”,
data: data,
//根据项目需求是否需要添加token
header: {
“Content-Type”: “application/json”,
},
success: function (res) {
if (show) {
wx.hideLoading();
}
// 根据有业务需求去 进行返回值
if (res.data.code !== 200) {
wx.showToast({
title: res.data.message || “请求失败”,
icon: “none”,
duration: 1500,
});
} else {
resolve(res.data);
}
},
fail: function (err) {
wx.hideLoading();
wx.showModal({
title: “网络错误”,
content: “网络出错,请刷新重试”,
showCancel: false,
});
reject(err);
},
});
});
};
// post
let http_post = function (url, show = true, data = {}) {
if (show) {
wx.showLoading({
title: “加载中”,
});
}
return new Promise(function (resolve, reject) {
wx.request({
url: req_url + url,
method: “POST”,
data:data,
header: {
“Content-Type”: “application/x-www-form-urlencoded”, // 默认
// Authorization: null,
},
success: function (res) {
if (show) {
wx.hideLoading();
}
if (res.data.code == 200) {
resolve(res.data);
} else {
wx.showToast({
title: res.data.message || “请求失败”,
icon: “none”,
duration: 1500,
});
}
},
fail: function (err) {
wx.hideLoading();
wx.showModal({
title: “网络错误”,
content: “网络出错,请刷新重试”,
showCancel: false,
});
reject(err);
},
});
});
};
// upLoad 单图片上传
let upLoad = function (url, data = {}) {
return new Promise((resolve, reject) => {
wx.uploadFile({
url: req_url + url, //仅为示例,非真实的接口地址
filePath: data.path,
name: data.file,
formData: data.formData,
success(res) {
resolve(res.data);
},
fail(err) {
reject(err);
},
});
});
};
**
module.exports = {
upLoad,
req_url,
http_get,
http_post,
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值