var app = getApp();
var requestnum = 0;
const addnum = function(){
  if (requestnum==0){
    wx.showLoading({
      title:"加载中...",
      // mask:true
    })
  }
  requestnum++;
}
const reducenum = function(){
  requestnum--;
  if (requestnum==0){
    wx.hideLoading();
  }
}
export const get=(url,data,callback,failback)=>{
  addnum();
  wx.request({
    url: app.globalData.serverpath+url,
    method: "GET",
    header: {
      "Content-Type": "json"
    },
    data:data,
    success: function (res){
      console.debug(res);
      callback && callback(res);
    },
    fail: function (error){
      failback && failback(error);
    },
    complete: function () {
      reducenum();
    }
  })
} 
export const post=(url,data,header,callback,failback)=>{
  var authorization = wx.getStorageSync("authorization")
  if(typeof header=="function"){
    failback = callback;
    callback = header;
    header = {};
  }
  addnum();
  wx.request({
    url: app.globalData.serverpath+url,
    method: "POST",
    header: Object.assign({
      "Content-Type": "json",
      "Authorization":authorization
    },header),
    data:data,
    success: function (res){
      //console.debug(res);
      callback && callback(res);
    },
    fail: function (error){
      failback && failback(error);
    },
    complete: function () {
      reducenum();
    }
  })
}