Skip to content

Commit efcb0bb

Browse files
committed
feat(axios): 封装axios
1 parent d6b9fd0 commit efcb0bb

File tree

11 files changed

+165
-25
lines changed

11 files changed

+165
-25
lines changed

.env.development

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
env = 'development'
22
VITE_MODE_NAME=development
3+
VITE_APP_BASE_API =http://123.207.32.32:8000/
34
VITE_APP_ID=123456
45
VITE_AGENT_ID=123456
56
VITE_LOGIN_TEST=true
6-
VITE_RES_URL=https://www.baidu.com
7+
VITE_RES_URL=v1.hitokoto.cn
78
VITE_APP_TITLE=管理平台
89
VITE_EDITOR=vscode

.env.production

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ VITE_MODE_NAME=production
33
VITE_APP_ID="123456"
44
VITE_AGENT_ID=123456
55
VITE_LOGIN_TEST=false
6-
VITE_RES_URL=https://www.baidu.com
6+
VITE_RES_URL=v1.hitokoto.cn
77
VITE_APP_TITLE=管理平台
88
VITE_EDITOR=vscode

.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ VITE_MODE_NAME=test
22
VITE_APP_ID=123456
33
VITE_AGENT_ID=123456
44
VITE_LOGIN_TEST=true
5-
VITE_RES_URL=https://www.baidu.com
5+
VITE_RES_URL=v1.hitokoto.cn
66
VITE_APP_TITLE=管理平台
77
VITE_EDITOR=vscode

src/env.d.ts

Lines changed: 2 additions & 1 deletion
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-16 14:42:44
7+
* @LastEditTime: 2022-01-16 22:49:46
88
*/
99
/// <reference types="vite/client" />
1010

@@ -23,4 +23,5 @@ interface ImportMetaEnv {
2323
VITE_LOGIN_TEST: string
2424
VITE_RES_URL: string
2525
VITE_APP_TITLE: string
26+
VITE_APP_BASE_API: string
2627
}

src/main.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,32 @@
44
* @Author: wenlan
55
* @Date: 2022-01-13 15:26:43
66
* @LastEditors: wenlan
7-
* @LastEditTime: 2022-01-16 14:27:14
7+
* @LastEditTime: 2022-01-18 16:12:43
88
*/
99
import { createApp } from 'vue'
1010
import router from './router'
1111
import App from './App.vue'
1212
import store from './store'
13-
import './service/axios_demo'
13+
import MyRequest from './service'
1414
const app = createApp(App)
1515
app.use(router) // 挂载之前
1616
app.use(store)
1717
app.mount('#app')
18+
MyRequest.request({
19+
url: '/home/multidata',
20+
method: 'GET',
21+
interceptors: {
22+
requestInterceptor: (config) => {
23+
console.log('单独请求拦截')
24+
return config
25+
},
26+
reponseInterceptor: (res) => {
27+
console.log('单独响应拦截')
28+
return res
29+
}
30+
}
31+
})
32+
// MyRequest.request({
33+
// url: '/home/multidata',
34+
// method: 'GET'
35+
// })

src/service/axios_demo.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/service/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @Descripttion:
3+
* @version:
4+
* @Author: wenlan
5+
* @Date: 2022-01-16 16:57:17
6+
* @LastEditors: wenlan
7+
* @LastEditTime: 2022-01-18 17:04:06
8+
*/
9+
import WDRequest from './request'
10+
import { TIME_OUT } from './request/config'
11+
//创建实例
12+
const MyRequest = new WDRequest({
13+
timeout: TIME_OUT,
14+
baseURL: import.meta.env.VITE_APP_BASE_API,
15+
interceptors: {
16+
requestInterceptor: (config) => {
17+
// const token = ''
18+
// //携带token
19+
// if (token) {
20+
// config.headers['Authorization'] = 'Bearer ' + token
21+
// }
22+
console.log('实例请求拦截成功')
23+
return config
24+
},
25+
requestIntetceptorCatch: (err) => {
26+
console.log('实例请求拦截失败')
27+
return err
28+
},
29+
reponseInterceptor: (config) => {
30+
console.log('实例响应拦截成功')
31+
return config
32+
},
33+
reponseInterceptorCatch: (err) => {
34+
console.log('实例响应拦截失败')
35+
return err
36+
}
37+
}
38+
})
39+
export default MyRequest

src/service/request/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Descripttion:
3+
* @version:
4+
* @Author: wenlan
5+
* @Date: 2022-01-16 16:50:51
6+
* @LastEditors: wenlan
7+
* @LastEditTime: 2022-01-18 13:40:46
8+
*/
9+
//config
10+
const TIME_OUT = 10000
11+
export { TIME_OUT }

src/service/request/index.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @Descripttion:
3+
* @version:
4+
* @Author: wenlan
5+
* @Date: 2022-01-16 16:52:38
6+
* @LastEditors: wenlan
7+
* @LastEditTime: 2022-01-18 16:09:29
8+
*/
9+
import axios from 'axios'
10+
import type { AxiosInstance } from 'axios'
11+
import type { WDAxiosRequestConfig, WDRequestInterceptors } from './type'
12+
class WDRequest {
13+
instance: AxiosInstance
14+
interceptors?: WDRequestInterceptors
15+
constructor(config: WDAxiosRequestConfig) {
16+
this.instance = axios.create(config)
17+
this.interceptors = config.interceptors
18+
//实例拦截
19+
this.instance.interceptors.request.use(
20+
this.interceptors?.requestInterceptor,
21+
this.interceptors?.requestIntetceptorCatch
22+
)
23+
this.instance.interceptors.response.use(
24+
this.interceptors?.reponseInterceptor,
25+
this.interceptors?.reponseInterceptorCatch
26+
)
27+
//默认拦截
28+
29+
this.instance.interceptors.request.use(
30+
(config) => {
31+
console.log('默认请求拦截成功')
32+
return config
33+
},
34+
(err) => {
35+
console.log('默认请求拦截失败')
36+
return err
37+
}
38+
)
39+
this.instance.interceptors.response.use(
40+
(config) => {
41+
console.log('默认响应拦截成功')
42+
return config
43+
},
44+
(err) => {
45+
console.log('默认响应拦截失败')
46+
return err
47+
}
48+
)
49+
}
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)
58+
}
59+
console.log(res)
60+
})
61+
}
62+
}
63+
export default WDRequest

src/service/request/type.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Descripttion:
3+
* @version:
4+
* @Author: wenlan
5+
* @Date: 2022-01-18 13:38:02
6+
* @LastEditors: wenlan
7+
* @LastEditTime: 2022-01-18 13:38:02
8+
*/
9+
import type { AxiosResponse, AxiosRequestConfig } from 'axios'
10+
export interface WDRequestInterceptors {
11+
requestInterceptor?: (config: AxiosRequestConfig) => AxiosRequestConfig
12+
requestIntetceptorCatch?: (err: any) => any
13+
reponseInterceptor?: (res: AxiosResponse) => AxiosResponse
14+
reponseInterceptorCatch?: (err: any) => any
15+
}
16+
17+
export interface WDAxiosRequestConfig extends AxiosRequestConfig {
18+
interceptors?: WDRequestInterceptors
19+
}

0 commit comments

Comments
 (0)