feat: 优化getBaseURL函数

This commit is contained in:
李强
2024-01-12 00:28:52 +08:00
parent 97737c3ef1
commit 4d00661163

View File

@@ -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('/')) {