Files
dukang/apps/mini-user/src/lib/brand-assets.ts
T
jacy a2abbb6169 feat(v3.4.14): mini-user store UX, brand config, client settings
Store list/detail package flow, configurable WeChat mini brand assets and customer service, shop H5 home refresh.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-06 15:21:59 +08:00

69 lines
2.1 KiB
TypeScript

import {
BRAND_LOGO_MARK_URL,
BRAND_LOGO_URL,
BRAND_LOGO_WIDE_URL,
CUSTOMER_SERVICE_PHONE,
QUALIFICATION_DISCLOSURE_URL,
type ClientRuntimeConfig,
} from '@dukang/shared-types';
import { fetchClientConfig } from './pay-wechat';
export type BrandAssets = {
brandLogoUrl: string;
brandLogoWideUrl: string;
brandLogoMarkUrl: string;
qualificationDisclosureUrl: string;
customerServicePhone: string;
};
const FALLBACK: BrandAssets = {
brandLogoUrl: BRAND_LOGO_URL,
brandLogoWideUrl: BRAND_LOGO_WIDE_URL,
brandLogoMarkUrl: BRAND_LOGO_MARK_URL,
qualificationDisclosureUrl: QUALIFICATION_DISCLOSURE_URL,
customerServicePhone: CUSTOMER_SERVICE_PHONE,
};
let cached: BrandAssets | null = null;
let inflight: Promise<BrandAssets> | null = null;
function fromConfig(config: ClientRuntimeConfig | null | undefined): BrandAssets {
return {
brandLogoUrl: config?.brandLogoUrl?.trim() || FALLBACK.brandLogoUrl,
brandLogoWideUrl: config?.brandLogoWideUrl?.trim() || FALLBACK.brandLogoWideUrl,
brandLogoMarkUrl: config?.brandLogoMarkUrl?.trim() || FALLBACK.brandLogoMarkUrl,
qualificationDisclosureUrl:
config?.qualificationDisclosureUrl?.trim() || FALLBACK.qualificationDisclosureUrl,
customerServicePhone: config?.customerServicePhone?.trim() || FALLBACK.customerServicePhone,
};
}
/** 同步读取最近一次缓存(未拉取前返回代码默认常量) */
export function getBrandAssetsSync(): BrandAssets {
return cached ?? FALLBACK;
}
/** 拉取 /common/client-config 中的品牌与客服配置并缓存 */
export async function loadBrandAssets(force = false): Promise<BrandAssets> {
if (!force && cached) return cached;
if (!force && inflight) return inflight;
inflight = fetchClientConfig()
.then((cfg) => {
cached = fromConfig(cfg);
return cached;
})
.catch(() => {
cached = FALLBACK;
return cached;
})
.finally(() => {
inflight = null;
});
return inflight;
}
export function applyBrandFromClientConfig(config: ClientRuntimeConfig | null | undefined) {
cached = fromConfig(config);
return cached;
}