From 4d006611632b09415c5699c21c82ee5611a80b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BC=BA?= <1206709430@qq.com> Date: Fri, 12 Jan 2024 00:28:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96getBaseURL=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/utils/baseUrl.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/web/src/utils/baseUrl.ts b/web/src/utils/baseUrl.ts index d2e1af6..6b55c27 100644 --- a/web/src/utils/baseUrl.ts +++ b/web/src/utils/baseUrl.ts @@ -3,8 +3,12 @@ import { pluginsAll } from '/@/views/plugins/index'; /** * @description 校验是否为租户模式。租户模式把域名替换成 域名 加端口 */ -export const getBaseURL = function (url:string) { +export const getBaseURL = function (url: null | string = null, isHost: null | boolean = null) { let baseURL = import.meta.env.VITE_API_URL as any; + // 如果需要host返回,时,返回地址前缀加http地址 + if (isHost && !baseURL.startsWith('http')) { + baseURL = window.location.protocol + '//' + window.location.host + baseURL + } let param = baseURL.split('/')[3] || ''; // @ts-ignore if (pluginsAll && pluginsAll.indexOf('dvadmin3-tenants-web') !== -1 && (!param || baseURL.startsWith('/'))) { @@ -26,14 +30,13 @@ export const getBaseURL = function (url:string) { baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' : '') + location.port + baseURL; } } - if(url){ + if (url) { const regex = /^(http|https):\/\//; - if(regex.test(url)){ + if (regex.test(url)) { return url - }else{ - if(url.startsWith('/')){ - return baseURL + url; - } + } else { + // js判断是否是斜杠结尾 + return baseURL.replace(/\/$/, '') + '/' + url.replace(/^\//, ''); } } if (!baseURL.endsWith('/')) {