"use strict";
const common_vendor = require("../common/vendor.js");
const utils_common = require("../utils/common.js");
require("./handle.js");
const hook_useSmallProgramLogin_index = require("../hook/use-small-program-login/index.js");
let _app;
const httpPost = (url, data, options = {}, complete = void 0) => {
  let has = getApp().globalData.accountInfo.miniProgram.envVersion != "develop";
  let querData = {};
  if (utils_common.isNotEmpty(getApp().globalData.noNeedSecretKeyApiArr)) {
    for (let i of getApp().globalData.noNeedSecretKeyApiArr) {
      if (url.indexOf(i) != "-1") {
        has = false;
        break;
      }
    }
  }
  if (url.indexOf("callApiJSON") == -1 && url.indexOf("callApi.do") == -1) {
    has = false;
  }
  let showLoading = true;
  if (options != void 0 && options.showLoading != void 0) {
    showLoading = options.showLoading;
  }
  if (showLoading) {
    utils_common.showLoading();
  }
  let header = {
    "content-type": "application/json",
    token: getApp().globalData.token || common_vendor.index.getStorageSync("token"),
    "call-appId": common_vendor.index.getStorageSync("frontEndConfig").fixedAppjsGlobalData.appId
  };
  if (has && utils_common.isNotEmpty(getApp().globalData.apiSecretKey)) {
    header.appId = "KASIET_alismaillpro", querData.data = utils_common.desEncrypt(JSON.stringify(data), getApp().globalData.apiSecretKey);
  }
  if (options) {
    const extOptions = [
      "showLoading",
      // 显示加载中
      "onChunkReceived",
      // 分块传输监听
      "timeout",
      // 超时时间, 默认取/app.json里的networkTimeout配置
      "responseType"
      // 响应结果
    ];
    Object.keys(options).forEach((key) => {
      if (!extOptions.includes(key)) {
        header[key] = options[key];
      }
    });
  }
  return new Promise((resolve, reject) => {
    const enableChunked = options && typeof options.onChunkReceived == "function";
    const requestOptions = {
      url,
      data: utils_common.isEmpty(querData) ? data : querData,
      method: "POST",
      header,
      enableChunked,
      // 新增分块数据
      success(res) {
        if (showLoading) {
          utils_common.hideLoading();
        }
        if (!complete) {
          if (options && options.construction) {
            resolve({ data: { Data: res.data, RespCode: 1e4 } });
          } else {
            resolve(res);
          }
        }
      },
      fail(error) {
        if (showLoading) {
          utils_common.hideLoading();
        }
        if (!complete) {
          reject(error);
        }
        common_vendor.index.__f__("error", "at service/request.ts:83", error);
        utils_common.showToast(`连接超时，请重试`);
      },
      complete(res) {
        if (complete) {
          resolve(res);
        }
      }
    };
    if (options && options.timeout != void 0 && options.timeout != null) {
      requestOptions.timeout = options.timeout;
    }
    if (options && options.responseType != void 0 && options.responseType != null) {
      requestOptions.responseType = options.responseType;
    }
    const wxRequest = common_vendor.index.request(requestOptions);
    if (enableChunked) {
      wxRequest.onChunkReceived(options.onChunkReceived);
    }
  });
};
const doPost = async (url, data, options = {}, complete = void 0) => {
  const response = httpPost(url, data, options, complete);
  let loginAgain = false;
  await response.then((resp) => {
    if (resp.data.RespCode == 401) {
      loginAgain = true;
    }
  });
  if (!loginAgain) {
    return response;
  }
  if (common_vendor.index.getStorageSync("isCall") != 1) {
    common_vendor.index.setStorageSync("isCall", 1);
    common_vendor.index.setStorageSync("data_expiration", 0);
    let retry2 = getApp().globalData.retryNum;
    while (retry2 > 0) {
      await hook_useSmallProgramLogin_index.useSmallProgramLogin(_app).then((resp) => {
        if (resp.data.RespCode != "10000") {
          retry2--;
        } else {
          retry2 = 0;
        }
      });
      if (retry2 != 0) {
        await sleep(300);
      }
    }
  }
  let retry = getApp().globalData.retryNum;
  while (retry > 0) {
    if (common_vendor.index.getStorageSync("isCall") != 1) {
      return httpPost(url, data, options);
    } else {
      await sleep(500);
      retry--;
    }
  }
  return httpPost(url, data, options);
};
const doGet = async (url, data, options = {}) => {
  const response = httpGet(url, data, options);
  let loginAgain = false;
  await response.then((resp) => {
    if (resp.data.RespCode == 401) {
      loginAgain = true;
    }
  });
  if (!loginAgain) {
    return response;
  }
  if (common_vendor.index.getStorageSync("isCall") != 1) {
    common_vendor.index.setStorageSync("isCall", 1);
    common_vendor.index.setStorageSync("data_expiration", 0);
    let retry2 = getApp().globalData.retryNum;
    while (retry2 > 0) {
      await hook_useSmallProgramLogin_index.useSmallProgramLogin(_app).then((resp) => {
        if (resp.data.RespCode != "10000") {
          retry2--;
        } else {
          retry2 = 0;
        }
      });
      if (retry2 != 0) {
        await sleep(300);
      }
    }
  }
  let retry = getApp().globalData.retryNum;
  while (retry > 0) {
    if (common_vendor.index.getStorageSync("isCall") != 1) {
      return httpGet(url, data, options);
    } else {
      await sleep(500);
      retry--;
    }
  }
  return httpGet(url, data, options);
};
const httpGet = (url, data, options = {}) => {
  let showLoading = true;
  if (options != void 0 && options.showLoading != void 0) {
    showLoading = options.showLoading;
  }
  if (showLoading) {
    utils_common.showLoading();
  }
  let header = {
    "content-type": "application/json",
    token: getApp().globalData.token || common_vendor.index.getStorageSync("token"),
    "call-appId": common_vendor.index.getStorageSync("frontEndConfig").fixedAppjsGlobalData.appId
  };
  if (options) {
    Object.keys(options).forEach((key) => {
      if (key != "showLoading") {
        header[key] = options[key];
      }
    });
  }
  return new Promise((resolve, reject) => {
    common_vendor.index.request({
      url,
      data,
      method: "GET",
      header,
      success(res) {
        if (showLoading) {
          utils_common.hideLoading();
        }
        if (options && options.construction) {
          resolve({ data: { Data: res.data, RespCode: 1e4 } });
        } else {
          resolve(res);
        }
      },
      fail(error) {
        if (showLoading) {
          utils_common.hideLoading();
        }
        reject(error);
        utils_common.showToast(`连接超时，请重试`);
      }
    });
  });
};
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const request = {
  doPost,
  doGet
};
exports.request = request;
//# sourceMappingURL=../../.sourcemap/mp-alipay/service/request.js.map
