"use strict";
const common_vendor = require("../common/vendor.js");
const stores_user = require("../stores/user.js");
const config = require("../config.js");
const utils_auth = require("../utils/auth.js");
const utils_errorCode = require("../utils/errorCode.js");
const utils_common = require("../utils/common.js");
const baseUrl = config.config.baseUrl;
const request = (config2) => {
  console.log(config2);
  const isToken = (config2.headers || {}).isToken === false;
  config2.header = config2.header || {};
  if (utils_auth.getToken() && !isToken) {
    config2.header["satoken"] = utils_auth.getToken();
    config2.header["sysId"] = common_vendor.index.getStorageSync("sysId");
  }
  if (config2.params) {
    let url = config2.url + "?" + utils_common.tansParams(config2.params);
    url = url.slice(0, -1);
    config2.url = url;
  }
  return new Promise((resolve, reject) => {
    common_vendor.index.request({
      method: config2.method || "get",
      url: config2.baseUrl || baseUrl + config2.url,
      data: config2.data,
      header: config2.header,
      dataType: "json"
    }).then((response) => {
      console.log(config2.url + "==============", response);
      const {
        data
      } = response;
      saveLogs(config2.url, config2.data, data.data);
      const code = data.code || 404;
      const msg = utils_errorCode.errorCode[code] || data.msg || utils_errorCode.errorCode["default"];
      if (code === 501) {
        let pages = getCurrentPages();
        let currentPage = pages[pages.length - 1].route;
        if (currentPage != "pages/login") {
          utils_common.toast("登录状态已过期，请重新登陆！");
          reject("无效的会话，或者会话已过期，请重新登录。");
          stores_user.useUserStore().LogOut().then((res) => {
            common_vendor.index.reLaunch({
              url: "/pages/login"
            });
          });
        }
      } else if (code === 500) {
        utils_common.toast(msg.substr(0, 50));
        reject(msg);
      } else if (code !== 200) {
        utils_common.toast(msg);
        reject(code);
      } else if (code === 404) {
        utils_common.toast("请求超时");
        reject(code);
      }
      resolve(data.data);
    }).catch((error) => {
      console.log("err", error);
      saveLogs(config2.url, config2.data, error);
      common_vendor.index.hideLoading();
      let {
        message
      } = error;
      if (message === "Network Error") {
        message = "网络连接超时";
      } else if (message.includes("timeout")) {
        message = "网络连接超时";
      } else if (message.includes("Request failed with status code")) {
        message = "系统接口" + message.substr(message.length - 3) + "异常";
      }
      utils_common.toast(message);
      reject(error);
    });
  });
};
function saveLogs(url, params, res) {
  let arr = [];
  let obj = {
    url,
    time: common_vendor.index.$u.timeFormat(new Date(), "yyyy-mm-dd hh:MM:ss"),
    params: JSON.stringify(params) || "无",
    res: JSON.stringify(res).substr(0, 1e3)
  };
  if (common_vendor.index.getStorageSync("logs")) {
    let arr2 = JSON.parse(common_vendor.index.getStorageSync("logs"));
    arr2.unshift(obj);
    if (arr2.length > 30) {
      arr2.pop();
    }
    common_vendor.index.setStorageSync("logs", JSON.stringify(arr2));
  } else {
    arr.unshift(obj);
    common_vendor.index.setStorageSync("logs", JSON.stringify(arr));
  }
}
exports.request = request;
