feat(@vben/web-antd): support tenant-code subdomain routing
All checks were successful
Build and Deploy TenantUI / build-and-deploy (push) Successful in 47s
All checks were successful
Build and Deploy TenantUI / build-and-deploy (push) Successful in 47s
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
VITE_BASE=/
|
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
|
# 是否开启压缩,可以设置为 none, brotli, gzip
|
||||||
VITE_COMPRESS=none
|
VITE_COMPRESS=none
|
||||||
|
|||||||
@@ -20,10 +20,16 @@ import { useAuthStore } from '#/store';
|
|||||||
import { refreshTokenApi } from './core';
|
import { refreshTokenApi } from './core';
|
||||||
|
|
||||||
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
|
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 TENANT_STORAGE_KEY = 'sys-tenant-id';
|
||||||
const DEV_TENANT_ID = import.meta.env.DEV ? import.meta.env.VITE_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) {
|
function getHeaderValue(headers: any, key: string) {
|
||||||
if (!headers) {
|
if (!headers) {
|
||||||
return '';
|
return '';
|
||||||
@@ -49,16 +55,31 @@ function setHeaderValue(headers: any, key: string, value: string) {
|
|||||||
headers[key] = value;
|
headers[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveTenantId() {
|
function resolveTenantHeaderPayload(): TenantHeaderPayload {
|
||||||
const hostname = window.location.hostname;
|
const hostname = window.location.hostname;
|
||||||
const hostnameParts = hostname.split('.').filter(Boolean);
|
const hostnameParts = hostname.split('.').filter(Boolean);
|
||||||
const isIpAddress = /^\d+\.\d+\.\d+\.\d+$/.test(hostname);
|
const isIpAddress = /^\d+\.\d+\.\d+\.\d+$/.test(hostname);
|
||||||
const subdomainTenantId =
|
const subdomainTenant =
|
||||||
hostnameParts.length > 2 && !isIpAddress ? hostnameParts[0] : '';
|
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) {
|
function createRequestClient(baseURL: string, options?: RequestClientOptions) {
|
||||||
@@ -128,11 +149,14 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) {
|
|||||||
}
|
}
|
||||||
setHeaderValue(config.headers, 'Accept-Language', preferences.app.locale);
|
setHeaderValue(config.headers, 'Accept-Language', preferences.app.locale);
|
||||||
|
|
||||||
const headerTenantId = getHeaderValue(config.headers, TENANT_HEADER_KEY);
|
const headerTenantId = getHeaderValue(config.headers, TENANT_ID_HEADER_KEY);
|
||||||
if (!headerTenantId) {
|
const headerTenantCode = getHeaderValue(config.headers, TENANT_CODE_HEADER_KEY);
|
||||||
const tenantId = resolveTenantId();
|
if (!headerTenantId && !headerTenantCode) {
|
||||||
if (tenantId) {
|
const { tenantCode, tenantId } = resolveTenantHeaderPayload();
|
||||||
setHeaderValue(config.headers, TENANT_HEADER_KEY, String(tenantId));
|
if (tenantCode) {
|
||||||
|
setHeaderValue(config.headers, TENANT_CODE_HEADER_KEY, tenantCode);
|
||||||
|
} else if (tenantId) {
|
||||||
|
setHeaderValue(config.headers, TENANT_ID_HEADER_KEY, tenantId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user