md/src/api/fetch.js

31 lines
679 B
JavaScript
Raw Normal View History

2020-10-20 19:43:11 +08:00
import axios from "axios";
2020-05-01 21:30:25 +08:00
// 创建axios实例
const service = axios.create({
2020-10-20 19:43:11 +08:00
baseURL: "",
timeout: 10 * 1000, // 请求超时时间
2020-05-01 21:30:25 +08:00
});
service.interceptors.request.use(
2020-10-20 19:43:11 +08:00
(config) => {
2020-05-01 21:30:25 +08:00
if (/^(post)|(put)|(delete)$/i.test(config.method)) {
if (config.data && config.data.upload) {
2020-10-20 19:43:11 +08:00
config.headers["Content-Type"] = "multipart/form-data";
2020-05-01 21:30:25 +08:00
}
}
return config;
2020-10-20 19:43:11 +08:00
},
(error) => {
2020-05-01 21:30:25 +08:00
Promise.reject(error);
}
);
2020-10-20 19:43:11 +08:00
service.interceptors.response.use(
(res) => {
return res.data ? res.data : Promise.reject(res);
},
(error) => Promise.reject(error)
);
2020-05-01 21:30:25 +08:00
2020-10-20 19:43:11 +08:00
export default service;