分享者:kingrome2017,来自原文地址
var root = "https://testapi.bidata.com.cn/platform/v3";
var env = "dev";
var _getURL = function(url) {
if(env == "mock") {
return url + ".json";
} else {
return root + url;
}
};
var _request = function(url,method,data,onSuccess,onError) {
data = data || {};
var behavior = data._behavior;
if(behavior) {
delete data._behavior;
} else {
behavior = "";
}
wx.request({
"url" : _getURL(url),
"method" : method,
"data" : data,
"success" : function(result) {
var code = result.data.CODE || result.data.code;
if(code == 200) {
onSuccess(result);
} else if(onError && typeof onError == 'function'){
onError(result);
} else {
// if(result.data.code == 401) {
// App.fireEvent("evt_invalid_token",result);
// }
// wx.showModal({
// "title" : "提示",
// "content" : result.data.msg,
// "showCancel":false
// });
}
},
"error" : onError || function(e) {
console.error(e);
},
"header" : {
// "X-Token" : wx.getStorageSync("token"),
// "X-Behavior" : behavior,
"X-Uid":"LMKCVXSU",
"X-Nonce":"45494415",
"X-Timestamp":"1493689633656",
"X-Signature":'96f0ca5db1564eabd17c844d9927738fb9fed3a6'
}
}
)};
module.exports={
$get : function(url,data,onSuccess,onError) {
_request(url,"GET",data,onSuccess,onError);
},
$put : function(url,data,onSuccess,onError) {
_request(url,"PUT",data,onSuccess,onError);
},
$post : function(url,data,onSuccess,onError) {
_request(url,"POST",data,onSuccess,onError);
},
$delete : function(url,data,onSuccess,onError) {
_request(url,"DELETE",data,onSuccess,onError);
},