function wxPromisify(fn) {
return function (obj = {}) {
return new Promise((resolve, reject) => {
obj.success = function (res) {
//成功
console.log("成功了了·····" + res);
setTimeout(function () {
wx.hideLoading();
}, 100);
if(res.data.code=="200"||res.data.code=="10219"){
resolve(res)
}else{//抛出异常
setTimeout(function () {
wx.hideLoading();
}, 100);
wx.showModal({
title: '提示',
content: res.data.message,
confirmColor: '#118EDE',
showCancel: false,
success: function (res) {
if (res.confirm) {
//console.log('用户点击确定')
}
}
});
}
}
obj.fail = function (res) {
//失败
console.log("失败了·····"+res);
setTimeout(function () {
wx.hideLoading();
}, 100);
wx.showToast({
title: '服务器暂时无法连接',
icon: 'loading',
duration: 2000
})
reject(res)
}
fn(obj)
})
}
}
//无论promise对象最后状态如何都会执行
Promise.prototype.finally = function (callback) {
let P = this.constructor;
return this.then(
value => P.resolve(callback()).then(() => value),
reason => P.reject(callback()).then(() => { throw reason })
);
};
/**
* 微信请求get方法
* url
* data 以对象的格式传入
*/
function getRequest(url, data) {
var getRequest = wxPromisify(wx.request)
return getRequest({
url: url,
method: 'GET',
data: data,
header: {
'Content-Type': 'application/json'
}
})
}
/**
* 微信请求post方法封装
* url
* data 以对象的格式传入
*/
function postRequest(url, data) {
var postRequest = wxPromisify(wx.request)
return postRequest({
url: url,
method: 'POST',
data: data,
header: {
"content-type": "application/x-www-form-urlencoded"
},
})
}
/**
* 微信请求post方法封装
* url
* data 以对象的格式传入
*/
function postRequestJson(url, data) {
var postRequest = wxPromisify(wx.request)
return postRequest({
url: url,
method: 'POST',
data: data,
header: {
'Content-Type': 'application/json'
},
})
}
/**
* 微信请求post方法封装
* url
* data 以对象的格式传入
*/
function postRequestParamAndBody(url,param, data) {
var str = "?";
for (var key in param) {
str = str + "&" + key + "=" + param[key]
};
var postRequest = wxPromisify(wx.request)
return postRequest({
url: url+str,
method: 'POST',
data: data,
header: {
'Content-Type': 'application/json'
},
})
}
/**
* 微信请求put方法封装
* url
* data 以对象的格式传入
*/
function putRequest(url, data) {
var str = "?";
for (var key in data) {
str = str + "&" + key + "=" + data[key]
};
var putRequest = wxPromisify(wx.request)
return putRequest({
url: url + str,
method: 'PUT',
data: data,
header: {
"content-type": "application/x-www-form-urlencoded"
},
})
}
/**
* 微信请求put方法封装
* url
* data 以对象的格式传入
* parmare对象传入
*/
function putRequestParamAndBody(url, parmaer, data) {
var str = "?";
for (var key in parmare) {
str = str + "&" + key + "=" + parmare[key]
};
var putRequest = wxPromisify(wx.request)
return putRequest({
url: url + str,
method: 'PUT',
data: data,
header: {
"content-type": "application/x-www-form-urlencoded"
},
})
}
/**
* 微信请求delete方法封装
* url
* data 以对象的格式传入
*/
function deleteRequest(url, data) {
var str="?";
for(var key in data){
str = str + "&" + key + "=" + data[key]
};
console.log(str);
var deleteRequest = wxPromisify(wx.request)
return deleteRequest({
url: url + str,
method: 'DELETE',
data:data,
header: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
}
module.exports = {
postRequest: postRequest,
getRequest: getRequest,
putRequest: putRequest,
postRequestJson: postRequestJson,
deleteRequest: deleteRequest,
postRequestParamAndBody: postRequestParamAndBody
}
转载于:https://my.oschina.net/u/3392853/blog/1632985