const http = function (action, data = {}, method = 'POST') {
  const url = wx._hwjShareData.serverUrl
  const extendParam = wx._hwjShareData.extendParam
  if (extendParam) data.extend_param = extendParam

  return new Promise(async function (resolve, reject) {
    wx.showLoading({title: '加载中...', mask: true})
    wx.request({
      data,
      method,
      url: url + action,
      header: {'content-type': 'application/json'},
      success(res) {
        wx.hideLoading()
        if (res.statusCode === 200) {
          resolve(res.data)
        } else {
          if(action=='pay_ad/v2/ad_after'||action=='pay_ad/v2/ad_before'){

          }else{
            wx.showToast({icon: 'none', title: '网络错误，状态码：' + res.statusCode})
          }
          resolve(null)
        }
      },
      fail(err) {
        wx.hideLoading()
        if(action=='pay_ad/v2/ad_after'||action=='pay_ad/v2/ad_before'){

        }else{
          wx.showToast({icon: 'none', title: err.errMsg})
        }
        
        reject(err)
      }
    })
  })
}


export default http
