import Vue from "vue";

Vue.prototype.$httpGetRetry = async function (...args) {
  let success = false;
  let error;
  for (let i = 0; i < 5; i++) {
    try {
      const res = await Vue.prototype.$http.get.apply(Vue, args);
      success = true;
      return res;
    } catch (_error) {
      console.log(_error);
      error = _error;
      if (!_error.response) {
        await new Promise((resolve) => setTimeout(resolve, 1000));
        continue; // 网络不通
      }
    }
  }
  if (!success) throw error;
};

Vue.prototype.$httpPostRetry = async function (...args) {
  let success = false;
  let error;
  for (let i = 0; i < 5; i++) {
    try {
      const res = await Vue.prototype.$http.post.apply(Vue, args);
      success = true;
      return res;
    } catch (_error) {
      console.log(_error);
      error = _error;
      if (!_error.response) {
        await new Promise((resolve) => setTimeout(resolve, 1000));
        continue; // 网络不通
      }
    }
  }
  if (!success) throw error;
};
