From 32afd40532949695dbdb5dd8bc0b026ca3402a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B4=9B=E5=B8=8C=E9=9B=85?= Date: Thu, 25 Apr 2024 08:39:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3baseUrl=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/request.ts | 19 +++++++++++++++---- typings/env.d.ts | 4 ++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index e3a7fac..4681b94 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -62,10 +62,21 @@ service.interceptors.response.use( ) export const baseUrl: string = (() => { - let port = import.meta.env.VITE_APP_BASE_URL - if (/:\d{1,5}/.test(port)) port = port.match(/(?:\d{1,5})/)?.groups?.port ?? '' - else port = `:${location.port}` - return `${location.protocol}//${location.hostname}${port}` + if (window.NGINX_BASE_URL) return window.NGINX_BASE_URL + + const mode = import.meta.env.VITE_APP_BASE_MODE + if (mode === 'port') { + const port = import.meta.env.VITE_APP_BASE_PORT || location.port + return `${location.protocol}//${location.hostname}:${port}` + } + if (mode === 'url') { + let url = import.meta.env.VITE_APP_BASE_URL + if (!url) url = `${location.protocol}//${location.host}` + return url + } + + //local + return `${location.protocol}//${location.host}` })() //导出service实例给页面调用 , config->页面的配置 diff --git a/typings/env.d.ts b/typings/env.d.ts index 0545982..c4ecbaa 100644 --- a/typings/env.d.ts +++ b/typings/env.d.ts @@ -9,5 +9,9 @@ declare global { interface ImportMeta { readonly env: ImportMetaEnv } + + interface Window { + readonly NGINX_BASE_URL: string | undefined + } } export {}