| | |
| | | import axios from 'axios'; |
| | | import { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'; |
| | | import { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'; |
| | | import { MessageBox } from 'element-ui'; |
| | | |
| | | export const service = axios.create({ |
| | | baseURL: process.env.BASE_URL |
| | | }) |
| | | |
| | | service.interceptors.request.use(function (config: AxiosRequestConfig) { |
| | | loadingStart(); |
| | | service.interceptors.request.use( |
| | | (config:AxiosRequestConfig)=>{ |
| | | loadingStart(); |
| | | return config; |
| | | }, function (error: AxiosError) { |
| | | loadingFinish(); |
| | | return Promise.reject(error); |
| | | }); |
| | | } |
| | | ); |
| | | |
| | | service.interceptors.response.use(function (response: AxiosResponse) { |
| | | loadingFinish(); |
| | | return response; |
| | | }, function (error: AxiosError) { |
| | | loadingFinish(); |
| | | service.interceptors.response.use( |
| | | (response:AxiosResponse)=>{ |
| | | loadingFinish(); |
| | | return response; // maybe can use response.data |
| | | }, |
| | | (error:AxiosError)=>{ |
| | | loadingFinish(); |
| | | openErrorMessage(); |
| | | return Promise.reject(error); |
| | | }); |
| | | } |
| | | ); |
| | | |
| | | |
| | | |
| | | function loadingStart(): void { |
| | | setTimeout(() => { |
| | | window.$nuxt.$loading.start(); |
| | | }); |
| | | } |
| | | |
| | | function loadingFinish(): void { |
| | | window.$nuxt.$loading.finish(); |
| | | } |
| | | |
| | | function openErrorMessage():void{ |
| | | MessageBox({ |
| | | message: '系統發生錯誤', |
| | | showClose:false, |
| | | showConfirmButton:true, |
| | | confirmButtonText:'確認', |
| | | customClass:'pam-message-box', |
| | | closeOnClickModal:false, |
| | | }); |
| | | } |