const http = ({
  url = "",
  content_type = "",
  param = {},
  showLoading,
  ...other
} = {}) => {
  if (showLoading) {
    uni.showLoading({
      title: "正在加载中",
    });
  }
  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")) {
      header = {
        "content-type": content_type,
        "zoomwin-token": value,
        Authorization: value,
        "zoomwin-sid": uni.getStorageSync("userInfo").sessionId,
		
      };
    } else {
      header = {
        "content-type": content_type,
        platform: "wxapp",
      };
    }
    uni.request({
      url: url,
      data: param,
      header: header,
      ...other,
      complete: (res) => {
    
        if (showLoading) {
          uni.hideLoading();
        }

        if (res.data.code == "B00103") {
          uni.navigateTo({
            url: "/pages/login/login",
          });
        } else if (res.data.code == "B00101") {
          //刷新token
          uni.showToast({
            title: "身份验证已过期，请重新登录",
            icon: "none",
            mask: true,
            duration: 1500,
          });
          console.log('身份验证已过期，请重新登录------');
          uni.removeStorageSync("token");
          uni.removeStorageSync("userInfo");
		  uni.removeStorageSync('treeList')
          setTimeout(() => {
            wx.navigateTo({
              url: "/pages/login/login",
            });
          }, 1500);
        } else if (res.data.code == -1) {
          wx.showToast({
            title: res.data.message,
            icon: "none",
          });
        } else {
          resolve(res.data);
        }
      },
    });
  });
};

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

// post方法
const post = (url, param = {}, showLoading) => {
  return http({
    url,
    param,
    showLoading,
    method: "POST",
    content_type: "application/x-www-form-urlencoded",
  });
};
// post方法
const postJ = (url, param = {}, showLoading) => {
  return http({
    url,
    param,
    showLoading,
    method: "post",
    content_type: "application/json",
  });
};

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