"use strict";
var __accessCheck = (obj, member, msg) => {
  if (!member.has(obj))
    throw TypeError("Cannot " + msg);
};
var __privateAdd = (obj, member, value) => {
  if (member.has(obj))
    throw TypeError("Cannot add the same private member more than once");
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateMethod = (obj, member, method) => {
  __accessCheck(obj, member, "access private method");
  return method;
};
var _request, request_fn;
const common_vendor = require("../common/vendor.js");
const store_user = require("../store/user.js");
const settings_siteSetting = require("../settings/siteSetting.js");
let requestCount = 0;
const showLoading = () => {
  if (requestCount === 0) {
    common_vendor.index.showLoading({ mask: true });
  }
  requestCount++;
};
const hideLoading = () => {
  requestCount--;
  if (requestCount <= 0) {
    common_vendor.index.hideLoading();
  }
};
class Request {
  constructor() {
    __privateAdd(this, _request);
  }
  get(options) {
    return __privateMethod(this, _request, request_fn).call(this, {
      ...options,
      method: "GET"
    });
  }
  post(options) {
    return __privateMethod(this, _request, request_fn).call(this, {
      ...options,
      method: "POST"
    });
  }
}
_request = new WeakSet();
request_fn = function(config) {
  const userStore = store_user.useUserStore();
  let {
    headers = {
      "Content-Type": "application/json;charset=UTF-8"
    },
    url,
    method,
    // 请求类型
    loading = true,
    // 是否触发 loading
    timeout = 60 * 3e3,
    // 请求超时时间
    returnResponse = false,
    // 是否返回原生数据
    message = true,
    hideErrorModal = false,
    // 报误弹窗
    params,
    // 请求参数
    customizeUrl = ""
    //自定义url
  } = config;
  const header = {
    ...headers,
    Authorization: userStore.getToken
  };
  if (method.toUpperCase() === "GET") {
    const now = Date.now();
    params = Object.assign(params || {}, { _t: now });
  }
  loading && showLoading();
  return new Promise((resolve, reject) => {
    common_vendor.index.request({
      url: settings_siteSetting.BASE_API_URL + url,
      header,
      method,
      timeout,
      data: params,
      dataType: "json",
      success: async (res) => {
        const { data } = res;
        if (returnResponse) {
          resolve(res);
          return;
        }
        switch (data.code) {
          case 200:
            resolve(data.data);
            break;
          case 400:
            common_vendor.index.showModal({
              title: data.msg || "系统异常，请联系管理员",
              cancelText: "回到首页",
              confirmText: "确定",
              success: (res2) => {
                if (res2.confirm)
                  ;
                else if (res2.cancel) {
                  common_vendor.index.redirectTo({ url: "/pages/index/index" });
                }
              }
            });
            reject();
            break;
          default:
            if (hideErrorModal)
              return;
            common_vendor.index.showModal({
              title: data.msg || "系统异常，请联系管理员",
              cancelText: "回到首页",
              confirmText: "确定",
              success: (res2) => {
                if (res2.confirm)
                  ;
                else if (res2.cancel) {
                  common_vendor.index.redirectTo({ url: "/pages/index/index" });
                }
              }
            });
            reject();
            break;
        }
      },
      fail: async (err) => {
        common_vendor.index.showToast({
          title: "网络不稳定，请稍后刷新重试",
          icon: "none"
        });
        await common_vendor.nextTick$1();
        reject(err);
      },
      complete: () => {
        loading && hideLoading();
      }
    });
  });
};
const api = new Request();
exports.api = api;
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map
