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({
|
2021-02-28 14:50:52 +08:00
|
|
|
baseURL: "",
|
|
|
|
timeout: 10 * 1000, // 请求超时时间
|
2020-05-01 21:30:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
service.interceptors.request.use(
|
2021-02-28 14:50:52 +08:00
|
|
|
(config) => {
|
|
|
|
if (/^(post)|(put)|(delete)$/i.test(config.method)) {
|
|
|
|
if (config.data && config.data.upload) {
|
|
|
|
config.headers["Content-Type"] = "multipart/form-data";
|
|
|
|
}
|
2020-05-01 21:30:25 +08:00
|
|
|
}
|
2021-02-28 14:50:52 +08:00
|
|
|
return config;
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
Promise.reject(error);
|
|
|
|
}
|
2020-05-01 21:30:25 +08:00
|
|
|
);
|
|
|
|
|
2020-10-20 19:43:11 +08:00
|
|
|
service.interceptors.response.use(
|
2021-02-28 14:50:52 +08:00
|
|
|
(res) => {
|
|
|
|
return res.data ? res.data : Promise.reject(res);
|
|
|
|
},
|
|
|
|
(error) => Promise.reject(error)
|
2020-10-20 19:43:11 +08:00
|
|
|
);
|
2020-05-01 21:30:25 +08:00
|
|
|
|
2020-10-20 19:43:11 +08:00
|
|
|
export default service;
|