Files
soft/backend/src/HealthManager.WebApi/Controllers/HealthController.cs
MingNian f412a474cd 重构健康中心页面:合并指标入口、独立记录页面、多指标趋势图
- 健康中心合并血压/心率/血糖/血氧/体重为统一入口卡片
- 记录页面每个指标独立日期+保存,紧凑设计
- 趋势图支持多指标切换显示,同时显示收缩压和舒张压
- 首页健康概览修复返回页面后数据不更新的问题
- Vite 添加代理,支持手机通过局域网 IP 访问
- 医生端患者详情新增健康趋势图及指标切换
- 运动饮食页面支持删除记录
- 修复复查完成后患者端消失的问题
2026-05-26 15:56:06 +08:00

70 lines
2.6 KiB
C#

using System.Security.Claims;
using System.Text.Json;
using HealthManager.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace HealthManager.WebApi.Controllers;
[ApiController]
[Route("api/health-records")]
[Authorize]
public class HealthController(HealthService healthService) : ControllerBase
{
private Guid UserId => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
private string Role => User.FindFirstValue(ClaimTypes.Role)!;
[HttpGet]
public async Task<IActionResult> GetRecords([FromQuery] string? type, [FromQuery] int days = 30)
{
var targetUserId = UserId;
// Doctors can query any patient
if (Role == "doctor" && Request.Query.ContainsKey("patientId"))
targetUserId = Guid.Parse(Request.Query["patientId"]!);
var records = await healthService.GetRecordsAsync(targetUserId, type, days);
return Ok(records.Select(r => new
{
r.Id, r.Type, Value = r.Value.RootElement.GetRawText(), r.Unit,
r.RecordedAt, r.Source, r.Notes, r.CreatedAt,
}));
}
[HttpGet("stats")]
public async Task<IActionResult> GetStats()
{
var stats = await healthService.GetStatsAsync(UserId);
return Ok(stats);
}
[HttpGet("latest/{type}")]
public async Task<IActionResult> GetLatest(string type)
{
var record = await healthService.GetLatestAsync(UserId, type);
if (record == null) return Ok((object?)null);
return Ok(new { record.Id, record.Type, Value = record.Value.RootElement.GetRawText(), record.Unit, record.RecordedAt, record.Source });
}
[HttpPost]
public async Task<IActionResult> AddRecord([FromBody] HealthRecordCreateRequest request)
{
// Validate JSON
try { JsonDocument.Parse(request.ValueJson); }
catch (JsonException) { return BadRequest(new { message = "无效的数据格式" }); }
var record = await healthService.AddRecordAsync(UserId, request.Type, request.ValueJson, request.Unit, request.RecordedAt, request.Notes);
return Ok(new { record.Id, record.Type, Value = record.Value.RootElement.GetRawText(), record.Unit, record.RecordedAt, record.Source });
}
[HttpDelete("{id:guid}")]
public async Task<IActionResult> DeleteRecord(Guid id)
{
var ok = await healthService.DeleteAsync(id, UserId);
if (!ok) return NotFound(new { message = "记录不存在" });
return Ok(new { message = "删除成功" });
}
}
public record HealthRecordCreateRequest(string Type, string ValueJson, string Unit, DateTime RecordedAt, string? Notes);