| | |
| | | import axios from 'axios'; |
| | | import { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'; |
| | | import ErrorMessageBox from '../errorService'; |
| | | 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 |
| | | }) |
| | | |
| | | service.interceptors.request.use(function (config: AxiosRequestConfig) { |
| | | return config; |
| | | }, function (error: AxiosError) { |
| | | return Promise.reject(error); |
| | | baseURL: process.env.BASE_URL, |
| | | withCredentials: true |
| | | }); |
| | | |
| | | service.interceptors.response.use(function (response: AxiosResponse) { |
| | | return response; |
| | | }, function (error: AxiosError) { |
| | | 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('登入逾時').then(()=>{ |
| | | location.href='/'; |
| | | window.$nuxt.$store.commit('localStorage/storageClear'); |
| | | }); |
| | | break; |
| | | default: |
| | | ErrorMessageBox(); |
| | | break; |
| | | } |
| | | |
| | | } |
| | | }; |