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

var reconnectMethod = '';
var reconnectParams = [];
var newReconnectMethod = '';
var newReconnectParams = [];


function requestLoading(method, params, successaction) {
  reconnectMethod = method;
  reconnectParams = params;
  wx.showLoading({
    title: "加载中",
  })

  var data = {
    'id': 1,
    'method': method,
    'params': params
  };

  wx.request({
    url: app.globalData.baseUrl,
    data: data,
    header: {
      'content-type': 'application/json',
      'cookie': app.globalData.cookie
    },
    method: 'POST',
    success: function (res) {
      // console.log(res) 
      if (res.data.error != null) {
        if (res.data.error.code == 401) {
          wx.request({
            url: app.globalData.baseUrl,
            data: {
              'id': 1,
              'method': "/MainSystem/MainSystem/Auth/WeixinLogin",
              'params': [app.globalData.appID, app.globalData.openID]
            },
            header: {
              'content-type': 'application/json',
              'cookie': app.globalData.cookie
            },
            method: 'POST',
            success: function (res) {
              app.globalData.cookie = res.data.result;
              requestLoading(reconnectMethod, reconnectParams, successaction);
            }
          })
        } else {
          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 transfer_request(method, params, successaction) {
  newReconnectMethod = method;
  newReconnectParams = params;
  wx.showLoading({
    title: "加载中",
  })

  var newData = {
    "DecryptCookie": app.globalData.cookie,
    "CustomerId": app.globalData.globalCustomerID,
    "Method": method,
    "Data": params,
  };

  var baseUrl = app.globalData.tranferBaseUrl;
  wx.request({
    url: baseUrl,
    data: newData,
    header: {
      'content-type': 'application/json',
      'cookie': app.globalData.cookie
    },
    method: 'POST',
    success: function (res) {
      // console.log(res)
      if (res.data.error != null) {
        if (res.data.error.code == 401) {
          wx.request({
            url: app.globalData.tranferBaseUrl,
            data: {
              "DecryptCookie": app.globalData.cookie,
              "CustomerId": app.globalData.globalCustomerID,
              "Method": "/MainSystem/MainSystem/Auth/WeixinLogin",
              "Data": [app.globalData.appID, app.globalData.openID]
            },
            header: {
              'content-type': 'application/json',
              'cookie': app.globalData.cookie
            },
            method: 'POST',
            success: function (res) {
              app.globalData.cookie = res.data.result;
              transfer_request(newReconnectMethod, newReconnectParams, successaction);

            }
          })
        } else {
          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 = {
  requestLoading: requestLoading,
  transfer_request: transfer_request
}
