//用BASEURL来表示域名地址
// const BASE_URL = 'http://192.168.110.153:9002'
// const BASE_URL = 'https://zswl.api.jpy.wang/zswl-cloud-bdb'
// const BASE_URL = 'https://api.dev.zonelife.cn'
import { BASE_URL } from "@/utils/config.js";

let errorCount = 0;
const MAX_ERROR_COUNT = 2;

//封装请求方法
export const request = (options) => {
  return new Promise((resolve, reject) => {
    //请求路径拼接，，其中options.url就是通过下面方法myRequest获取到接口部分的url
    //method--请求方法，不是method的post就是get
    //请求的参数，当没有参数的时候就是空对象
    // BASE_URL = BASE_UR.includes('dev') ?
    let BASE_URL2 = BASE_URL;
    // if(options.url.includes('/zswl-cloud-shop')){
    // 	BASE_URL2 = 'http://g3710170f8.zicp.fun'
    // 	options.url =	options.url.replace("/zswl-cloud-shop","")
    // }

    uni.request({
      url: BASE_URL2 + options.url,
      method: options.method || "GET",
      data: options.data || {},
      header: {
        accessToken: uni.getStorageSync("token"),
      },
      success: (res) => {
        if (res.statusCode !== 200) {
          return uni.showToast({
            title: "获取失败",
          });

          // uni.hideLoading()
        } else if (res.data.msg != "成功") {
          //接口返回报错

          uni.showToast({
            title:
              res.data.content ||
              res.data.msg ||
              res.data.exception.message ||
              "请求失败",
            icon: "error",
          });

          console.log("报错：", res);

          // token过期或出了问题
          if (
            res.data.exception &&
            res.data.exception.type ==
              "AuthenticationCredentialsNotFoundException"
          ) {
            uni.showLoading({
              title: "刷新登录中",
            });
            if (errorCount <= MAX_ERROR_COUNT) {
              console.log("errorCount", errorCount);
              errorCount++;
              if (!uni.getStorageSync("refreshToken")) {
                uni.clearStorageSync();
                uni.reLaunch({
                  url: "/pages/index/index",
                });
              }

              // 刷新token
              uni.request({
                url: BASE_URL + "/zswl-cloud-bdb/user/refreshToken",
                method: "GET",
                data: {
                  refreshToken: uni.getStorageSync("refreshToken"),
                },
                success(r) {
                  uni.hideLoading();
                  if (r.data.content == "刷新令牌错误") {
                    uni.showToast({
                      title: "登录失效",
                      icon: "fail",
                    });
                    uni.clearStorageSync();
                    uni.reLaunch({
                      url: "/pages/index/index",
                    });
                  } else {
                    uni.setStorageSync("token", r.data.content); //登录状态
                    // 刷新页面
                    let list = ["/pay/pay"];
                    var pages = getCurrentPages();
                    var url = "/" + pages[pages.length - 1].route;
                    if (list.indexOf(url) == -1) {
                      uni.reLaunch({
                        url,
                      });
                    }
                  }
                },
              });
            }
          }
        }

        resolve(res.data);
      },
      //请求失败
      fail: (err) => {
        uni.showToast({
          title: "请求接口失败",
        });
        reject(err);
      },
    });
  });
};
