feat(@vben/web-antd): support tenant-code subdomain routing
All checks were successful
Build and Deploy TenantUI / build-and-deploy (push) Successful in 47s

This commit is contained in:
2026-02-24 16:00:10 +08:00
parent 274e1eb187
commit 3e55697532
2 changed files with 35 additions and 11 deletions

View File

@@ -1,7 +1,7 @@
VITE_BASE=/
# 接口地址
VITE_GLOB_API_URL=https://mock-napi.vben.pro/api
VITE_GLOB_API_URL=https://api-tenant.laosankeji.com/api/tenant/v1
# 是否开启压缩,可以设置为 none, brotli, gzip
VITE_COMPRESS=none

View File

@@ -20,10 +20,16 @@ import { useAuthStore } from '#/store';
import { refreshTokenApi } from './core';
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
const TENANT_HEADER_KEY = 'X-Tenant-Id';
const TENANT_ID_HEADER_KEY = 'X-Tenant-Id';
const TENANT_CODE_HEADER_KEY = 'X-Tenant-Code';
const TENANT_STORAGE_KEY = 'sys-tenant-id';
const DEV_TENANT_ID = import.meta.env.DEV ? import.meta.env.VITE_TENANT_ID : '';
interface TenantHeaderPayload {
tenantCode?: string;
tenantId?: string;
}
function getHeaderValue(headers: any, key: string) {
if (!headers) {
return '';
@@ -49,16 +55,31 @@ function setHeaderValue(headers: any, key: string, value: string) {
headers[key] = value;
}
function resolveTenantId() {
function resolveTenantHeaderPayload(): TenantHeaderPayload {
const hostname = window.location.hostname;
const hostnameParts = hostname.split('.').filter(Boolean);
const isIpAddress = /^\d+\.\d+\.\d+\.\d+$/.test(hostname);
const subdomainTenantId =
const subdomainTenant =
hostnameParts.length > 2 && !isIpAddress ? hostnameParts[0] : '';
const storageTenantId = localStorage.getItem(TENANT_STORAGE_KEY) || '';
if (subdomainTenant) {
if (/^\d+$/.test(subdomainTenant)) {
return { tenantId: subdomainTenant };
}
return subdomainTenantId || storageTenantId || DEV_TENANT_ID || '';
return { tenantCode: subdomainTenant };
}
const storageTenantId = localStorage.getItem(TENANT_STORAGE_KEY) || '';
if (storageTenantId) {
return { tenantId: storageTenantId };
}
if (DEV_TENANT_ID) {
return { tenantId: DEV_TENANT_ID };
}
return {};
}
function createRequestClient(baseURL: string, options?: RequestClientOptions) {
@@ -128,11 +149,14 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) {
}
setHeaderValue(config.headers, 'Accept-Language', preferences.app.locale);
const headerTenantId = getHeaderValue(config.headers, TENANT_HEADER_KEY);
if (!headerTenantId) {
const tenantId = resolveTenantId();
if (tenantId) {
setHeaderValue(config.headers, TENANT_HEADER_KEY, String(tenantId));
const headerTenantId = getHeaderValue(config.headers, TENANT_ID_HEADER_KEY);
const headerTenantCode = getHeaderValue(config.headers, TENANT_CODE_HEADER_KEY);
if (!headerTenantId && !headerTenantCode) {
const { tenantCode, tenantId } = resolveTenantHeaderPayload();
if (tenantCode) {
setHeaderValue(config.headers, TENANT_CODE_HEADER_KEY, tenantCode);
} else if (tenantId) {
setHeaderValue(config.headers, TENANT_ID_HEADER_KEY, tenantId);
}
}