Skip to content

Commit 27cd67f

Browse files
committed
feat: ✨ 新增网络请求Axios v1
1 parent c5269c7 commit 27cd67f

File tree

16 files changed

+434
-25
lines changed

16 files changed

+434
-25
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<h1 align="center">react 模板</h1>
22
<br/>
33

4+
<center>
5+
<img src='./docs/images/home.png' style='width: 75%'/>
6+
</center>
7+
48
## 👨🏻‍💻 项目说明
59

610
- react 模板, 一个比 CRA 更丰富的模板
@@ -14,11 +18,10 @@
1418
- React Hook
1519
- TypeScript v4
1620
- webpack v5
17-
- axios
21+
- axios v1
1822
- mobx v6
1923
- mobx-react-lite v3
2024
- react-router-dom v6
21-
- postcss-px-to-viewport
2225

2326
## ⌛️ 安装项目依赖
2427

@@ -127,7 +130,10 @@ merge 🔀 合并分支
127130
128131
```
129132

130-
## 技术栈说明
133+
## 🚀 陆续新增内容:
131134

132-
- React18
133-
- TypeScript
135+
- ① 样式 ✅ 2023/2/6 日提交 [config: 🔧  新增样式文件(css/less/sass/postCss)处理](https://github.com/guokaigdg/react-enterprise-template/commit/11fb415bac609dfa7474a1ee2db93ccb4a350a51)
136+
- ② 代码规范 ✅ 2023/2/7 日提交 [config: 🔧  新增 Prettier/ESlint/StyleLint/EditorConfig 代码规范](https://github.com/guokaigdg/react-enterprise-template/commit/87dd1ca333f81203dd245a6eb40479a0745f096f)
137+
- ③ 路由 ✅ 2023/2/8 日提交 [config: 🔧 新增路由管理 react-router-dom v6](https://github.com/guokaigdg/react-enterprise-template/commit/239446d0709eb52bad2b48af4983eef91c49f60d)
138+
- ④ 网络请求 ✅ 2023/2/21 日提交
139+
- ⑤ 数据共享 ✅ [feature: ✨  新增状态管理 Mobx v6](https://github.com/guokaigdg/react-enterprise-template/commit/992e1884943d4f8bda836f48c60df473418397d7)

docs/images/home.png

4.02 MB
Loading

package-lock.json

+60-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
"webpackbar": "^5.0.2"
106106
},
107107
"dependencies": {
108+
"axios": "^1.3.3",
109+
"axios-retry": "^3.4.0",
108110
"mobx": "^6.8.0",
109111
"mobx-react-lite": "^3.4.0",
110112
"react": "^18.2.0",

src/api/index.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @file: 所有的接口列表
3+
* 宝可梦api https://pokeapi.co/about
4+
*/
5+
6+
import http from '../http';
7+
import {pokemonOptions} from '@/interface/http';
8+
9+
/**
10+
* @function get
11+
* @description 请求测试
12+
*/
13+
14+
export function fetchPokemon(data: pokemonOptions) {
15+
return http({
16+
url: ' https://pokeapi.co/api/v2/pokemon',
17+
params: data
18+
});
19+
}
20+
/**
21+
* @function post
22+
* @description 请求测试
23+
*/
24+
25+
export function fetchPostTest(data: any) {
26+
return http({
27+
url: '/xxx/list',
28+
method: 'post',
29+
data
30+
});
31+
}

src/http/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1. 实现请求拦截
2+
2. 实现响应拦截
3+
3. 常见错误信息处理
4+
4. 请求头设置
5+
5. api 集中式管理
6+
6. 重复发送请求

src/http/index.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* @Author: guokai05
3+
* @Date: 2023-02-19 22:31:15
4+
* @LastEditors: guokai05
5+
* @LastEditTime: 2023-02-21 16:18:27
6+
*/
7+
import axios from 'axios';
8+
import axiosRetry from 'axios-retry';
9+
import {InternalAxiosRequestConfig, AxiosRequestConfig, AxiosResponse, AxiosError} from 'axios';
10+
const whiteRetry = new Set(['ECONNABORTED', undefined, 0]);
11+
// import {baseURL} from '@/utils/variable';
12+
13+
// 创建 axios 请求实例
14+
const serviceAxios = axios.create({
15+
baseURL: '', // 接口请求地址
16+
timeout: 15 * 1000, // 请求超时设置
17+
withCredentials: false, // 跨域请求是否需要携带 cookie
18+
headers: {
19+
'Content-Type': 'application/json;charset=utf-8'
20+
},
21+
validateStatus() {
22+
// 使用async-await,处理reject情况较为繁琐,所以全部返回resolve,在业务代码中处理异常
23+
return true;
24+
}
25+
});
26+
27+
axiosRetry(serviceAxios, {
28+
retries: 2, // 重复请求次数
29+
shouldResetTimeout: true, // 重置超时时间
30+
retryDelay: (retryCount) => {
31+
return retryCount * 10000; // 重复请求延迟
32+
},
33+
retryCondition: (err) => {
34+
// true为打开自动发送请求,false为关闭自动发送请求
35+
const {code, message} = err;
36+
return whiteRetry.has(<string>code) || message.includes('timeout');
37+
}
38+
});
39+
40+
// 请求拦截器
41+
serviceAxios.interceptors.request.use(
42+
(config: InternalAxiosRequestConfig) => {
43+
return config;
44+
},
45+
(err: AxiosError) => {
46+
return Promise.reject(err);
47+
}
48+
);
49+
50+
// 响应拦截器
51+
serviceAxios.interceptors.response.use(
52+
(res: AxiosResponse) => {
53+
return res;
54+
},
55+
(err: AxiosError) => {
56+
return Promise.reject(err);
57+
}
58+
);
59+
60+
// 统一发起请求的函数
61+
async function request<T>(options: AxiosRequestConfig) {
62+
try {
63+
const response = await serviceAxios.request<T>(options);
64+
const {status, data} = response;
65+
// 处理 HTTP 状态码
66+
if (status < 200 || status >= 500) {
67+
return Promise.reject();
68+
}
69+
return Promise.resolve(data);
70+
} catch (error) {
71+
return Promise.reject(error);
72+
}
73+
}
74+
75+
export default request;

src/interface/http.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface pokemonOptions {
2+
offset?: number;
3+
limit: number;
4+
}

src/router/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const About = Suspenselazy(() => import(/* webpackChunkName:"about" */ '@/view/A
1515
const routes: RouteObject[] = [
1616
{
1717
path: '/',
18-
element: <Navigate to='home' /> // 重定向
18+
element: <Navigate to='home/two' /> // 重定向
1919
},
2020
{
2121
path: 'home',

src/store/global/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1-
import {makeAutoObservable} from 'mobx';
1+
import {makeAutoObservable, runInAction} from 'mobx';
2+
import {fetchPokemon} from '@/api';
3+
import {pokemonOptions} from '@/interface/http';
24

35
class Global {
46
constructor() {
57
makeAutoObservable(this);
68
}
79
count = 0;
810
name = 'react';
11+
data: any = [];
12+
loading = true;
13+
914
addCount = () => {
1015
this.count++;
1116
};
1217
setName = (data: string) => {
1318
this.name = data;
1419
};
20+
21+
getFetchGetTest = async (params: pokemonOptions) => {
22+
try {
23+
const result: any = await fetchPokemon(params);
24+
const {results} = result;
25+
runInAction(() => {
26+
this.data = results;
27+
this.loading = false;
28+
});
29+
} catch (err) {
30+
console.log(err);
31+
this.loading = false;
32+
}
33+
};
1534
}
1635

1736
const globalStore = new Global();

0 commit comments

Comments
 (0)