保誠-保戶業務員媒合平台
[ Update ] : 修正 http header 統一由攔截器去加入以及回傳資料改為 response.data
修改1個檔案
29 ■■■■■ 已變更過的檔案
PAMapp/assets/ts/api/share.ts 29 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
PAMapp/assets/ts/api/share.ts
@@ -1,9 +1,9 @@
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',
@@ -16,38 +16,45 @@
});
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;