@@ -6,7 +6,9 @@ import { getToken } from '@/utils/auth'
66// 创建axios实例
77const service = axios . create ( {
88 baseURL : process . env . BASE_API , // api 的 base_url
9- timeout : 5000 // 请求超时时间
9+ timeout : 5000 , // 请求超时时间
10+ retry : 3 , // 全局的请求次数,请求的间隙
11+ retryDelay : 300
1012} )
1113
1214// request拦截器
@@ -62,12 +64,39 @@ service.interceptors.response.use(
6264 } ,
6365 error => {
6466 console . log ( 'err' + error ) // for debug
65- Message ( {
66- message : error . message ,
67- type : 'error' ,
68- duration : 5 * 1000
67+ const config = error . config
68+ console . log ( config )
69+ // If config does not exist or the retry option is not set, reject
70+ if ( ! config || ! config . retry ) return Promise . reject ( error )
71+
72+ // Set the variable for keeping track of the retry count
73+ config . __retryCount = config . __retryCount || 0
74+
75+ // Check if we've maxed out the total number of retries
76+ if ( config . __retryCount >= config . retry ) {
77+ // Reject with the error
78+ Message ( {
79+ message : error . message ,
80+ type : 'error' ,
81+ duration : 5 * 1000
82+ } )
83+ return Promise . reject ( error )
84+ }
85+
86+ // Increase the retry count
87+ config . __retryCount += 1
88+
89+ // Create new promise to handle exponential backoff
90+ const backoff = new Promise ( function ( resolve ) {
91+ setTimeout ( function ( ) {
92+ resolve ( )
93+ } , config . retryDelay || 1 )
94+ } )
95+
96+ // Return the promise in which recalls axios to retry the request
97+ return backoff . then ( function ( ) {
98+ return axios ( config )
6999 } )
70- return Promise . reject ( error )
71100 }
72101)
73102
0 commit comments