You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.3 KiB
JavaScript
62 lines
1.3 KiB
JavaScript
9 months ago
|
var Promise = require('../plugins/es6-promise.js')
|
||
|
function wxPromisify(fn) {
|
||
|
return function (obj = {}) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
obj.success = function (res) {
|
||
|
//成功
|
||
|
resolve(res)
|
||
|
}
|
||
|
obj.fail = function (res) {
|
||
|
//失败
|
||
|
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.resolve(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"
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
postRequest: postRequest,
|
||
|
getRequest: getRequest
|
||
|
}
|