| | |
| | | import { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'; |
| | | import { AxiosRequestConfig, AxiosError, AxiosResponse} from 'axios'; |
| | | import ErrorMessageBox from '../errorService'; |
| | | import axios from 'axios'; |
| | | import _ from 'lodash'; |
| | | |
| | | const notRequireInterceptorErrorUrl=[ |
| | | const notRequireInterceptorErrorUrl = [ |
| | | '/otp/verify', |
| | | '/eService/authenticate', |
| | | '/login/validate/get_img_code', |
| | |
| | | }); |
| | | |
| | | service.interceptors.request.use( |
| | | (config:AxiosRequestConfig)=>{ |
| | | (config: AxiosRequestConfig) => { |
| | | loadingStart(); |
| | | addHttpHeader(config); |
| | | return config; |
| | | } |
| | | ); |
| | | |
| | | service.interceptors.response.use( |
| | | (response:AxiosResponse)=>{ |
| | | (response: AxiosResponse) => { |
| | | loadingFinish(); |
| | | return response; // maybe can use response.data |
| | | return response.data; |
| | | }, |
| | | (error:AxiosError)=>{ |
| | | (error: AxiosError) => { |
| | | loadingFinish(); |
| | | showErrorMessageBox(error) |
| | | return Promise.reject(error); |
| | | } |
| | | ); |
| | | |
| | | function addHttpHeader(config: AxiosRequestConfig): void { |
| | | config.headers = { |
| | | Authorization: 'Bearer ' + localStorage.getItem('id_token') |
| | | } |
| | | } |
| | | |
| | | function loadingStart(): void { |
| | | window.$nuxt.$loading.start(); |
| | | window.$nuxt.$loading.start(); |
| | | }; |
| | | |
| | | function loadingFinish(): void { |
| | | window.$nuxt.$loading.finish(); |
| | | window.$nuxt.$loading.finish(); |
| | | }; |
| | | |
| | | function showErrorMessageBox(error:any):void{ |
| | | function showErrorMessageBox(error: any): void { |
| | | // console.log('error', error, error.response); |
| | | if(!_.includes(notRequireInterceptorErrorUrl,error.config.url)){ |
| | | if (!_.includes(notRequireInterceptorErrorUrl, error.config.url)) { |
| | | switch (error.response.status) { |
| | | case 401: |
| | | Promise.all([ErrorMessageBox('登入逾時'),window.$nuxt.$store.dispatch('localStorage/actionStorageClear')]).then(()=>{ |
| | | Promise.all([ErrorMessageBox('登入逾時'), window.$nuxt.$store.dispatch('localStorage/actionStorageClear')]).then(() => { |
| | | _.isEqual(window.$nuxt.$route.name, 'index') ? location.reload() : window.$nuxt.$router.push('/'); |
| | | }); |
| | | break; |