forked from wangrongding/github-university
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
38 lines (36 loc) · 904 Bytes
/
request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import axios from "axios";
// 创建axios实例
const request = axios.create({
timeout: 200000, // 请求超时时间
});
// request请求拦截器
request.interceptors.request.use(
(config) => {
const { data = {}, method } = config;
if (method === "post") {
config.data = data.data;
}
// get请求转参数key为params
if (method === "get" || method === "delete") {
config.params = data;
}
if (method === "put") {
config.data = { ...data.data };
}
return config;
},
(error) => error
);
// 请求成功回调
function successCallback(response) {
// console.log(response);
return Promise.resolve(response);
}
// 请求错误回调
function errorCallback(error) {
console.log(error);
return Promise.reject(error);
}
// response返回拦截器
request.interceptors.response.use(successCallback, errorCallback);
export default request;