forked from wangziqi/gongxue-base
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
export interface TenantLaunchInput {
|
|
query?: Record<string, unknown>;
|
|
referrerExtraData?: Record<string, unknown>;
|
|
}
|
|
|
|
function normalizedTenantCode(value: unknown) {
|
|
if (typeof value !== 'string') return '';
|
|
const normalized = value.trim();
|
|
return /^[A-Za-z0-9._-]{2,64}$/.test(normalized) ? normalized : '';
|
|
}
|
|
|
|
function tenantCodeFromScene(value: unknown) {
|
|
if (typeof value !== 'string' || !value.trim()) return '';
|
|
let decoded = value.trim();
|
|
try {
|
|
decoded = decodeURIComponent(decoded);
|
|
} catch {
|
|
return '';
|
|
}
|
|
if (!decoded.includes('=')) return normalizedTenantCode(decoded);
|
|
for (const segment of decoded.split('&')) {
|
|
const separator = segment.indexOf('=');
|
|
if (separator < 0) continue;
|
|
const key = segment.slice(0, separator).trim();
|
|
if (key !== 'tenantCode' && key !== 'tenant') continue;
|
|
return normalizedTenantCode(segment.slice(separator + 1));
|
|
}
|
|
return '';
|
|
}
|
|
|
|
export function tenantCodeFromLaunch(input: TenantLaunchInput) {
|
|
const query = input.query || {};
|
|
const extraData = input.referrerExtraData || {};
|
|
return normalizedTenantCode(query.tenantCode)
|
|
|| normalizedTenantCode(query.tenant)
|
|
|| tenantCodeFromScene(query.scene)
|
|
|| normalizedTenantCode(extraData.tenantCode)
|
|
|| normalizedTenantCode(extraData.tenant)
|
|
|| '';
|
|
}
|