首页 > 微信 > 详细

微信小程序封装http。js

时间:2019-11-25 12:16:32      阅读:163      评论:0      收藏:0      [点我收藏+]
const baseUrl = ‘https://dev.app.qianyipan.com‘;
const url = ‘http://dev.static.qianyipan.com/‘

const http = ({ url = ‘‘, param = {}, ...other } = {}) => {
wx.showLoading({
title: ‘请求中,请耐心等待..‘
});
let timeStart = Date.now();
return new Promise((resolve, reject) => {
console.log(url, other)
wx.request({
url: getUrl(url),
data: param,
header: {
‘content-type‘: other.method == ‘get‘ ? ‘application/json‘ : ‘application/x-www-form-urlencoded‘
},
...other,
complete: (res) => {
wx.hideLoading();
console.log(`耗时${Date.now() - timeStart}`);
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(res.data)
} else {
reject(res)
}
}
})
})
}

const getUrl = (url) => {
if (url.indexOf(‘://‘) == -1) {
url = baseUrl + url;
}
return url
}

// get方法
const _get = (url, param = {}) => {
return http({
url,
param,
method: ‘get‘
})
}

const _post = (url, param = {}) => {
return http({
url,
param,
method: ‘post‘
})
}

const _put = (url, param = {}) => {
return http({
url,
param,
method: ‘put‘
})
}

const _delete = (url, param = {}) => {
return http({
url,
param,
method: ‘put‘
})
}
module.exports = {
url,
baseUrl,
_get,
_post,
_put,
_delete
}

微信小程序封装http。js

原文:https://www.cnblogs.com/teahouse/p/11926754.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!