feat: 通知推送/免打扰偏好 + 饮食记录侧滑删除修复 + 聊天交互优化

- 通知: 新增推送开关/免打扰时段偏好持久化 + EF 迁移; 通知管线支持免打扰过滤
- 饮食: 侧滑删除 confirmDismiss 按方向放行(修复删不掉); 新增 diet_nutrition_widgets; 饮食录入页重构
- 聊天: 智能体胶囊点击锁防误触; 流式状态 select 优化; 卡片入场动画
- 主页: 消息列表提取 _HomeMessages + 侧滑手势打开抽屉
- 其他: api_client IP 适配; 通知/用药/饮食端点微调; 测试更新
This commit is contained in:
MingNian
2026-07-13 10:39:34 +08:00
parent 335a3e6440
commit e654c1e0cc
19 changed files with 2140 additions and 343 deletions

View File

@@ -89,6 +89,10 @@ public sealed class NotificationPreference
public bool FollowUpReminder { get; set; } = true;
public bool DoctorReply { get; set; } = true;
public bool AbnormalAlert { get; set; } = true;
public bool PushEnabled { get; set; } = true;
public bool DndEnabled { get; set; }
public int DndStartMinutes { get; set; } = 22 * 60;
public int DndEndMinutes { get; set; } = 8 * 60;
// 健康录入提醒——总开关 + 5 子项
public bool HealthRecordReminder { get; set; } = true;
public bool HealthRecordReminderBloodPressure { get; set; } = true;

View File

@@ -0,0 +1,62 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Health.Infrastructure.Data.Migrations
{
/// <inheritdoc />
public partial class AddPushAndDndPreferences : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "DndEnabled",
table: "NotificationPreferences",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<int>(
name: "DndEndMinutes",
table: "NotificationPreferences",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "DndStartMinutes",
table: "NotificationPreferences",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<bool>(
name: "PushEnabled",
table: "NotificationPreferences",
type: "boolean",
nullable: false,
defaultValue: false);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DndEnabled",
table: "NotificationPreferences");
migrationBuilder.DropColumn(
name: "DndEndMinutes",
table: "NotificationPreferences");
migrationBuilder.DropColumn(
name: "DndStartMinutes",
table: "NotificationPreferences");
migrationBuilder.DropColumn(
name: "PushEnabled",
table: "NotificationPreferences");
}
}
}

View File

@@ -673,6 +673,15 @@ namespace Health.Infrastructure.Data.Migrations
b.Property<bool>("AbnormalAlert")
.HasColumnType("boolean");
b.Property<bool>("DndEnabled")
.HasColumnType("boolean");
b.Property<int>("DndEndMinutes")
.HasColumnType("integer");
b.Property<int>("DndStartMinutes")
.HasColumnType("integer");
b.Property<bool>("DoctorReply")
.HasColumnType("boolean");
@@ -700,6 +709,9 @@ namespace Health.Infrastructure.Data.Migrations
b.Property<bool>("MedicationReminder")
.HasColumnType("boolean");
b.Property<bool>("PushEnabled")
.HasColumnType("boolean");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");

View File

@@ -94,7 +94,11 @@ public sealed class EfNotificationOutboxProcessor(AppDbContext db) : INotificati
var message = JsonSerializer.Deserialize<NotificationMessage>(candidate.Payload)
?? throw new InvalidOperationException("通知消息内容为空");
if (!await _db.UserNotifications.AsNoTracking()
var preference = await _db.NotificationPreferences.AsNoTracking()
.FirstOrDefaultAsync(x => x.UserId == candidate.UserId, ct);
var deliver = preference == null || ShouldDeliver(preference, message.Type, DateTime.UtcNow.AddHours(8));
if (deliver && !await _db.UserNotifications.AsNoTracking()
.AnyAsync(x => x.SourceId == candidate.SourceTaskId, ct))
{
await _db.UserNotifications.AddAsync(new UserNotification
@@ -136,4 +140,25 @@ public sealed class EfNotificationOutboxProcessor(AppDbContext db) : INotificati
return true;
}
private static bool ShouldDeliver(NotificationPreference preference, string type, DateTime nowCst)
{
if (!preference.PushEnabled) return false;
var minuteOfDay = nowCst.Hour * 60 + nowCst.Minute;
var inDnd = preference.DndEnabled && (preference.DndStartMinutes <= preference.DndEndMinutes
? minuteOfDay >= preference.DndStartMinutes && minuteOfDay < preference.DndEndMinutes
: minuteOfDay >= preference.DndStartMinutes || minuteOfDay < preference.DndEndMinutes);
if (inDnd) return false;
return type switch
{
"medication_reminder" => preference.MedicationReminder,
"follow_up_reminder" => preference.FollowUpReminder,
"doctor_reply" => preference.DoctorReply,
"abnormal_alert" => preference.AbnormalAlert,
"health_record_reminder" => preference.HealthRecordReminder,
_ => true,
};
}
}

View File

@@ -54,11 +54,16 @@ public sealed class HealthRecordReminderService(
// 取所有启用了健康录入提醒的用户偏好
var prefs = await db.NotificationPreferences
.Where(p => p.HealthRecordReminder)
.Where(p => p.PushEnabled && p.HealthRecordReminder)
.ToListAsync(ct);
foreach (var pref in prefs)
{
var minuteOfDay = nowCst.Hour * 60 + nowCst.Minute;
var inDnd = pref.DndEnabled && (pref.DndStartMinutes <= pref.DndEndMinutes
? minuteOfDay >= pref.DndStartMinutes && minuteOfDay < pref.DndEndMinutes
: minuteOfDay >= pref.DndStartMinutes || minuteOfDay < pref.DndEndMinutes);
if (inDnd) continue;
// 该用户当天已录入的指标
var recordedTypes = await db.HealthRecords
.Where(r => r.UserId == pref.UserId && r.RecordedAt >= todayStart && r.RecordedAt < todayEnd)

View File

@@ -26,8 +26,10 @@ public static class DietEndpoints
group.MapDelete("/{id:guid}", async (Guid id, HttpContext http, IDietService diets, CancellationToken ct) =>
{
var userId = GetUserId(http);
await diets.DeleteAsync(userId, id, ct);
return Results.Ok(new { code = 0, data = new { success = true }, message = (string?)null });
var deleted = await diets.DeleteAsync(userId, id, ct);
return deleted
? Results.Ok(new { code = 0, data = new { success = true }, message = (string?)null })
: Results.Ok(new { code = 40004, data = (object?)null, message = "饮食记录不存在" });
});
group.MapPut("/{id:guid}", async (Guid id, HttpContext http, IDietService diets, CancellationToken ct) =>

View File

@@ -36,8 +36,10 @@ public static class MedicationEndpoints
group.MapDelete("/{id:guid}", async (Guid id, HttpContext http, IMedicationService medications, CancellationToken ct) =>
{
var userId = GetUserId(http);
await medications.DeleteAsync(userId, id, ct);
return Results.Ok(new { code = 0, data = new { success = true }, message = (string?)null });
var deleted = await medications.DeleteAsync(userId, id, ct);
return deleted
? Results.Ok(new { code = 0, data = new { success = true }, message = (string?)null })
: Results.Ok(new { code = 40004, data = (object?)null, message = "用药记录不存在" });
});
group.MapPost("/{id:guid}/confirm", async (Guid id, HttpContext http, IMedicationService medications, CancellationToken ct) =>

View File

@@ -90,6 +90,10 @@ public static class NotificationEndpoints
if (body.FollowUpReminder.HasValue) pref.FollowUpReminder = body.FollowUpReminder.Value;
if (body.DoctorReply.HasValue) pref.DoctorReply = body.DoctorReply.Value;
if (body.AbnormalAlert.HasValue) pref.AbnormalAlert = body.AbnormalAlert.Value;
if (body.PushEnabled.HasValue) pref.PushEnabled = body.PushEnabled.Value;
if (body.DndEnabled.HasValue) pref.DndEnabled = body.DndEnabled.Value;
if (body.DndStartMinutes is >= 0 and < 1440) pref.DndStartMinutes = body.DndStartMinutes.Value;
if (body.DndEndMinutes is >= 0 and < 1440) pref.DndEndMinutes = body.DndEndMinutes.Value;
if (body.HealthRecordReminder.HasValue) pref.HealthRecordReminder = body.HealthRecordReminder.Value;
if (body.HealthRecordReminderBloodPressure.HasValue) pref.HealthRecordReminderBloodPressure = body.HealthRecordReminderBloodPressure.Value;
if (body.HealthRecordReminderHeartRate.HasValue) pref.HealthRecordReminderHeartRate = body.HealthRecordReminderHeartRate.Value;
@@ -153,6 +157,10 @@ public static class NotificationEndpoints
followUpReminder = pref.FollowUpReminder,
doctorReply = pref.DoctorReply,
abnormalAlert = pref.AbnormalAlert,
pushEnabled = pref.PushEnabled,
dndEnabled = pref.DndEnabled,
dndStartMinutes = pref.DndStartMinutes,
dndEndMinutes = pref.DndEndMinutes,
healthRecordReminder = pref.HealthRecordReminder,
healthRecordReminderBloodPressure = pref.HealthRecordReminderBloodPressure,
healthRecordReminderHeartRate = pref.HealthRecordReminderHeartRate,
@@ -167,6 +175,10 @@ public sealed record NotificationPrefsUpdateDto(
bool? FollowUpReminder,
bool? DoctorReply,
bool? AbnormalAlert,
bool? PushEnabled,
bool? DndEnabled,
int? DndStartMinutes,
int? DndEndMinutes,
bool? HealthRecordReminder,
bool? HealthRecordReminderBloodPressure,
bool? HealthRecordReminderHeartRate,