const outLoginList = ["B00103", "B00109", "B00101", "401", "301"];

const http = ({
  url = "",
  content_type = "",
  param = {},
  showLoading,
  showErrorToast = true,
  ...other
} = {}) => {
  if (showLoading) {
    uni.showLoading({
      title: "正在加载中",
      mask: true,
    });
  }
  return new Promise((resolve, reject) => {
    let userId = wx.getStorageSync("userId");
    let Authorization = wx.getStorageSync("token");
    let header = {};
    const value = uni.getStorageSync("token"); //取存本地的token
    //登录接口不传token
    if (
      value &&
      !url.includes("/user/login") &&
      !url.includes("/connection/getConnectionTest")
    ) {
      header = {
        "content-type": content_type,
        "zoomwin-token": value,
        "zoomwin-sid": uni.getStorageSync("userInfo").sessionId,
      };
    } else {
      header = {
        "content-type": content_type,
        platform: "wxapp",
      };
    }
    uni.request({
      url: url,
      data: param,
      header: header,
      ...other,
      success: (res) => {

        if (showLoading) {
          uni.hideLoading();
        }

        if (outLoginList.includes(res.data.code)) {
          // 	uni.navigateTo({
          // 		url: '/pages/login/login',
          // 	})
          // } else if (res.data.code == "B00101") { //刷新token
          uni.showToast({
            title: "身份验证失效，请重新登录",
            icon: "none",
            mask: true,
            duration: 2500,
          });
          uni.removeStorageSync("token");
          uni.removeStorageSync("userInfo");
          setTimeout(() => {
            wx.navigateTo({
              url: "/pages/login/login",
            });
          }, 2500);
	
        } else if (res.data.code == 0) {
		
          resolve(res.data);
        } else {
          if (showErrorToast) {
            wx.showToast({
              title: res.data.message || res.data.error,
              icon: "none",
            });
          }
          reject(res.data);
        }
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
};

// get方法
const get = (url, param = {}, showLoading, showErrorToast, others = {}) => {
  return http({
    url,
    param,
    showLoading,
    showErrorToast,
    content_type: "application/x-www-form-urlencoded",
    ...others,
  });
};
// get方法
const getJ = (url, param = {}, showLoading, showErrorToast) => {
  return http({
    url,
    param,
    showLoading,
    showErrorToast,
    content_type: "application/json",
  });
};

// post方法
const post = (url, param = {}, showLoading, showErrorToast) => {
  return http({
    url,
    param,
    showLoading,
    showErrorToast,
    method: "POST",
    content_type: "application/x-www-form-urlencoded",
  });
};
// post方法 请求文件
const postB = (url, param = {}, showLoading, showErrorToast) => {
  return http({
    url,
    param,
    showLoading,
    showErrorToast,
    method: "POST",
    content_type: "application/x-www-form-urlencoded",
    responseType: "blob",
  });
};
// post方法
const postJ = (url, param = {}, showLoading, showErrorToast) => {
  return http({
    url,
    param,
    showLoading,
    showErrorToast,
    method: "post",
    content_type: "application/json;charset=UTF-8",
  });
};

module.exports = {
  get,
  getJ,
  post,
  postB,
  postJ,
};
