Skip to content

Commit 9ccfd77

Browse files
committed
feat(axios): 完成axios封装
1 parent efcb0bb commit 9ccfd77

File tree

5 files changed

+122
-32
lines changed

5 files changed

+122
-32
lines changed

src/main.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @Author: wenlan
55
* @Date: 2022-01-13 15:26:43
66
* @LastEditors: wenlan
7-
* @LastEditTime: 2022-01-18 16:12:43
7+
* @LastEditTime: 2022-01-18 23:03:01
88
*/
99
import { createApp } from 'vue'
1010
import router from './router'
@@ -14,10 +14,15 @@ import MyRequest from './service'
1414
const app = createApp(App)
1515
app.use(router) // 挂载之前
1616
app.use(store)
17+
interface dataType {
18+
data: any
19+
returnCode: string
20+
success: boolean
21+
}
1722
app.mount('#app')
18-
MyRequest.request({
23+
MyRequest.get<dataType>({
1924
url: '/home/multidata',
20-
method: 'GET',
25+
showLoading: true,
2126
interceptors: {
2227
requestInterceptor: (config) => {
2328
console.log('单独请求拦截')
@@ -28,6 +33,8 @@ MyRequest.request({
2833
return res
2934
}
3035
}
36+
}).then((res) => {
37+
console.log(res)
3138
})
3239
// MyRequest.request({
3340
// url: '/home/multidata',

src/service/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @Author: wenlan
55
* @Date: 2022-01-16 16:57:17
66
* @LastEditors: wenlan
7-
* @LastEditTime: 2022-01-18 17:04:06
7+
* @LastEditTime: 2022-01-18 19:19:11
88
*/
99
import WDRequest from './request'
1010
import { TIME_OUT } from './request/config'

src/service/request/index.ts

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
* @Author: wenlan
55
* @Date: 2022-01-16 16:52:38
66
* @LastEditors: wenlan
7-
* @LastEditTime: 2022-01-18 16:09:29
7+
* @LastEditTime: 2022-01-18 23:02:05
88
*/
99
import axios from 'axios'
1010
import type { AxiosInstance } from 'axios'
1111
import type { WDAxiosRequestConfig, WDRequestInterceptors } from './type'
12+
import { ElLoading } from 'element-plus'
13+
const DEFAULT_LOADING = true
1214
class WDRequest {
1315
instance: AxiosInstance
16+
showLoading: boolean
1417
interceptors?: WDRequestInterceptors
18+
loading?: any
1519
constructor(config: WDAxiosRequestConfig) {
20+
//创建axios实例
1621
this.instance = axios.create(config)
22+
this.showLoading = config.showLoading ?? true
1723
this.interceptors = config.interceptors
18-
//实例拦截
24+
25+
//实例拦截操作
1926
this.instance.interceptors.request.use(
2027
this.interceptors?.requestInterceptor,
2128
this.interceptors?.requestIntetceptorCatch
@@ -24,40 +31,93 @@ class WDRequest {
2431
this.interceptors?.reponseInterceptor,
2532
this.interceptors?.reponseInterceptorCatch
2633
)
27-
//默认拦截
2834

35+
//默认类的拦截(全局拦截)
36+
37+
//请求拦截
2938
this.instance.interceptors.request.use(
3039
(config) => {
3140
console.log('默认请求拦截成功')
41+
if (this.showLoading) {
42+
this.loading = ElLoading.service({
43+
lock: true,
44+
text: '数据加载中',
45+
background: 'rgba(0, 0, 0, 0.7)',
46+
fullscreen: true
47+
})
48+
}
3249
return config
3350
},
3451
(err) => {
3552
console.log('默认请求拦截失败')
3653
return err
3754
}
3855
)
56+
//响应拦截
3957
this.instance.interceptors.response.use(
40-
(config) => {
41-
console.log('默认响应拦截成功')
42-
return config
58+
(res) => {
59+
setTimeout(() => {
60+
this.loading.close()
61+
}, 3000)
62+
const data = res.data
63+
if (data.returnCode === '-1000') {
64+
console.log('请求失败')
65+
} else {
66+
return data
67+
}
4368
},
4469
(err) => {
70+
if (err.response.status === 404) {
71+
console.log('返回状态码错误为404')
72+
}
4573
console.log('默认响应拦截失败')
4674
return err
4775
}
4876
)
4977
}
50-
request(config: WDAxiosRequestConfig): void {
51-
//单独请求拦截
52-
if (config.interceptors?.requestInterceptor) {
53-
config = config.interceptors.requestInterceptor(config)
54-
}
55-
this.instance.request(config).then((res) => {
56-
if (config.interceptors?.reponseInterceptor) {
57-
res = config.interceptors.reponseInterceptor(res)
78+
79+
request<T>(config: WDAxiosRequestConfig): Promise<T> {
80+
return new Promise((resolve, reject) => {
81+
//单独请求拦截
82+
if (config.interceptors?.requestInterceptor) {
83+
config = config.interceptors.requestInterceptor(config)
84+
}
85+
if (config.showLoading === false) {
86+
this.showLoading = config.showLoading
5887
}
59-
console.log(res)
88+
this.instance
89+
.request<any, T>(config)
90+
.then((res) => {
91+
if (config.interceptors?.reponseInterceptor) {
92+
res = config.interceptors.reponseInterceptor(res)
93+
}
94+
//确保不会影响下一个请求
95+
this.showLoading = DEFAULT_LOADING
96+
resolve(res)
97+
})
98+
.catch((err) => {
99+
//确保不会影响下一个请求
100+
this.showLoading = DEFAULT_LOADING
101+
reject(err)
102+
})
60103
})
61104
}
105+
106+
//GET
107+
get<T>(config: WDAxiosRequestConfig): Promise<T> {
108+
return this.request<T>({ ...config, method: 'GET' })
109+
}
110+
//PUT
111+
put<T>(config: WDAxiosRequestConfig): Promise<T> {
112+
return this.request<T>({ ...config, method: 'PUT' })
113+
}
114+
//POST
115+
post<T>(config: WDAxiosRequestConfig): Promise<T> {
116+
return this.request<T>({ ...config, method: 'POST' })
117+
}
118+
//DEL
119+
delete<T>(config: WDAxiosRequestConfig): Promise<T> {
120+
return this.request<T>({ ...config, method: 'DELETE' })
121+
}
62122
}
63123
export default WDRequest

src/service/request/type.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
* @Author: wenlan
55
* @Date: 2022-01-18 13:38:02
66
* @LastEditors: wenlan
7-
* @LastEditTime: 2022-01-18 13:38:02
7+
* @LastEditTime: 2022-01-18 22:52:35
88
*/
9-
import type { AxiosResponse, AxiosRequestConfig } from 'axios'
9+
import type { AxiosRequestConfig } from 'axios'
1010
export interface WDRequestInterceptors {
1111
requestInterceptor?: (config: AxiosRequestConfig) => AxiosRequestConfig
1212
requestIntetceptorCatch?: (err: any) => any
13-
reponseInterceptor?: (res: AxiosResponse) => AxiosResponse
13+
reponseInterceptor?: (res: any) => any
1414
reponseInterceptorCatch?: (err: any) => any
1515
}
1616

1717
export interface WDAxiosRequestConfig extends AxiosRequestConfig {
1818
interceptors?: WDRequestInterceptors
19+
showLoading?: boolean
1920
}

src/views/Main.vue

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,39 @@
44
* @Author: wenlan
55
* @Date: 2022-01-15 16:43:51
66
* @LastEditors: wenlan
7-
* @LastEditTime: 2022-01-15 23:16:23
7+
* @LastEditTime: 2022-01-18 21:07:30
88
-->
9-
<script lang="ts"></script>
10-
119
<template>
12-
<h2>main</h2>
13-
<el-button>hello world!</el-button>
14-
<el-alert title="success alert" type="success" effect="dark"> </el-alert>
15-
<el-alert title="info alert" type="info" effect="dark"> </el-alert>
16-
<el-alert title="warning alert" type="warning" effect="dark"> </el-alert>
17-
<el-alert title="error alert" type="error" effect="dark"> </el-alert>
10+
<el-button
11+
v-loading.fullscreen.lock="fullscreenLoading"
12+
type="primary"
13+
@click="openFullScreen1"
14+
>
15+
As a directive
16+
</el-button>
17+
<el-button type="primary" @click="openFullScreen2"> As a service </el-button>
1818
</template>
1919

20-
<style scoped></style>
20+
<script lang="ts" setup>
21+
import { ref } from 'vue'
22+
import { ElLoading } from 'element-plus'
23+
24+
const fullscreenLoading = ref(false)
25+
const openFullScreen1 = () => {
26+
fullscreenLoading.value = true
27+
setTimeout(() => {
28+
fullscreenLoading.value = false
29+
}, 2000)
30+
}
31+
32+
const openFullScreen2 = () => {
33+
const loading = ElLoading.service({
34+
lock: true,
35+
text: 'Loading',
36+
background: 'rgba(0, 0, 0, 0.7)'
37+
})
38+
setTimeout(() => {
39+
loading.close()
40+
}, 2000)
41+
}
42+
</script>

0 commit comments

Comments
 (0)