import { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios';
|
import ErrorMessageBox from '../errorService';
|
import LocalStorage from '~/store/localStorage';
|
import axios from 'axios';
|
import _ from 'lodash';
|
|
const notRequireInterceptorErrorUrl=[
|
'/otp/verify',
|
'/eService/authenticate',
|
'/login/validate/get_img_code',
|
'/login/validate/verify_img_code',
|
];
|
|
export const service = axios.create({
|
baseURL: process.env.BASE_URL,
|
withCredentials: true
|
});
|
|
service.interceptors.request.use(
|
(config:AxiosRequestConfig)=>{
|
loadingStart();
|
return config;
|
}
|
);
|
|
service.interceptors.response.use(
|
(response:AxiosResponse)=>{
|
loadingFinish();
|
return response; // maybe can use response.data
|
},
|
(error:AxiosError)=>{
|
loadingFinish();
|
showErrorMessageBox(error)
|
return Promise.reject(error);
|
}
|
);
|
|
function loadingStart(): void {
|
window.$nuxt.$loading.start();
|
};
|
|
function loadingFinish(): void {
|
window.$nuxt.$loading.finish();
|
};
|
|
function showErrorMessageBox(error:any):void{
|
if(!_.includes(notRequireInterceptorErrorUrl,error.config.url)){
|
switch (error.response.status) {
|
case 401:
|
ErrorMessageBox('登入逾時');
|
window.$nuxt.$router.push('/');
|
window.$nuxt.$store.commit('localStorage/storageClear');
|
break;
|
default:
|
ErrorMessageBox();
|
break;
|
}
|
|
}
|
};
|