forked from vbenjs/vue-vben-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add request retry (vbenjs#1553)
- Loading branch information
Showing
11 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters