//不显示对话框的请求
const app = getApp()

function request(url, params, success, fail) {
  this.requestLoading(url, params, "", success, fail)
}

// 请求自己的服务器
function previousRequestLoading(method, params, successaction) {
  wx.showLoading({
    title: "加载中",
  })
  var data = {
    'id': 1,
    'method': method,
    'params': params
  };
  var baseUrl = app.globalData.BaseUrl;
  wx.request({
    url: baseUrl,
    data: data,
    header: {
      'content-type': 'application/json'
    },
    method: 'post',
    success: function(res) {
      console.log(res)
      if (res.data.error != null) {
        wx.showModal({
          showCancel: false,
          title: '执行出错',
          content: res.data.error.message,
        })
      } else {
        successaction(res.data)
      }
    },
    fail: function(res) {
      wx.showModal({
        title: '执行出错',
        content: res,
      })
    },
    complete: function(res) {
      wx.hideLoading()
    },
  })
}

// 中转，请求客户服务器
function requestLoading(method, params, successaction) {
  wx.showLoading({
    title: "加载中",
  })
  var newData = {
    "DecryptCookie": "",
    "CustomerId": app.globalData.CustomerId,
    "AppendUrlString": "?appid=" + app.globalData.AppId + "&phone=" + app.globalData.Phone,
    "Method": method,
    "Data": params,
  };
  var baseUrl = app.globalData.TranferBaseUrl;
  wx.request({
    url: baseUrl,
    data: newData,
    header: {
      'content-type': 'application/json'
    },
    method: 'post',
    success: function(res) {
      console.log(res)
      if (res.data.error != null) {
        wx.showModal({
          showCancel: false,
          title: '执行出错',
          content: res.data.error.message,
        })
      } else {
        successaction(res.data)
      }
    },
    fail: function(res) {
      wx.showModal({
        title: '执行出错',
        content: res,
      })
    },
    complete: function(res) {
      wx.hideLoading()
    },
  })
}

module.exports = {
  request: request,
  requestLoading: requestLoading,
  previousRequestLoading: previousRequestLoading
}