http.js
import axios from "axios";
import { Message } from "element-ui";
// 请求拦截
axios.interceptors.request.use(
(config) => {
if (!config.headers) {
config.headers = {
"Content-Type": "application/json",
};
}
config.headers["X-Requested-With"] = "XMLHttpRequest";
return config;
},
(err) => {
return Promise.reject(err);
}
);
// 响应拦截
axios.interceptors.response.use(
(response) => {
if (typeof response.data == "string") {
response.data = JSON.parse(response.data);
} else if (typeof response.data == "object") {
if (response.data.size) {
return response.data;
} else if (response.data.statusCode == 401) {
location.replace(`#/login`);
} else if (response.data.statusCode != 0) {
return Promise.reject(response.data); //统一拦截处理请求异常
} else {
return response.data;
}
}
},
(error) => {
let errorStatus = error.response ? error.response.status : error.statusCode;
let errorText = error.response ? error.response.message : error.message;
if (errorStatus == 504 && !errorText)
switch (errorStatus) {
case 400:
console.info("400");
break;
default:
break;
}
return Promise.reject(error);
}
);
export default axios;
Api.js
import axios from "../http";
import qs from "qs";
const api = {
getAccess12Month: "check/internalDataAccess12Month",
};
for (let key in api) {
api[key] = `api/${api[key]}`;
}
export const getAccess12Month = () => {
return axios.get(api.getAccess12Month);
};