Refactor code structure for improved readability and maintainability

This commit is contained in:
yuchenglong
2025-11-28 09:50:24 +08:00
parent 43d1033296
commit 93a63277c3
3 changed files with 17 additions and 14 deletions

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

BIN
src/assets/nv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View File

@@ -2,7 +2,8 @@ import React, { useEffect, useState } from "react";
import "./u2.css";
import { useNavigate } from "react-router-dom";
import DecorLine from "../../components/DecorLine";
import avatar from "../../assets/avatar.png";
import nan from "../../assets/nan.png";
import nv from "../../assets/nv.png";
import semicircle from "../../assets/semicircle.png";
import BackButton from "../../components/BackButton";
import ConfirmButton from "../../components/ConfirmButton";
@@ -117,31 +118,33 @@ const U2: React.FC = () => {
}
};
// 根据性别和姓名生成问候称呼:取姓名第一个字 + (先生|女士|先生/女士)
// 根据 gender_name 和姓名生成问候称呼:取姓名第一个字 + (先生|女士)
const getGreeting = () => {
// 在加载中或没有姓名时不返回欢迎语,避免先渲染空的问候
if (loading || !patientInfo?.name) return "";
const name = patientInfo.name.trim();
const firstChar = name ? name.charAt(0) : "";
const genderName = (patientInfo?.gender_name || "").trim();
const sex = (patientInfo as any)?.sex;
let title = "";
if (genderName === "男" || sex === "1" || sex === 1) title = "先生";
else if (genderName === "女" || sex === "2" || sex === 2) title = "女士";
if (genderName === "男") title = "先生";
else if (genderName === "女") title = "女士";
return `尊敬的${
firstChar ? `${firstChar}${title}` : title
},欢迎您的到来:`;
};
// 返回头像地址:优先使用读到的本地照片路径file:///),否则使用默认 avatar
// 返回头像地址:使用 gender_name 字段("女" 显示女性头像),忽略 photo_path
const getAvatarSrc = () => {
const photoPath = (patientInfo as any)?.photo_path || "";
if (!photoPath) return avatar;
// 如果已经是 file:// 协议,直接返回
if (photoPath.startsWith("file://")) return photoPath;
// 将 Windows 路径转换为 file:/// 格式(把反斜杠改为斜杠)
const normalized = photoPath.replace(/\\/g, "/").replace(/\\/g, "/");
return `file:///${normalized}`;
const genderName = (patientInfo?.gender_name || "").trim();
if (genderName === "女") return nv;
return nan;
};
// 根据 gender_name 返回显示的性别标签(用于界面展示)
const getGenderLabel = () => {
const genderName = (patientInfo?.gender_name || "").trim();
if (!genderName) return "";
return genderName;
};
// 在数据加载时显示占位,加载完成后再渲染完整内容
@@ -168,7 +171,7 @@ const U2: React.FC = () => {
<div className="u2-detail-row">
<div className="u2-detail-bar" />
<div className="u2-detail-text">
{loading ? "" : patientInfo?.gender_name || "---"}
{loading ? "" : getGenderLabel()}
</div>
</div>