| | |
| | | import { AxiosRequestConfig, AxiosError, AxiosResponse} from 'axios'; |
| | | import ErrorMessageBox from '../errorService'; |
| | | import axios from 'axios'; |
| | | import _ from 'lodash'; |
| | | |
| | | import messageBoxService from './message-box.service'; |
| | | |
| | | const notRequireInterceptorErrorUrl = [ |
| | | '/otp/verify', |
| | |
| | | withCredentials: true |
| | | }); |
| | | |
| | | let apiNumber = 0; |
| | | |
| | | http.interceptors.request.use( |
| | | (config: AxiosRequestConfig) => { |
| | | apiNumber += 1; |
| | | loadingStart(); |
| | | addHttpHeader(config); |
| | | return config; |
| | |
| | | |
| | | http.interceptors.response.use( |
| | | (response: AxiosResponse) => { |
| | | loadingFinish(); |
| | | apiNumber -= 1; |
| | | if (apiNumber === 0) { |
| | | loadingFinish(); |
| | | } |
| | | return response; |
| | | }, |
| | | (error: AxiosError) => { |
| | | loadingFinish(); |
| | | apiNumber -= 1; |
| | | if (apiNumber === 0) { |
| | | loadingFinish(); |
| | | } |
| | | showErrorMessageBox(error) |
| | | return Promise.reject(error); |
| | | } |
| | |
| | | if (!_.includes(notRequireInterceptorErrorUrl, error.config.url)) { |
| | | switch (error.response.status) { |
| | | case 401: |
| | | Promise.all([ErrorMessageBox('登入逾時'), window.$nuxt.$store.dispatch('localStorage/actionStorageClear')]).then(() => { |
| | | Promise.all([messageBoxService.showErrorMessage('登入逾時'), window.$nuxt.$store.dispatch('localStorage/actionStorageClear')]).then(() => { |
| | | _.isEqual(window.$nuxt.$route.name, 'index') ? location.reload() : window.$nuxt.$router.push('/'); |
| | | }); |
| | | break; |
| | | |
| | | default: |
| | | ErrorMessageBox('', error); |
| | | messageBoxService.showErrorMessage('', error); |
| | | break; |
| | | } |
| | | } |