-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
53 lines (43 loc) · 1.69 KB
/
request.js
File metadata and controls
53 lines (43 loc) · 1.69 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Todo 在实际业务中修改该文件, 在 openapi-typescript-cli 生成的代码时, 会检测这个文件, 不会覆盖
// 该文件是对axios的封装, 可以在这里添加请求拦截器, 响应拦截器, 也可以在这里添加全局的错误处理
// 也可以在这里添加全局的loading, toast等
// 有问题欢迎联系 作者微信: zhutty
import axios from 'axios'
let instance = axios.create({});
instance.defaults.baseURL = '/';
instance.defaults.headers.common['Authorization'] = '';
instance.defaults.headers.post['Content-Type'] = 'application/json';
instance.defaults.timeout = 10000;
instance.interceptors.request.use(
(config) => {
console.log('请求拦截', config)
return config;
},
(error) => {
return Promise.reject();
}
);
instance.interceptors.response.use(
(res) => {
if (res.status === 200) {
if (res.data.status) {
// 在接收到响应数据之前可以进行一些处理,例如解析响应数据、错误处理等
const contentType = res.headers['content-type'];
if (contentType === 'application/octet-stream' ||
contentType === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
// 注意:Blob类型文件下载需要请求头参数添加 responseType:'blob' 下载 导出等功能需要
// return downloadFile(response)
} else {
return Promise.resolve(res.data);
}
}
}
},
(error) => {
return error
}
);
export function updateInstance(params) {
instance = {...instance, ...params}
}
export default instance