医生备注优化

This commit is contained in:
2026-04-30 12:19:04 +08:00
parent cba04067bd
commit 901629ac6e
15 changed files with 675 additions and 272 deletions
@@ -25,8 +25,8 @@ class DoctorNoteLogic extends BaseLogic
->find();
$newContent = trim($params['content'] ?? '');
$newImages = self::parseJsonArray($params['tongue_images'] ?? []);
$newReports = self::parseJsonArray($params['report_files'] ?? []);
$newImages = array_map([self::class, 'toRelativePath'], self::parseJsonArray($params['tongue_images'] ?? []));
$newReports = array_map([self::class, 'toRelativePath'], self::parseJsonArray($params['report_files'] ?? []));
if ($existing) {
$data = [];
@@ -108,6 +108,91 @@ class DoctorNoteLogic extends BaseLogic
}
}
/**
* 删除备注中的单张图片
*/
public static function deleteImage(int $noteId, string $imageType, string $imagePath): bool
{
try {
$note = DoctorNote::where('id', $noteId)->whereNull('delete_time')->find();
if (!$note) {
self::setError('记录不存在');
return false;
}
if (!in_array($imageType, ['tongue_images', 'report_files'])) {
self::setError('类型无效');
return false;
}
$images = self::parseJsonArray($note->$imageType);
// 统一转为相对路径再匹配
$targetPath = self::toRelativePath($imagePath);
$images = array_values(array_filter($images, fn($url) => self::toRelativePath($url) !== $targetPath));
$note->$imageType = empty($images) ? null : json_encode($images, JSON_UNESCAPED_UNICODE);
$note->save();
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* 聚合某诊单所有备注中的图片(供 DiagnosisLogic::detail 使用)
*/
public static function getAggregatedImages(int $diagnosisId): array
{
$records = DoctorNote::where('diagnosis_id', $diagnosisId)
->whereNull('delete_time')
->select();
$tongueImages = [];
$reportFiles = [];
foreach ($records as $r) {
$tongueImages = array_merge($tongueImages, self::parseJsonArray($r->tongue_images));
$reportFiles = array_merge($reportFiles, self::parseJsonArray($r->report_files));
}
return [
'tongue_images' => array_map(
fn($u) => empty($u) ? $u : FileService::getFileUrl($u),
array_values(array_unique($tongueImages))
),
'report_files' => array_map(
fn($u) => empty($u) ? $u : FileService::getFileUrl($u),
array_values(array_unique($reportFiles))
),
];
}
/**
* 如果 URL 的域名是当前配置的存储域名则去掉,否则原样保留
*/
private static function toRelativePath(string $url): string
{
if (empty($url)) return $url;
if (stripos($url, 'http://') !== 0 && stripos($url, 'https://') !== 0) {
return $url;
}
// 获取当前存储域名
$domain = self::getStorageDomain();
if ($domain && stripos($url, rtrim($domain, '/')) === 0) {
$relative = substr($url, strlen(rtrim($domain, '/')));
return ltrim($relative, '/');
}
// 非当前存储域名,保留完整 URL
return $url;
}
private static function getStorageDomain(): string
{
$default = \app\common\service\ConfigService::get('storage', 'default', 'local');
if ($default === 'local') {
return request()->domain() . '/';
}
$storage = \app\common\service\ConfigService::get('storage', $default);
return $storage ? ($storage['domain'] ?? '') : '';
}
private static function parseJsonArray($value): array
{
if (is_array($value)) return $value;