import Vue from "vue";

Vue.prototype.$httpGetRetry = async function(...args) {
  for (let i = 0; i < 5; i++) {
    try {
      const res = await Vue.prototype.$http.get.apply(Vue, args);

      return res;
    } catch (error) {
      console.log(error);
      if (!error.response) {
        await new Promise(resolve => setTimeout(resolve, 1000));
        continue; // 网络不通
      } else {
        return; // 其他错误不管
      }
    }
  }
};

Vue.prototype.$httpPostRetry = async function(...args) {
  for (let i = 0; i < 5; i++) {
    try {
      const res = await Vue.prototype.$http.post.apply(Vue, args);

      return res;
    } catch (error) {
      console.log(error);
      if (!error.response) {
        await new Promise(resolve => setTimeout(resolve, 1000));
        continue; // 网络不通
      } else {
        return; // 其他错误不管
      }
    }
  }
};
