-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
434 additions
and
25 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
/* | ||
* @file: 所有的接口列表 | ||
* 宝可梦api https://pokeapi.co/about | ||
*/ | ||
|
||
import http from '../http'; | ||
import {pokemonOptions} from '@/interface/http'; | ||
|
||
/** | ||
* @function get | ||
* @description 请求测试 | ||
*/ | ||
|
||
export function fetchPokemon(data: pokemonOptions) { | ||
return http({ | ||
url: ' https://pokeapi.co/api/v2/pokemon', | ||
params: data | ||
}); | ||
} | ||
/** | ||
* @function post | ||
* @description 请求测试 | ||
*/ | ||
|
||
export function fetchPostTest(data: any) { | ||
return http({ | ||
url: '/xxx/list', | ||
method: 'post', | ||
data | ||
}); | ||
} |
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,6 @@ | ||
1. 实现请求拦截 | ||
2. 实现响应拦截 | ||
3. 常见错误信息处理 | ||
4. 请求头设置 | ||
5. api 集中式管理 | ||
6. 重复发送请求 |
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,75 @@ | ||
/* | ||
* @Author: guokai05 | ||
* @Date: 2023-02-19 22:31:15 | ||
* @LastEditors: guokai05 | ||
* @LastEditTime: 2023-02-21 16:18:27 | ||
*/ | ||
import axios from 'axios'; | ||
import axiosRetry from 'axios-retry'; | ||
import {InternalAxiosRequestConfig, AxiosRequestConfig, AxiosResponse, AxiosError} from 'axios'; | ||
const whiteRetry = new Set(['ECONNABORTED', undefined, 0]); | ||
// import {baseURL} from '@/utils/variable'; | ||
|
||
// 创建 axios 请求实例 | ||
const serviceAxios = axios.create({ | ||
baseURL: '', // 接口请求地址 | ||
timeout: 15 * 1000, // 请求超时设置 | ||
withCredentials: false, // 跨域请求是否需要携带 cookie | ||
headers: { | ||
'Content-Type': 'application/json;charset=utf-8' | ||
}, | ||
validateStatus() { | ||
// 使用async-await,处理reject情况较为繁琐,所以全部返回resolve,在业务代码中处理异常 | ||
return true; | ||
} | ||
}); | ||
|
||
axiosRetry(serviceAxios, { | ||
retries: 2, // 重复请求次数 | ||
shouldResetTimeout: true, // 重置超时时间 | ||
retryDelay: (retryCount) => { | ||
return retryCount * 10000; // 重复请求延迟 | ||
}, | ||
retryCondition: (err) => { | ||
// true为打开自动发送请求,false为关闭自动发送请求 | ||
const {code, message} = err; | ||
return whiteRetry.has(<string>code) || message.includes('timeout'); | ||
} | ||
}); | ||
|
||
// 请求拦截器 | ||
serviceAxios.interceptors.request.use( | ||
(config: InternalAxiosRequestConfig) => { | ||
return config; | ||
}, | ||
(err: AxiosError) => { | ||
return Promise.reject(err); | ||
} | ||
); | ||
|
||
// 响应拦截器 | ||
serviceAxios.interceptors.response.use( | ||
(res: AxiosResponse) => { | ||
return res; | ||
}, | ||
(err: AxiosError) => { | ||
return Promise.reject(err); | ||
} | ||
); | ||
|
||
// 统一发起请求的函数 | ||
async function request<T>(options: AxiosRequestConfig) { | ||
try { | ||
const response = await serviceAxios.request<T>(options); | ||
const {status, data} = response; | ||
// 处理 HTTP 状态码 | ||
if (status < 200 || status >= 500) { | ||
return Promise.reject(); | ||
} | ||
return Promise.resolve(data); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
} | ||
|
||
export default request; |
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,4 @@ | ||
export interface pokemonOptions { | ||
offset?: number; | ||
limit: number; | ||
} |
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
Oops, something went wrong.