From b84f7e4c72bd91ce81f45db4fa95258a1e12199d Mon Sep 17 00:00:00 2001 From: ChenZhaoYu <790348264@qq.com> Date: Wed, 22 Feb 2023 22:55:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=BF=98=E5=8E=9F=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 4 +++- src/typings/env.d.ts | 2 +- src/utils/request/axios.ts | 2 +- vite.config.ts | 11 +++++++---- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.env b/.env index 0d8d53f..a9b3cf5 100644 --- a/.env +++ b/.env @@ -1,5 +1,7 @@ # Glob API URL -VITE_GLOB_API_URL=http://localhost:3002 +VITE_GLOB_API_URL=/api + +VITE_APP_API_BASE_URL=http://localhost:3002/ # Glob API Timeout (ms) VITE_GLOB_API_TIMEOUT=100000 diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index f390ca6..54408d7 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -3,5 +3,5 @@ interface ImportMetaEnv { readonly VITE_GLOB_API_URL: string; readonly VITE_GLOB_API_TIMEOUT: string; - readonly VITE_GLOB_HTTP_PROXY: 'Y' | 'N'; + readonly VITE_APP_API_BASE_URL: string; } diff --git a/src/utils/request/axios.ts b/src/utils/request/axios.ts index ab7b991..06f993c 100644 --- a/src/utils/request/axios.ts +++ b/src/utils/request/axios.ts @@ -1,7 +1,7 @@ import axios, { type AxiosResponse } from 'axios' const service = axios.create({ - baseURL: import.meta.env.VITE_GLOB_HTTP_PROXY === 'Y' ? '/api' : import.meta.env.VITE_GLOB_API_URL, + baseURL: import.meta.env.VITE_GLOB_API_URL, timeout: !isNaN(+import.meta.env.VITE_GLOB_API_TIMEOUT) ? Number(import.meta.env.VITE_GLOB_API_TIMEOUT) : 60 * 1000, }) diff --git a/vite.config.ts b/vite.config.ts index d96f9a9..e056d05 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,13 +1,10 @@ import path from 'path' import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' -import { createViteProxy } from './config' export default defineConfig((env) => { const viteEnv = loadEnv(env.mode, process.cwd()) as unknown as ImportMetaEnv - const isOpenProxy = viteEnv.VITE_GLOB_HTTP_PROXY === 'Y' - return { resolve: { alias: { @@ -19,7 +16,13 @@ export default defineConfig((env) => { host: '0.0.0.0', port: 1002, open: false, - proxy: createViteProxy(isOpenProxy, viteEnv), + proxy: { + '/api': { + target: viteEnv.VITE_APP_API_BASE_URL, + changeOrigin: true, // 允许跨域 + rewrite: path => path.replace('/api/', '/'), + }, + }, }, build: { reportCompressedSize: false,