修复前后端显示异常问题(受试者、中心、负责人、日期等)
This commit is contained in:
@@ -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()
|
||||
)}`;
|
||||
};
|
||||
Reference in New Issue
Block a user