修复前后端显示异常问题(受试者、中心、负责人、日期等)

This commit is contained in:
Cheng Zhou
2025-12-18 22:09:28 +08:00
parent fb4950f8f7
commit 7137665410
48 changed files with 764 additions and 156 deletions
+45
View File
@@ -0,0 +1,45 @@
/**
* 统一的显示适配层,避免直接暴露 ID / 枚举原值 / 原始时间。
*/
export const displayFallback = "—";
export const displayEnum = (enumMap: Record<string, string>, value?: string | null) => {
if (!value) return displayFallback;
return enumMap[value] || displayFallback;
};
export const displayUser = (
userId?: string | null,
opts?: { users?: Record<string, string>; members?: Record<string, string> }
) => {
if (!userId) return displayFallback;
const name =
opts?.users?.[userId] ||
opts?.members?.[userId] ||
// 兼容成员数据的 user_id 为 key 的情况
(opts?.members && Object.values(opts.members).find((_, k) => k === userId));
return (name as string) || displayFallback;
};
export const displayEntity = (entityMap: Record<string, string>, id?: string | null) => {
if (!id) return displayFallback;
return entityMap[id] || displayFallback;
};
export const displayDate = (value?: string | number | Date | null) => {
if (!value) return displayFallback;
const date = new Date(value);
if (isNaN(date.getTime())) return displayFallback;
return date.toISOString().slice(0, 10);
};
export const displayDateTime = (value?: string | number | Date | null) => {
if (!value) return displayFallback;
const date = new Date(value);
if (isNaN(date.getTime())) return displayFallback;
const pad = (n: number) => String(n).padStart(2, "0");
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(
date.getMinutes()
)}`;
};