import axios from 'axios';
const whiteList = ['post /api/login'];
const resetFetchList = [];
let isLogin = false;
axios.interceptors.request.use(function (config) {
const requestMark = `${config.method} ${config.url}`;
const flag = resetFetchList.findIndex(item=>item.requestMark === requestMark);
if (flag !== -1) {
resetFetchList.splice(flag,1);
}
const cancelToken = axios.CancelToken;
const source = cancelToken.source();
config.cancelToken = source.token;
config.cancel = source.cancel;
config.requestMark = requestMark;
if (!whiteList.includes(requestMark) && !isLogin) {
resetFetchList.push(config);
config.cancel();
}
return config;
}, function (error) {
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
if(response.config.requestMark === 'post /api/login' ) {
isLogin = true;
resetFetchList.forEach(item => {
axios.request(item);
})
}
return response;
}, function (error) {
return Promise.reject(error);
});
export default axios;