From c75b2fc79f5aa811ba3f5d028633f3765a017bbb Mon Sep 17 00:00:00 2001
From: Mila <Mila@pollex.com.tw>
Date: 星期二, 30 十一月 2021 19:30:07 +0800
Subject: [PATCH] Merge branch 'master' of https://192.168.0.10:8443/r/pcalife/PAM

---
 PAMapp/assets/ts/api/share.ts |   67 +++++++++++++++++++++++++++------
 1 files changed, 55 insertions(+), 12 deletions(-)

diff --git a/PAMapp/assets/ts/api/share.ts b/PAMapp/assets/ts/api/share.ts
index 680c065..6e92bac 100644
--- a/PAMapp/assets/ts/api/share.ts
+++ b/PAMapp/assets/ts/api/share.ts
@@ -1,18 +1,61 @@
-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);
-});
\ No newline at end of file
+  }
+);
+
+function loadingStart(): void {
+    window.$nuxt.$loading.start();
+};
+
+function loadingFinish(): void {
+    window.$nuxt.$loading.finish();
+};
+
+function showErrorMessageBox(error:any):void{
+  // console.log('error', error, error.response);
+  if(!_.includes(notRequireInterceptorErrorUrl,error.config.url)){
+    switch (error.response.status) {
+      case 401:
+        ErrorMessageBox('���暹��').then(()=>{
+          window.$nuxt.$store.commit('localStorage/storageClear');
+          location.href='/';
+        });
+        break;
+      default:
+        ErrorMessageBox('', error);
+        break;
+    }
+
+  }
+};

--
Gitblit v1.8.0