refactor: 更新风险监控页面标题为国际化支持,优化风险池控制台的标题处理
This commit is contained in:
102
src/lib/admin-play-types.ts
Normal file
102
src/lib/admin-play-types.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import type { AdminPlayTypeRow } from "@/types/api/admin-config";
|
||||
|
||||
let cachedByCode = new Map<string, AdminPlayTypeRow>();
|
||||
let inflightLoad: Promise<void> | null = null;
|
||||
|
||||
export function getCachedAdminPlayType(playCode: string): AdminPlayTypeRow | undefined {
|
||||
return cachedByCode.get(playCode);
|
||||
}
|
||||
|
||||
export function getCachedAdminPlayTypes(): AdminPlayTypeRow[] {
|
||||
return [...cachedByCode.values()];
|
||||
}
|
||||
|
||||
export function setCachedAdminPlayTypes(items: AdminPlayTypeRow[]): void {
|
||||
cachedByCode = new Map(items.map((row) => [row.play_code, row]));
|
||||
}
|
||||
|
||||
export function clearCachedAdminPlayTypes(): void {
|
||||
cachedByCode = new Map();
|
||||
inflightLoad = null;
|
||||
}
|
||||
|
||||
export function getAdminPlayTypesLoadPromise(
|
||||
loader: () => Promise<{ items: AdminPlayTypeRow[] }>,
|
||||
): Promise<void> {
|
||||
if (cachedByCode.size > 0) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
if (inflightLoad !== null) {
|
||||
return inflightLoad;
|
||||
}
|
||||
|
||||
inflightLoad = loader()
|
||||
.then((data) => {
|
||||
setCachedAdminPlayTypes(data.items);
|
||||
})
|
||||
.catch(() => {
|
||||
// 玩法目录加载失败时回退展示 play_code,不阻断页面。
|
||||
})
|
||||
.finally(() => {
|
||||
inflightLoad = null;
|
||||
});
|
||||
|
||||
return inflightLoad;
|
||||
}
|
||||
|
||||
function pickDisplayName(row: AdminPlayTypeRow, language: string): string | null {
|
||||
const lang = language.split("-")[0]?.toLowerCase() ?? "zh";
|
||||
|
||||
if (lang === "en" && row.display_name_en?.trim()) {
|
||||
return row.display_name_en.trim();
|
||||
}
|
||||
if (lang === "ne" && row.display_name_ne?.trim()) {
|
||||
return row.display_name_ne.trim();
|
||||
}
|
||||
if (row.display_name_zh?.trim()) {
|
||||
return row.display_name_zh.trim();
|
||||
}
|
||||
if (row.display_name_en?.trim()) {
|
||||
return row.display_name_en.trim();
|
||||
}
|
||||
if (row.display_name_ne?.trim()) {
|
||||
return row.display_name_ne.trim();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 按当前语言解析玩法显示名;无配置时回退 play_code */
|
||||
export function resolveAdminPlayTypeDisplayName(
|
||||
playCode: string | null | undefined,
|
||||
language: string,
|
||||
row?: AdminPlayTypeRow,
|
||||
): string {
|
||||
if (playCode == null || playCode === "") {
|
||||
return "—";
|
||||
}
|
||||
|
||||
const resolved = row ?? cachedByCode.get(playCode);
|
||||
if (!resolved) {
|
||||
return playCode;
|
||||
}
|
||||
|
||||
return pickDisplayName(resolved, language) ?? playCode;
|
||||
}
|
||||
|
||||
/** 表格展示:显示名 + 编码(与报表筛选一致) */
|
||||
export function formatAdminPlayCodeLabel(
|
||||
playCode: string | null | undefined,
|
||||
language: string,
|
||||
): string {
|
||||
if (playCode == null || playCode === "") {
|
||||
return "—";
|
||||
}
|
||||
|
||||
const name = resolveAdminPlayTypeDisplayName(playCode, language);
|
||||
if (name === playCode) {
|
||||
return playCode;
|
||||
}
|
||||
|
||||
return `${name} (${playCode})`;
|
||||
}
|
||||
Reference in New Issue
Block a user