const app = getApp()

module.exports = function send_request({ url, method, data }) {
  wx.showLoading({
    title: '加载中'
  });
  wx.showNavigationBarLoading()
  // wx.startPullDownRefresh() // 会触发页面的刷新事件 造成死循环
  return new Promise((resolve, reject) => {
    let requestOption = {
      url: `${app.globalData.baseUrl}${url}`,
      method: method,
      data: data,
      success: function (d) {
        wx.hideLoading()
        let result = d.data;
        if (result.success) {
          // console.log('result', result)
          resolve(result.data)
        } else {
          wx.showToast({
            title: result.msg,
            icon: 'error',
            duration: 2000
          })
          resolve(null)
        }
      },
      fail: function (err) {
        wx.hideLoading()
        wx.showToast({
          title: "请求失败",
          icon: 'error',
          duration: 2000
        })
        reject(err)
      },
      complete: function () {
        wx.hideNavigationBarLoading()
        wx.stopPullDownRefresh()
      }
    }
    switch (method.toUpperCase) {
      case "GET":
        break;
      case "POST":
        requestOption.header = {
          "Content-Type": "application/x-www-form-urlencoded" //用于post
        }
        break;
    }
    wx.request(requestOption)
  });
}