import uni from './uniHooks';

let reqCount = 0,
  resCount = 0; //记录请求次数和响应次数

const request = (options) => {
  reqCount++;
  uni.showLoading(options?.showLoading || {});
  return new Promise((resolve, reject) => {
    uni
      .request({
        method: options.method,
        url: options.url,
        data: options.data,
        header: options.header, //如需其他配置可在此添加或先转换options.url，然后 ...options
      })
      .then((response) => {
        resCount++;
        if (reqCount === resCount) uni.hideLoading(); //防止连续请求多个接口时loading闪现
        // let [error, res] = response;
        resolve(response);
        if (response.status == 581) {
          uni.navigateTo({
            url: '/pages/errorPage/errorLimit?msg=当前访问太火爆了，稍后试试吧~',
          });
        }
      })
      .catch((error) => {
        resCount++;
        if (reqCount === resCount) uni.hideLoading();
        // let [err, res] = error;
        reject(error);
      });
  });
};
export default request;
