小程序 之wx.request和wx.showModal简单封装
一、request封装
request(url, data, successCallBack = function (data) {}, completeCallBack = function (data) {}, method = "GET", dataType = 'json', responseType = 'text') {
var method = method.toLowerCase()
if (method == 'get') {
var header = {
'content-type': 'application/json'
}
} else if (method == 'post') {
var header = {
'content-type': 'application/x-www-form-urlencoded'
}
}
wx.request({
url: url,
data: data,
header: header,
method: method,
dataType: dataType,
responseType: responseType,
success: function (res) {
successCallBack(res.data);
},
fail: function (res) {},
complete: function (res) {
completeCallBack(res)
},
})
}
二、showModal封装
tipsModal(content, successCallback = function() {}, title = '提示', showCancel = false, comfirmText = '知道了', confirmColor = '#03a9f4', hasCancel = false, cancelText = '取消', cancelColor = '#000') {
var params = {
title: title,
content: content,
showCancel: showCancel,
confirmText: comfirmText,
confirmColor: confirmColor,
success: function(res) {
if (res.confirm) {
successCallback();
}
}
}
if (hasCancel == true) {
params.cancelText = cancelText;
params.cancelColor = cancelColor;
}
wx.showModal(params);
}

浙公网安备 33010602011771号