feat(tenant): 增加租户基础与域名解析

This commit is contained in:
yml
2026-11-15 15:37:23 +08:00
parent 79b8bc3ac1
commit ca57f604bf
24 changed files with 1329 additions and 1931 deletions
@@ -697,11 +697,6 @@ function resourceText(row: Listing, key: string) {
return quantity > 0 ? formatQuantity(quantity) : '-'
}
function skinGroupText(row: Listing, groupKey: string) {
const skins = getSkinGroup(row, groupKey)
return skins.length ? skins.join('、') : '-'
}
function skinAssetText(row: Listing) {
const groups = [
{ label: '刀', key: 'melee' },
@@ -68,7 +68,7 @@ const displayAnnouncements = computed(() => {
const banners = ref<HomeBannerSlide[]>(defaultHomeBanners)
const displayBanners = computed<HomeBannerSlide[]>(() => {
const list =
siteStore.isDistribution && siteStore.banners.length > 0
siteStore.hasCustomBrand && siteStore.banners.length > 0
? mapDistributionHomeBanners(siteStore.banners, siteStore.siteName)
: banners.value
return list.filter(slide => slide.enabled !== false)
@@ -114,7 +114,7 @@ const displayAnnouncements = computed(() => {
const bannerSlides = ref<HomeBannerSlide[]>(defaultHomeBanners)
const displayBanners = computed<HomeBannerSlide[]>(() => {
const list =
siteStore.isDistribution && siteStore.banners.length > 0
siteStore.hasCustomBrand && siteStore.banners.length > 0
? mapDistributionHomeBanners(siteStore.banners, siteStore.siteName)
: bannerSlides.value
return list.filter(slide => slide.enabled !== false)
@@ -873,15 +873,13 @@ if (!restoreHomeConfigFromCache()) {
<section class="mobile-hero">
<div class="mobile-topbar">
<div class="mobile-brand">
<span class="mobile-logo">{{
siteStore.isDistribution ? siteStore.siteName.slice(0, 1) : '锤'
}}</span>
<span class="mobile-logo">{{ siteStore.brandMark }}</span>
<div>
<div class="mobile-brand-title-row">
<strong>{{ siteStore.siteName }}</strong>
</div>
<small>{{
siteStore.isDistribution ? siteStore.storefrontText.tagline : '哈夫币租号'
siteStore.hasCustomBrand ? siteStore.storefrontText.tagline : '哈夫币租号'
}}</small>
</div>
</div>
+2 -4
View File
@@ -155,15 +155,13 @@ async function handleSupportClick() {
alt="Site Logo"
class="pc-brand-logo-img"
/>
<span v-else class="pc-brand-mark">{{
siteStore.isDistribution ? siteStore.siteName.slice(0, 1) : '锤'
}}</span>
<span v-else class="pc-brand-mark">{{ siteStore.brandMark }}</span>
<div class="pc-brand-info">
<div class="pc-brand-title-row">
<span class="pc-brand-text">{{ siteStore.siteName }}</span>
</div>
<small class="pc-brand-sub">{{
siteStore.isDistribution ? siteStore.storefrontText.tagline : '哈夫币租号'
siteStore.hasCustomBrand ? siteStore.storefrontText.tagline : '哈夫币租号'
}}</small>
</div>
</RouterLink>
+130
View File
@@ -140,6 +140,27 @@ export interface DistributionSiteInfo {
receive_cost_delta?: number
}
export interface TenantSiteContext {
mode: 'tenant'
tenant_id: number
tenant_code: string
tenant_name: string
host: string
domain_type: string
status: string
profile: Record<string, unknown>
features: Record<string, unknown>
}
export function shouldProbeTenantContext(
hostname: string,
_querySite: string,
_envCode: string
): boolean {
if (!hostname || hostname === 'localhost' || hostname === '127.0.0.1') return false
return !hostname.endsWith('.localhost')
}
/**
* 获取当前上下文生效的分销站点编码
* 优先级:
@@ -173,6 +194,10 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
return {
siteCode: code,
siteName: sessionStorage.getItem('hfb_site_name') || '大锤商行',
tenantId: 0,
tenantCode: '',
siteMode: 'legacy' as 'legacy' | 'tenant',
features: {} as Record<string, unknown>,
domain: '',
isDistribution: Boolean(code),
displayAccountEnabled: true,
@@ -192,10 +217,18 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
getters: {
/** 品牌标识字符:分站取站名首字,主站沿用「锤」。 */
brandMark(state): string {
if (state.siteMode === 'tenant') return state.siteName.slice(0, 1) || '锤'
return state.isDistribution && state.siteName ? state.siteName.slice(0, 1) : '锤'
},
isTenant(state): boolean {
return state.siteMode === 'tenant'
},
hasCustomBrand(state): boolean {
return state.siteMode === 'tenant' || state.isDistribution
},
/** 分站首页主标题:站名与主站联名。 */
coBrandName(state): string {
if (state.siteMode === 'tenant') return state.siteName || '大锤商行'
return formatDistributionCoBrandName(state.siteName, state.isDistribution)
},
},
@@ -212,6 +245,23 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
''
).trim()
if (shouldProbeTenantContext(hostname, querySite, envCode)) {
try {
const { data } = await apiClient.get<ApiResponse<TenantSiteContext>>('/site/context', {
silent: true,
skipErrorHandler: true,
})
const info = data.data
if (info?.mode === 'tenant' && info.status === 'active') {
this.applyTenantContext(info)
this.initialized = true
return
}
} catch {
// 当前域名不是经营租户时继续走历史分销站点识别。
}
}
// 1. 显式清除或重置为主站
if (querySite === 'main' || querySite === 'clear' || querySite === '0') {
this.resetToMainSite()
@@ -262,6 +312,10 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
if (info && info.site_code && info.status === 'active') {
this.siteCode = info.site_code
this.siteName = info.site_name || info.site_code
this.tenantId = 0
this.tenantCode = ''
this.siteMode = 'legacy'
this.features = {}
this.domain = info.domain || ''
this.isDistribution = true
this.displayAccountEnabled = info.display_account_enabled ?? true
@@ -342,6 +396,10 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
resetToMainSite() {
this.siteCode = ''
this.siteName = '大锤商行'
this.tenantId = 0
this.tenantCode = ''
this.siteMode = 'legacy'
this.features = {}
this.domain = ''
this.isDistribution = false
this.displayAccountEnabled = true
@@ -360,5 +418,77 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
localStorage.removeItem('hfb_site_name')
document.title = '大锤商行 - 官方租号平台'
},
applyTenantContext(info: TenantSiteContext) {
const profile = info.profile || {}
this.tenantId = Number(info.tenant_id || 0)
this.tenantCode = info.tenant_code || ''
this.siteMode = 'tenant'
this.features = info.features || {}
this.siteCode = ''
this.siteName = text(profile.site_name) || info.tenant_name || info.tenant_code
this.domain = info.host || ''
this.isDistribution = false
this.displayAccountEnabled =
typeof profile.display_account_enabled === 'boolean'
? profile.display_account_enabled
: true
this.defaultSupportGroupId =
typeof profile.default_support_group_id === 'number'
? profile.default_support_group_id
: null
this.logoUrl = text(profile.logo_url)
this.faviconUrl = text(profile.favicon_url)
this.banners = parseJSONArray<DistributionBanner>(profile.banners_json)
this.announcements = normalizeAnnouncements(
parseJSONArray<unknown>(profile.announcements_json)
)
this.footerInfo = parseJSONObject<DistributionFooterInfo>(profile.footer_info_json)
this.storefrontText = mergeStorefrontText(
parseJSONObject<Partial<StorefrontText>>(profile.storefront_text_json)
)
this.receiveRatioDelta = Math.max(0, Number(profile.receive_ratio_delta || 0))
this.receiveCostDelta = Math.max(0, Number(profile.receive_cost_delta || 0))
sessionStorage.removeItem('hfb_site_code')
sessionStorage.removeItem('hfb_site_name')
localStorage.removeItem('hfb_site_code')
localStorage.removeItem('hfb_site_name')
document.title = `${this.siteName} - 官方租号平台`
this.applyFavicon()
},
applyFavicon() {
if (!this.faviconUrl || typeof document === 'undefined') return
let link = document.querySelector("link[rel*='icon']") as HTMLLinkElement | null
if (!link) {
link = document.createElement('link')
link.rel = 'icon'
document.head.appendChild(link)
}
link.href = this.faviconUrl
},
},
})
function parseJSONArray<T>(value: unknown): T[] {
if (Array.isArray(value)) return value as T[]
if (typeof value !== 'string' || !value.trim()) return []
try {
const parsed = JSON.parse(value)
return Array.isArray(parsed) ? parsed : []
} catch {
return []
}
}
function parseJSONObject<T extends object>(value: unknown): T | null {
if (value && typeof value === 'object' && !Array.isArray(value)) return value as T
if (typeof value !== 'string' || !value.trim()) return null
try {
const parsed = JSON.parse(value)
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? (parsed as T) : null
} catch {
return null
}
}
+21
View File
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest'
import { shouldProbeTenantContext } from './distributionSite'
describe('shouldProbeTenantContext', () => {
it('正式域名优先探测经营租户', () => {
expect(shouldProbeTenantContext('zuhaox.com', '', '')).toBe(true)
expect(shouldProbeTenantContext('hfb.example.com', '', '')).toBe(true)
})
it('本地开发不主动探测,避免覆盖旧分销调试模式', () => {
expect(shouldProbeTenantContext('localhost', '', '')).toBe(false)
expect(shouldProbeTenantContext('127.0.0.1', '', '')).toBe(false)
expect(shouldProbeTenantContext('site.localhost', '', '')).toBe(false)
})
it('正式域名先识别经营租户,不能通过旧分站参数覆盖', () => {
expect(shouldProbeTenantContext('zuhaox.com', 'branch_a', '')).toBe(true)
expect(shouldProbeTenantContext('zuhaox.com', '', 'branch_b')).toBe(true)
})
})