Initial commit: 健康管家 AI 健康陪伴助手
- Backend: .NET 10 Minimal API + EF Core + PostgreSQL - Frontend: Flutter + Riverpod + GoRouter + Dio - AI: DeepSeek LLM + Qwen VLM (OpenAI-compatible) - Auth: SMS + JWT (access/refresh tokens) - Features: AI chat, health tracking, medication management, diet analysis, exercise plans, doctor consultations, report analysis
This commit is contained in:
152
backend/src/Health.Infrastructure/AI/AiClients.cs
Normal file
152
backend/src/Health.Infrastructure/AI/AiClients.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Health.Infrastructure.AI;
|
||||
|
||||
/// <summary>
|
||||
/// DeepSeek LLM 客户端(对话 + Tool Calling)
|
||||
/// </summary>
|
||||
public sealed class DeepSeekClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly string _model;
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
|
||||
public DeepSeekClient(HttpClient http, IConfiguration config)
|
||||
{
|
||||
_http = http;
|
||||
_model = config["DEEPSEEK_MODEL"] ?? "deepseek-chat";
|
||||
_jsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 流式 Chat Completions
|
||||
/// </summary>
|
||||
public async IAsyncEnumerable<string> ChatStreamAsync(
|
||||
List<ChatMessage> messages,
|
||||
List<ToolDefinition>? tools = null,
|
||||
int maxTokens = 2048,
|
||||
float temperature = 0.7f,
|
||||
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken ct = default)
|
||||
{
|
||||
var request = new ChatCompletionRequest
|
||||
{
|
||||
Model = _model, Messages = messages, Stream = true,
|
||||
MaxTokens = maxTokens, Temperature = temperature, Tools = tools,
|
||||
};
|
||||
if (tools?.Count > 0) request.ToolChoice = "auto";
|
||||
|
||||
var json = JsonSerializer.Serialize(request, _jsonOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var httpRequest = new HttpRequestMessage(HttpMethod.Post, "chat/completions") { Content = content };
|
||||
httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
|
||||
|
||||
using var response = await _http.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, ct);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
using var stream = await response.Content.ReadAsStreamAsync(ct);
|
||||
using var reader = new StreamReader(stream);
|
||||
|
||||
string? line;
|
||||
while ((line = await reader.ReadLineAsync(ct)) != null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line)) continue;
|
||||
if (!line.StartsWith("data: ")) continue;
|
||||
var data = line["data: ".Length..];
|
||||
if (data == "[DONE]") break;
|
||||
yield return data;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 非流式 Chat Completions(用于 Tool Calling)
|
||||
/// </summary>
|
||||
public async Task<ChatCompletionResponse> ChatAsync(
|
||||
List<ChatMessage> messages,
|
||||
List<ToolDefinition>? tools = null,
|
||||
int maxTokens = 2048,
|
||||
float temperature = 0.7f,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var request = new ChatCompletionRequest
|
||||
{
|
||||
Model = _model, Messages = messages, Stream = false,
|
||||
MaxTokens = maxTokens, Temperature = temperature, Tools = tools,
|
||||
};
|
||||
if (tools?.Count > 0) request.ToolChoice = "auto";
|
||||
|
||||
var json = JsonSerializer.Serialize(request, _jsonOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await _http.PostAsync("chat/completions", content, ct);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync(ct);
|
||||
return JsonSerializer.Deserialize<ChatCompletionResponse>(body, _jsonOptions)!;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 千问 VL 视觉客户端(食物识别 + 报告解读)
|
||||
/// </summary>
|
||||
public sealed class QwenVisionClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly string _model;
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
|
||||
public QwenVisionClient(HttpClient http, IConfiguration config)
|
||||
{
|
||||
_http = http;
|
||||
_model = config["QWEN_VISION_MODEL"] ?? "qwen-vl-max";
|
||||
_jsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ChatCompletionResponse> VisionAsync(
|
||||
string systemPrompt,
|
||||
List<string> imageUrls,
|
||||
string? userText = null,
|
||||
int maxTokens = 2048,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var messages = new List<ChatMessage>();
|
||||
if (!string.IsNullOrEmpty(systemPrompt))
|
||||
messages.Add(new ChatMessage { Role = "system", Content = systemPrompt });
|
||||
|
||||
var contentParts = new List<object>();
|
||||
foreach (var url in imageUrls)
|
||||
contentParts.Add(new { type = "image_url", image_url = new { url } });
|
||||
if (!string.IsNullOrEmpty(userText))
|
||||
contentParts.Add(new { type = "text", text = userText });
|
||||
|
||||
var userMessage = new ChatMessage
|
||||
{
|
||||
Role = "user",
|
||||
Content = JsonSerializer.Serialize(contentParts, _jsonOptions)
|
||||
};
|
||||
messages.Add(userMessage);
|
||||
|
||||
var request = new ChatCompletionRequest
|
||||
{
|
||||
Model = _model, Messages = messages, MaxTokens = maxTokens, Stream = false,
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(request, _jsonOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
var response = await _http.PostAsync("chat/completions", content, ct);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var body = await response.Content.ReadAsStringAsync(ct);
|
||||
return JsonSerializer.Deserialize<ChatCompletionResponse>(body, _jsonOptions)!;
|
||||
}
|
||||
}
|
||||
235
backend/src/Health.Infrastructure/AI/OpenAiCompatibleClient.cs
Normal file
235
backend/src/Health.Infrastructure/AI/OpenAiCompatibleClient.cs
Normal file
@@ -0,0 +1,235 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Health.Infrastructure.AI;
|
||||
|
||||
/// <summary>
|
||||
/// OpenAI 兼容协议 HTTP 客户端,统一调用 DeepSeek / 千问 VL
|
||||
/// </summary>
|
||||
public sealed class OpenAiCompatibleClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly string _model;
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
|
||||
public OpenAiCompatibleClient(string baseUrl, string apiKey, string model)
|
||||
{
|
||||
_http = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(baseUrl.TrimEnd('/') + "/"),
|
||||
Timeout = TimeSpan.FromSeconds(60)
|
||||
};
|
||||
_http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
|
||||
_http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
_model = model;
|
||||
_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 流式 Chat Completions(SSE)
|
||||
/// </summary>
|
||||
public async IAsyncEnumerable<string> ChatStreamAsync(
|
||||
List<ChatMessage> messages,
|
||||
List<ToolDefinition>? tools = null,
|
||||
int maxTokens = 2048,
|
||||
float temperature = 0.7f)
|
||||
{
|
||||
var request = new ChatCompletionRequest
|
||||
{
|
||||
Model = _model,
|
||||
Messages = messages,
|
||||
Stream = true,
|
||||
MaxTokens = maxTokens,
|
||||
Temperature = temperature,
|
||||
Tools = tools,
|
||||
};
|
||||
|
||||
if (tools?.Count > 0)
|
||||
request.ToolChoice = "auto";
|
||||
|
||||
var json = JsonSerializer.Serialize(request, _jsonOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var httpRequest = new HttpRequestMessage(HttpMethod.Post, "chat/completions")
|
||||
{
|
||||
Content = content
|
||||
};
|
||||
httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
|
||||
|
||||
var response = await _http.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
using var stream = await response.Content.ReadAsStreamAsync();
|
||||
using var reader = new StreamReader(stream);
|
||||
|
||||
string? line;
|
||||
while ((line = await reader.ReadLineAsync()) != null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line)) continue;
|
||||
if (!line.StartsWith("data: ")) continue;
|
||||
|
||||
var data = line["data: ".Length..];
|
||||
if (data == "[DONE]") break;
|
||||
|
||||
yield return data;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 非流式 Chat Completions
|
||||
/// </summary>
|
||||
public async Task<ChatCompletionResponse> ChatAsync(
|
||||
List<ChatMessage> messages,
|
||||
List<ToolDefinition>? tools = null,
|
||||
int maxTokens = 2048,
|
||||
float temperature = 0.7f)
|
||||
{
|
||||
var request = new ChatCompletionRequest
|
||||
{
|
||||
Model = _model,
|
||||
Messages = messages,
|
||||
Stream = false,
|
||||
MaxTokens = maxTokens,
|
||||
Temperature = temperature,
|
||||
Tools = tools,
|
||||
};
|
||||
|
||||
if (tools?.Count > 0)
|
||||
request.ToolChoice = "auto";
|
||||
|
||||
var json = JsonSerializer.Serialize(request, _jsonOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await _http.PostAsync("chat/completions", content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<ChatCompletionResponse>(body, _jsonOptions)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Vision 图片理解(非流式)
|
||||
/// </summary>
|
||||
public async Task<ChatCompletionResponse> VisionAsync(
|
||||
string systemPrompt,
|
||||
List<string> imageUrls,
|
||||
string? userText = null,
|
||||
int maxTokens = 2048)
|
||||
{
|
||||
var messages = new List<ChatMessage>();
|
||||
|
||||
if (!string.IsNullOrEmpty(systemPrompt))
|
||||
messages.Add(new ChatMessage { Role = "system", Content = systemPrompt });
|
||||
|
||||
// 构建多模态消息内容
|
||||
var contentParts = new List<object>();
|
||||
foreach (var url in imageUrls)
|
||||
contentParts.Add(new { type = "image_url", image_url = new { url } });
|
||||
if (!string.IsNullOrEmpty(userText))
|
||||
contentParts.Add(new { type = "text", text = userText });
|
||||
|
||||
var userMessage = new ChatMessage
|
||||
{
|
||||
Role = "user",
|
||||
Content = JsonSerializer.Serialize(contentParts, _jsonOptions)
|
||||
};
|
||||
|
||||
messages.Add(userMessage);
|
||||
|
||||
var request = new ChatCompletionRequest
|
||||
{
|
||||
Model = _model,
|
||||
Messages = messages,
|
||||
MaxTokens = maxTokens,
|
||||
Stream = false,
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(request, _jsonOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
var response = await _http.PostAsync("chat/completions", content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<ChatCompletionResponse>(body, _jsonOptions)!;
|
||||
}
|
||||
}
|
||||
|
||||
#region 请求/响应模型
|
||||
|
||||
public sealed class ChatCompletionRequest
|
||||
{
|
||||
public string Model { get; set; } = string.Empty;
|
||||
public List<ChatMessage> Messages { get; set; } = [];
|
||||
public bool Stream { get; set; }
|
||||
public int MaxTokens { get; set; } = 2048;
|
||||
public float Temperature { get; set; } = 0.7f;
|
||||
public List<ToolDefinition>? Tools { get; set; }
|
||||
public string? ToolChoice { get; set; }
|
||||
}
|
||||
|
||||
public sealed class ChatMessage
|
||||
{
|
||||
public string Role { get; set; } = string.Empty;
|
||||
public string Content { get; set; } = string.Empty;
|
||||
public string? ToolCallId { get; set; }
|
||||
public List<ToolCall>? ToolCalls { get; set; }
|
||||
}
|
||||
|
||||
public sealed class ToolDefinition
|
||||
{
|
||||
public string Type { get; set; } = "function";
|
||||
public ToolFunction Function { get; set; } = new();
|
||||
}
|
||||
|
||||
public sealed class ToolFunction
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public object Parameters { get; set; } = new();
|
||||
}
|
||||
|
||||
public sealed class ToolCall
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string Type { get; set; } = "function";
|
||||
public ToolCallFunction Function { get; set; } = new();
|
||||
}
|
||||
|
||||
public sealed class ToolCallFunction
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Arguments { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class ChatCompletionResponse
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public List<Choice> Choices { get; set; } = [];
|
||||
}
|
||||
|
||||
public sealed class Choice
|
||||
{
|
||||
public int Index { get; set; }
|
||||
public ResponseMessage? Message { get; set; }
|
||||
public ResponseDelta? Delta { get; set; }
|
||||
public string? FinishReason { get; set; }
|
||||
}
|
||||
|
||||
public sealed class ResponseMessage
|
||||
{
|
||||
public string Role { get; set; } = string.Empty;
|
||||
public string? Content { get; set; }
|
||||
public List<ToolCall>? ToolCalls { get; set; }
|
||||
}
|
||||
|
||||
public sealed class ResponseDelta
|
||||
{
|
||||
public string? Content { get; set; }
|
||||
public string? Role { get; set; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
116
backend/src/Health.Infrastructure/AI/PromptManager.cs
Normal file
116
backend/src/Health.Infrastructure/AI/PromptManager.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using Health.Domain.Enums;
|
||||
|
||||
namespace Health.Infrastructure.AI;
|
||||
|
||||
/// <summary>
|
||||
/// System Prompt 模板管理
|
||||
/// </summary>
|
||||
public sealed class PromptManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取指定 Agent 的 System Prompt
|
||||
/// </summary>
|
||||
public string GetSystemPrompt(AgentType agentType) => agentType switch
|
||||
{
|
||||
AgentType.Default => DefaultPrompt,
|
||||
AgentType.Consultation => ConsultationPrompt,
|
||||
AgentType.Health => HealthDataPrompt,
|
||||
AgentType.Diet => DietPrompt,
|
||||
AgentType.Medication => MedicationPrompt,
|
||||
AgentType.Report => ReportPrompt,
|
||||
AgentType.Exercise => ExercisePrompt,
|
||||
_ => DefaultPrompt
|
||||
};
|
||||
|
||||
private const string DefaultPrompt = """
|
||||
你是一个心脏术后康复患者的私人 AI 健康管家,名叫"阿福"。
|
||||
语气温暖、专业、像朋友一样关怀患者。
|
||||
|
||||
职责:
|
||||
1. 理解用户的健康需求,解析健康数据
|
||||
2. 主动查看患者近期数据,发现异常时提醒
|
||||
3. 回答健康知识问题
|
||||
4. 每次回复末尾,如有需要提醒的事项,简短温馨地提醒一句
|
||||
|
||||
规则:
|
||||
- 不要提供超出你能力范围的医疗建议
|
||||
- 遇到紧急症状(剧烈胸痛、呼吸困难)立即建议就医
|
||||
- 饮食/运动建议要结合患者档案中的疾病和限制
|
||||
""";
|
||||
|
||||
private const string ConsultationPrompt = """
|
||||
你是一个心血管内科医生助手,负责对心脏术后患者进行多轮问诊。
|
||||
|
||||
规则:
|
||||
1. 每次只问一个问题,不要一次问多个
|
||||
2. 给出 2-3 个快捷选项让患者点击
|
||||
3. 问诊步骤:先问感受 → 持续时间 → 伴随症状 → 近期用药 → 给出初步分析
|
||||
4. 遇到以下情况建议立即就医:剧烈胸痛、呼吸困难、心悸
|
||||
5. 遇到以下情况建议转医生:血压持续>160/100、心率>120或<50
|
||||
6. 所有分析末尾标注"以上为AI分析,具体请咨询医生"
|
||||
7. 问诊结束给出结构化小结
|
||||
""";
|
||||
|
||||
private const string HealthDataPrompt = """
|
||||
你是一个健康数据录入助手。
|
||||
|
||||
规则:
|
||||
1. 解析用户消息中的指标和数值(血压/心率/血糖/血氧/体重)
|
||||
2. 指标明确+数值明确→直接录入
|
||||
3. 数值明确但指标模糊(如只说"120")→追问是"收缩压还是血糖?"
|
||||
4. 时间模糊→取当前时间,在确认卡片中告知用户
|
||||
5. 数值超出正常范围→附带异常提醒
|
||||
6. 录入后生成确认卡片格式的回复
|
||||
|
||||
正常值参考范围:
|
||||
- 收缩压 90-139 mmHg,舒张压 60-89 mmHg
|
||||
- 心率 60-100 次/分
|
||||
- 空腹血糖 3.9-6.1 mmol/L
|
||||
- 血氧 95-100%
|
||||
""";
|
||||
|
||||
private const string DietPrompt = """
|
||||
你是一个营养分析专家,专门为心脏术后患者提供饮食指导。
|
||||
|
||||
规则:
|
||||
1. 收到VLM食物识别结果后,结合患者档案进行综合分析
|
||||
2. 总热量汇总
|
||||
3. 逐项判断"能不能吃"(基于疾病诊断/过敏/饮食限制/近期指标)
|
||||
4. 给出 1-5 星健康评分
|
||||
5. 单项警告 + 整体饮食建议
|
||||
6. 追问餐次归属(早餐/午餐/晚餐/加餐)
|
||||
""";
|
||||
|
||||
private const string MedicationPrompt = """
|
||||
你是一个用药管理专家。
|
||||
|
||||
规则:
|
||||
1. 解析用户口中的药品信息(药名/剂量/频次/时间)
|
||||
2. "早饭后"等模糊时间→追问具体几点
|
||||
3. 解析完成后展示确认卡片
|
||||
4. 处方拍照→提取药品信息→生成用药计划→让用户确认
|
||||
5. 回答用药相关疑问
|
||||
""";
|
||||
|
||||
private const string ReportPrompt = """
|
||||
你是一个医学报告解读专家。
|
||||
|
||||
规则:
|
||||
1. 收到报告图片后,提取所有指标及其数值
|
||||
2. 标注异常指标(偏高/偏低/正常)
|
||||
3. 给出初步分析
|
||||
4. 所有内容标注"AI预解读,待医生确认"
|
||||
5. 图像类报告(彩超/CT)注明"需医生人工审阅"
|
||||
""";
|
||||
|
||||
private const string ExercisePrompt = """
|
||||
你是一个运动康复教练,专门为心脏术后患者制定运动计划。
|
||||
|
||||
规则:
|
||||
1. 帮助用户设定每周运动计划(类型/时长/天数)
|
||||
2. 以周为单位,每天指定运动类型和时长
|
||||
3. 推荐适合心脏康复的运动:散步、慢跑、太极、游泳等
|
||||
4. 运动强度要循序渐进
|
||||
5. 避免剧烈运动
|
||||
""";
|
||||
}
|
||||
136
backend/src/Health.Infrastructure/Data/AppDbContext.cs
Normal file
136
backend/src/Health.Infrastructure/Data/AppDbContext.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using Health.Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace Health.Infrastructure.Data;
|
||||
|
||||
/// <summary>
|
||||
/// 应用程序数据库上下文
|
||||
/// </summary>
|
||||
public sealed class AppDbContext : DbContext
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
|
||||
|
||||
// 核心业务表
|
||||
public DbSet<User> Users => Set<User>();
|
||||
public DbSet<HealthRecord> HealthRecords => Set<HealthRecord>();
|
||||
public DbSet<Medication> Medications => Set<Medication>();
|
||||
public DbSet<MedicationLog> MedicationLogs => Set<MedicationLog>();
|
||||
public DbSet<DietRecord> DietRecords => Set<DietRecord>();
|
||||
public DbSet<DietFoodItem> DietFoodItems => Set<DietFoodItem>();
|
||||
public DbSet<ExercisePlan> ExercisePlans => Set<ExercisePlan>();
|
||||
public DbSet<ExercisePlanItem> ExercisePlanItems => Set<ExercisePlanItem>();
|
||||
public DbSet<Report> Reports => Set<Report>();
|
||||
public DbSet<Conversation> Conversations => Set<Conversation>();
|
||||
public DbSet<ConversationMessage> ConversationMessages => Set<ConversationMessage>();
|
||||
public DbSet<Consultation> Consultations => Set<Consultation>();
|
||||
public DbSet<ConsultationMessage> ConsultationMessages => Set<ConsultationMessage>();
|
||||
public DbSet<Doctor> Doctors => Set<Doctor>();
|
||||
public DbSet<FollowUp> FollowUps => Set<FollowUp>();
|
||||
public DbSet<HealthArchive> HealthArchives => Set<HealthArchive>();
|
||||
|
||||
// 支撑表
|
||||
public DbSet<RefreshToken> RefreshTokens => Set<RefreshToken>();
|
||||
public DbSet<VerificationCode> VerificationCodes => Set<VerificationCode>();
|
||||
public DbSet<NotificationPreference> NotificationPreferences => Set<NotificationPreference>();
|
||||
public DbSet<DeviceToken> DeviceTokens => Set<DeviceToken>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
base.OnModelCreating(builder);
|
||||
|
||||
// ---- User ----
|
||||
builder.Entity<User>(e =>
|
||||
{
|
||||
e.HasIndex(u => u.Phone).IsUnique();
|
||||
});
|
||||
|
||||
// ---- HealthRecord ----
|
||||
builder.Entity<HealthRecord>(e =>
|
||||
{
|
||||
e.HasIndex(r => new { r.UserId, r.RecordedAt }).IsDescending(false, true);
|
||||
e.HasIndex(r => new { r.UserId, r.MetricType });
|
||||
e.Property(r => r.MetricType).HasConversion<string>();
|
||||
e.Property(r => r.Source).HasConversion<string>();
|
||||
});
|
||||
|
||||
// ---- Medication ----
|
||||
builder.Entity<Medication>(e =>
|
||||
{
|
||||
e.HasIndex(m => new { m.UserId, m.IsActive });
|
||||
e.Property(m => m.Frequency).HasConversion<string>();
|
||||
e.Property(m => m.Source).HasConversion<string>();
|
||||
e.Property(m => m.TimeOfDay).HasColumnType("time[]");
|
||||
});
|
||||
|
||||
builder.Entity<MedicationLog>(e =>
|
||||
{
|
||||
e.HasIndex(l => new { l.MedicationId, l.CreatedAt }).IsDescending(false, true);
|
||||
e.Property(l => l.Status).HasConversion<string>();
|
||||
});
|
||||
|
||||
// ---- Diet ----
|
||||
builder.Entity<DietRecord>(e =>
|
||||
{
|
||||
e.HasIndex(d => new { d.UserId, d.RecordedAt }).IsDescending(false, true);
|
||||
e.Property(d => d.MealType).HasConversion<string>();
|
||||
});
|
||||
|
||||
// ---- ExercisePlan ----
|
||||
builder.Entity<ExercisePlan>(e =>
|
||||
{
|
||||
e.HasIndex(p => new { p.UserId, p.WeekStartDate }).IsDescending(false, true);
|
||||
});
|
||||
|
||||
// ---- Report ----
|
||||
builder.Entity<Report>(e =>
|
||||
{
|
||||
e.Property(r => r.FileType).HasConversion<string>();
|
||||
e.Property(r => r.Category).HasConversion<string>();
|
||||
e.Property(r => r.Status).HasConversion<string>();
|
||||
});
|
||||
|
||||
// ---- Conversation ----
|
||||
builder.Entity<Conversation>(e =>
|
||||
{
|
||||
e.HasIndex(c => new { c.UserId, c.UpdatedAt }).IsDescending(false, true);
|
||||
e.Property(c => c.AgentType).HasConversion<string>();
|
||||
});
|
||||
|
||||
builder.Entity<ConversationMessage>(e =>
|
||||
{
|
||||
e.HasIndex(m => new { m.ConversationId, m.CreatedAt });
|
||||
e.Property(m => m.Role).HasConversion<string>();
|
||||
});
|
||||
|
||||
// ---- Consultation ----
|
||||
builder.Entity<Consultation>(e =>
|
||||
{
|
||||
e.Property(c => c.Status).HasConversion<string>();
|
||||
});
|
||||
|
||||
builder.Entity<ConsultationMessage>(e =>
|
||||
{
|
||||
e.HasIndex(m => new { m.ConsultationId, m.CreatedAt }).IsDescending(false, true);
|
||||
e.Property(m => m.SenderType).HasConversion<string>();
|
||||
});
|
||||
|
||||
// ---- VerificationCode ----
|
||||
builder.Entity<VerificationCode>(e =>
|
||||
{
|
||||
e.HasIndex(v => new { v.Phone, v.CreatedAt }).IsDescending(false, true);
|
||||
});
|
||||
|
||||
// ---- NotificationPreference ----
|
||||
builder.Entity<NotificationPreference>(e =>
|
||||
{
|
||||
e.HasIndex(n => n.UserId).IsUnique();
|
||||
});
|
||||
|
||||
// ---- HealthArchive ----
|
||||
builder.Entity<HealthArchive>(e =>
|
||||
{
|
||||
e.HasIndex(a => a.UserId).IsUnique();
|
||||
});
|
||||
}
|
||||
}
|
||||
47
backend/src/Health.Infrastructure/Data/DataSeeder.cs
Normal file
47
backend/src/Health.Infrastructure/Data/DataSeeder.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Health.Domain.Entities;
|
||||
|
||||
namespace Health.Infrastructure.Data;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库种子数据
|
||||
/// </summary>
|
||||
public static class DataSeeder
|
||||
{
|
||||
public static async Task SeedAsync(AppDbContext db)
|
||||
{
|
||||
// 种子医生数据
|
||||
if (!db.Doctors.Any())
|
||||
{
|
||||
db.Doctors.AddRange(
|
||||
new Doctor
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "王建国",
|
||||
Title = "主任医师",
|
||||
Department = "心血管内科",
|
||||
Introduction = "擅长冠心病术后管理、心脏康复指导",
|
||||
IsActive = true
|
||||
},
|
||||
new Doctor
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "李芳",
|
||||
Title = "副主任医师",
|
||||
Department = "营养科",
|
||||
Introduction = "擅长术后营养指导、膳食规划",
|
||||
IsActive = true
|
||||
},
|
||||
new Doctor
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "张明",
|
||||
Title = "主任医师",
|
||||
Department = "心脏康复科",
|
||||
Introduction = "擅长心脏术后运动康复、心肺功能评估",
|
||||
IsActive = true
|
||||
}
|
||||
);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
144
backend/src/Health.Infrastructure/Data/DevDataSeeder.cs
Normal file
144
backend/src/Health.Infrastructure/Data/DevDataSeeder.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using Health.Domain.Entities;
|
||||
using Health.Domain.Enums;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Health.Infrastructure.Data;
|
||||
|
||||
/// <summary>
|
||||
/// 开发环境测试数据填充。生产环境不要调用!
|
||||
/// 开关:DEVDATA_ENABLED=true 才会执行
|
||||
/// </summary>
|
||||
public static class DevDataSeeder
|
||||
{
|
||||
public static async Task SeedIfEnabled(AppDbContext db, IConfiguration config)
|
||||
{
|
||||
// 通过环境变量控制:DEVDATA_ENABLED=true 才填充测试数据
|
||||
var enabled = config["DEVDATA_ENABLED"]?.ToLowerInvariant();
|
||||
if (enabled != "true") return;
|
||||
|
||||
// 检查是否已有测试用户(避免重复填充)
|
||||
if (db.Users.Any(u => u.Phone == "13800000001")) return;
|
||||
|
||||
// ---- 创建测试患者 ----
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(), Phone = "13800000001", Name = "张三",
|
||||
Gender = "男", BirthDate = new DateOnly(1970, 3, 15),
|
||||
CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow,
|
||||
};
|
||||
db.Users.Add(user);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
// ---- 健康档案 ----
|
||||
db.HealthArchives.Add(new HealthArchive
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id,
|
||||
Diagnosis = "冠心病", SurgeryType = "PCI支架植入术",
|
||||
SurgeryDate = new DateOnly(2026, 3, 15),
|
||||
Allergies = ["青霉素"], DietRestrictions = ["低盐", "低脂"],
|
||||
ChronicDiseases = ["高血压", "高血脂"],
|
||||
FamilyHistory = "父亲冠心病",
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
});
|
||||
|
||||
// ---- 健康数据(过去 7 天)----
|
||||
var random = new Random(42);
|
||||
for (int i = 7; i >= 0; i--)
|
||||
{
|
||||
var date = DateTime.UtcNow.AddDays(-i);
|
||||
// 血压
|
||||
db.HealthRecords.Add(new HealthRecord
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.BloodPressure,
|
||||
Systolic = 120 + random.Next(-5, 15), Diastolic = 75 + random.Next(-5, 10),
|
||||
Unit = "mmHg", Source = HealthRecordSource.AiEntry,
|
||||
IsAbnormal = false, RecordedAt = date,
|
||||
});
|
||||
// 心率
|
||||
db.HealthRecords.Add(new HealthRecord
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.HeartRate,
|
||||
Value = 68 + random.Next(-5, 10), Unit = "次/分",
|
||||
Source = HealthRecordSource.AiEntry,
|
||||
IsAbnormal = false, RecordedAt = date,
|
||||
});
|
||||
// 血糖
|
||||
db.HealthRecords.Add(new HealthRecord
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.Glucose,
|
||||
Value = 5.0m + (decimal)(random.NextDouble() * 1.5),
|
||||
Unit = "mmol/L", Source = HealthRecordSource.AiEntry,
|
||||
IsAbnormal = false, RecordedAt = date,
|
||||
});
|
||||
// 血氧
|
||||
db.HealthRecords.Add(new HealthRecord
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.SpO2,
|
||||
Value = 96 + random.Next(0, 3), Unit = "%",
|
||||
Source = HealthRecordSource.AiEntry,
|
||||
IsAbnormal = false, RecordedAt = date,
|
||||
});
|
||||
}
|
||||
// 一条异常血压
|
||||
db.HealthRecords.Add(new HealthRecord
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.BloodPressure,
|
||||
Systolic = 148, Diastolic = 92, Unit = "mmHg",
|
||||
Source = HealthRecordSource.AiEntry, IsAbnormal = true,
|
||||
RecordedAt = DateTime.UtcNow.AddDays(-1),
|
||||
});
|
||||
|
||||
// ---- 用药计划 ----
|
||||
db.Medications.Add(new Medication
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, Name = "阿司匹林", Dosage = "100mg",
|
||||
Frequency = MedicationFrequency.Daily, TimeOfDay = [new TimeOnly(8, 0)],
|
||||
Source = MedicationSource.Prescription, IsActive = true, StartDate = new DateOnly(2026, 4, 1),
|
||||
});
|
||||
db.Medications.Add(new Medication
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, Name = "阿托伐他汀", Dosage = "20mg",
|
||||
Frequency = MedicationFrequency.Daily, TimeOfDay = [new TimeOnly(20, 0)],
|
||||
Source = MedicationSource.Prescription, IsActive = true, StartDate = new DateOnly(2026, 4, 1),
|
||||
});
|
||||
|
||||
// ---- 运动计划 ----
|
||||
var monday = DateOnly.FromDateTime(DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek + 1));
|
||||
var plan = new ExercisePlan { Id = Guid.NewGuid(), UserId = user.Id, WeekStartDate = monday };
|
||||
plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 0, ExerciseType = "散步", DurationMinutes = 30, IsCompleted = true, CompletedAt = DateTime.UtcNow.AddDays(-1) });
|
||||
plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 1, ExerciseType = "慢跑", DurationMinutes = 20 });
|
||||
plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 2, ExerciseType = "散步", DurationMinutes = 30 });
|
||||
plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 3, IsRestDay = true });
|
||||
plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 4, ExerciseType = "太极", DurationMinutes = 40 });
|
||||
plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 5, IsRestDay = true });
|
||||
plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 6, ExerciseType = "散步", DurationMinutes = 30 });
|
||||
db.ExercisePlans.Add(plan);
|
||||
|
||||
// ---- 饮食记录 ----
|
||||
var lunch = new DietRecord
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, MealType = MealType.Lunch,
|
||||
TotalCalories = 644, HealthScore = 3, RecordedAt = DateOnly.FromDateTime(DateTime.Now),
|
||||
};
|
||||
lunch.FoodItems.Add(new DietFoodItem { Id = Guid.NewGuid(), Name = "米饭", Portion = "约1碗", Calories = 174, SortOrder = 1 });
|
||||
lunch.FoodItems.Add(new DietFoodItem { Id = Guid.NewGuid(), Name = "红烧肉", Portion = "约5块", Calories = 470, Warning = "脂肪含量偏高", SortOrder = 2 });
|
||||
db.DietRecords.Add(lunch);
|
||||
|
||||
// ---- 复查计划 ----
|
||||
db.FollowUps.Add(new FollowUp
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, Title = "心内科复查",
|
||||
DoctorName = "王建国", Department = "心血管内科",
|
||||
ScheduledAt = DateTime.UtcNow.AddDays(3), Status = FollowUpStatus.Upcoming,
|
||||
});
|
||||
db.FollowUps.Add(new FollowUp
|
||||
{
|
||||
Id = Guid.NewGuid(), UserId = user.Id, Title = "术后3周复查",
|
||||
DoctorName = "王建国", Department = "心血管内科",
|
||||
ScheduledAt = DateTime.UtcNow.AddDays(-14), Status = FollowUpStatus.Completed,
|
||||
});
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
Console.WriteLine($"[DEV] 测试数据已填充:用户 {user.Phone}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Health.Domain\Health.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.18.0" />
|
||||
<PackageReference Include="Minio" Version="7.0.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.2" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.18.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
80
backend/src/Health.Infrastructure/Services/JwtProvider.cs
Normal file
80
backend/src/Health.Infrastructure/Services/JwtProvider.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Health.Infrastructure.Services;
|
||||
|
||||
/// <summary>
|
||||
/// JWT Token 生成与验证服务
|
||||
/// </summary>
|
||||
public sealed class JwtProvider
|
||||
{
|
||||
private readonly string _secret;
|
||||
private readonly string _issuer;
|
||||
private readonly string _audience;
|
||||
|
||||
public JwtProvider(IConfiguration configuration)
|
||||
{
|
||||
_secret = configuration["JWT_SECRET"] ?? "dev-secret-key-change-in-production-min-32-chars!!";
|
||||
_issuer = configuration["JWT_ISSUER"] ?? "health-manager";
|
||||
_audience = configuration["JWT_AUDIENCE"] ?? "health-manager-app";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成 access_token(30 分钟有效)
|
||||
/// </summary>
|
||||
public string GenerateAccessToken(Guid userId, string phone)
|
||||
{
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, userId.ToString()),
|
||||
new Claim(ClaimTypes.MobilePhone, phone),
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
};
|
||||
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_secret));
|
||||
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: _issuer,
|
||||
audience: _audience,
|
||||
claims: claims,
|
||||
expires: DateTime.UtcNow.AddMinutes(30),
|
||||
signingCredentials: credentials
|
||||
);
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成 refresh_token(30 天有效)
|
||||
/// </summary>
|
||||
public string GenerateRefreshToken()
|
||||
{
|
||||
var randomBytes = new byte[64];
|
||||
using var rng = RandomNumberGenerator.Create();
|
||||
rng.GetBytes(randomBytes);
|
||||
return Convert.ToBase64String(randomBytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证 JWT token 并返回 ClaimsPrincipal
|
||||
/// </summary>
|
||||
public TokenValidationParameters GetValidationParameters()
|
||||
{
|
||||
return new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = _issuer,
|
||||
ValidAudience = _audience,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_secret)),
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
}
|
||||
}
|
||||
26
backend/src/Health.Infrastructure/Services/SmsService.cs
Normal file
26
backend/src/Health.Infrastructure/Services/SmsService.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace Health.Infrastructure.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 短信验证码服务(开发阶段直接返回成功)
|
||||
/// </summary>
|
||||
public sealed class SmsService
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送验证码(开发阶段不做真实发送)
|
||||
/// </summary>
|
||||
public Task<bool> SendCodeAsync(string phone, string code)
|
||||
{
|
||||
// 开发阶段:直接在控制台输出,不做真实发送
|
||||
Console.WriteLine($"[SMS DEV] 发送验证码到 {phone}: {code}");
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成 6 位随机数字验证码
|
||||
/// </summary>
|
||||
public string GenerateCode()
|
||||
{
|
||||
// Next(min, max) 的 max 是 exclusive 的,所以用 1000000 保证 6 位
|
||||
return Random.Shared.Next(100000, 1000000).ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user