committed by
GitHub
11 changed files with 111 additions and 3 deletions
@ -0,0 +1,28 @@ |
|||
import { AxiosError, AxiosInstance } from 'axios'; |
|||
/** |
|||
* 请求重试机制 |
|||
*/ |
|||
|
|||
export class AxiosRetry { |
|||
/** |
|||
* 重试 |
|||
*/ |
|||
retry(AxiosInstance: AxiosInstance, error: AxiosError) { |
|||
// @ts-ignore
|
|||
const { config } = error.response; |
|||
const { waitTime, count } = config?.requestOptions?.retryRequest; |
|||
config.__retryCount = config.__retryCount || 0; |
|||
if (config.__retryCount >= count) { |
|||
return Promise.reject(error); |
|||
} |
|||
config.__retryCount += 1; |
|||
return this.delay(waitTime).then(() => AxiosInstance(config)); |
|||
} |
|||
|
|||
/** |
|||
* 延迟 |
|||
*/ |
|||
private delay(waitTime: number) { |
|||
return new Promise((resolve) => setTimeout(resolve, waitTime)); |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
<template> |
|||
<div class="request-box"> |
|||
<a-button @click="handleClick" color="primary"> 点击会重新发起请求5次 </a-button> |
|||
<p>打开浏览器的network面板,可以看到发出了六次请求</p> |
|||
</div> |
|||
</template> |
|||
<script lang="ts" setup> |
|||
import { testRetry } from '/@/api/sys/user'; |
|||
// @ts-ignore |
|||
const handleClick = async () => { |
|||
await testRetry(); |
|||
}; |
|||
</script> |
|||
|
|||
<style lang="less"> |
|||
.request-box { |
|||
margin: 50px; |
|||
} |
|||
|
|||
p { |
|||
margin-top: 10px; |
|||
} |
|||
</style> |
|||
Loading…
Reference in new issue