保誠-保戶業務員媒合平台
Tomas
2023-12-25 f065760fa7df1f88747395ab4b55349ce8b2faf0
PAMapp/shared/services/httpClient.ts
@@ -1,6 +1,4 @@
import { AxiosRequestConfig, AxiosError, AxiosResponse} from 'axios';
import axios from 'axios';
import _ from 'lodash';
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
import messageBoxService from './message-box.service';
@@ -13,9 +11,21 @@
  '/api/access_analysis/insert'
];
function getBaseUrl(): string {
  const baseUrl = process.env.BASE_URL;
  if (!baseUrl) {
    throw new Error('BASE_URL is not defined in process.env');
  }
  // const pattern = /^(https?:\/\/)[\w.-]+(:\d+)?/i; // 更嚴格的URL驗證
  // if (!pattern.test(baseUrl)) {
  //   throw new Error('Invalid BASE_URL');
  // }
  // 不需要再清理URL,因為它已經是絕對URL
  return baseUrl;
}
export const http = axios.create({
  baseURL: process.env.BASE_URL,
  withCredentials: true,
  baseURL: getBaseUrl(), // 使用函數動態獲取baseURL
  withCredentials: true
});
let apiNumber = 0;
@@ -74,11 +84,15 @@
      messageBoxService.showErrorMessage('', error);
      return
    }
    if (!_.includes(notRequireInterceptorErrorUrl, error.config.url)) {
    if (!notRequireInterceptorErrorUrl.includes(error.config.url)) {
      switch (error.response.status) {
        case 401:
          Promise.all([messageBoxService.showErrorMessage('登入逾時'), window.$nuxt.$store.dispatch('localStorage/actionStorageClear')]).then(() => {
            _.isEqual(window.$nuxt.$route.name, 'index') ? location.reload() : window.$nuxt.$router.push('/');
            if (window.$nuxt.$route.name === 'index') {
              location.reload();
            } else {
              window.$nuxt.$router.push('/');
            }
          });
          break;