//不显示对话框的请求
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;
  console.log("params == " + params);
  console.log("method == " + method);
  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) {
      console.log(res)
      wx.showModal({
        title: '执行出错',
        content: res,
      })
    },
    complete: function(res) {
      wx.hideLoading()
    },
  })
}

// 新请求方式，添加中转服务器
function requestLoading(method, params, successaction) {
  wx.showLoading({
    title: "加载中",
  })
  //{"DecryptCookie":"","CustomerId":1,"Method":"/MainSystem/MainSystem/Auth/Login","Data":["栾慧",""]}
  var newData = {
    // 客户和司机不添加cookie请求
    "DecryptCookie": "",//EasyAuth=72335493ecb14015be2db57fc364dc84
    // 小程序所在客户ID（仙坛，万福，和美）
    "CustomerId": app.globalData.CustomerId,
    // 代替url原来后边拼接的方式，提取到data内
    "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) {
      console.log(res)
      wx.showModal({
        title: '执行出错',
        content: res,
      })
    },
    complete: function(res) {
      wx.hideLoading()
    },
  })
}

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