feat: 后端架构重构 — Endpoint→Service→Repository分层 + AI确认机制 + 异步任务持久化
- 核心业务拆分为 Endpoint → Application Service → Repository 三层 - AI写入操作必须用户确认后才写库(确认卡片机制) - 报告/饮食/用药分析改为持久化任务队列(原子领取/重试/重启恢复) - 运动计划修复: 连续真实日期替代周模板 - 用药提醒去重 + 通知Outbox预留 - 认证收拢到AuthService, 管理员收拢到AdminService - AI会话加用户归属校验防串号 - 提示词调整为患者视角 - 开发假数据已关闭 - 21/21测试通过, 0警告0错误
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import '../../core/app_colors.dart';
|
||||
import '../../core/app_theme.dart';
|
||||
import '../../core/api_client.dart' show baseUrl;
|
||||
import '../../core/navigation_provider.dart';
|
||||
import '../../providers/auth_provider.dart';
|
||||
|
||||
@@ -19,12 +20,14 @@ class ReportState {
|
||||
final String? uploadingImage;
|
||||
final bool isAnalyzing;
|
||||
final ReportAnalysis? currentAnalysis;
|
||||
final String? uploadError;
|
||||
|
||||
ReportState({
|
||||
this.reports = const [],
|
||||
this.uploadingImage,
|
||||
this.isAnalyzing = false,
|
||||
this.currentAnalysis,
|
||||
this.uploadError,
|
||||
});
|
||||
|
||||
ReportState copyWith({
|
||||
@@ -32,12 +35,21 @@ class ReportState {
|
||||
String? uploadingImage,
|
||||
bool? isAnalyzing,
|
||||
ReportAnalysis? currentAnalysis,
|
||||
String? uploadError,
|
||||
bool clearUploadingImage = false,
|
||||
bool clearCurrentAnalysis = false,
|
||||
bool clearUploadError = false,
|
||||
}) {
|
||||
return ReportState(
|
||||
reports: reports ?? this.reports,
|
||||
uploadingImage: uploadingImage ?? this.uploadingImage,
|
||||
uploadingImage: clearUploadingImage
|
||||
? null
|
||||
: uploadingImage ?? this.uploadingImage,
|
||||
isAnalyzing: isAnalyzing ?? this.isAnalyzing,
|
||||
currentAnalysis: currentAnalysis ?? this.currentAnalysis,
|
||||
currentAnalysis: clearCurrentAnalysis
|
||||
? null
|
||||
: currentAnalysis ?? this.currentAnalysis,
|
||||
uploadError: clearUploadError ? null : uploadError ?? this.uploadError,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -47,9 +59,12 @@ class ReportItem {
|
||||
final String title;
|
||||
final String type;
|
||||
final DateTime uploadedAt;
|
||||
final String? imagePath;
|
||||
final String? fileUrl;
|
||||
final bool hasAnalysis;
|
||||
final String status; // PendingDoctor | DoctorReviewed
|
||||
final String status; // Legacy combined status from backend.
|
||||
final String aiStatus; // Analyzing | Succeeded | Failed
|
||||
final String reviewStatus; // Pending | Reviewed
|
||||
final String? analysisSummary;
|
||||
final String? severity; // Normal | Abnormal | Severe | Critical
|
||||
final String? doctorComment;
|
||||
final String? doctorRecommendation;
|
||||
@@ -61,9 +76,12 @@ class ReportItem {
|
||||
required this.title,
|
||||
required this.type,
|
||||
required this.uploadedAt,
|
||||
this.imagePath,
|
||||
this.fileUrl,
|
||||
this.hasAnalysis = false,
|
||||
this.status = 'PendingDoctor',
|
||||
this.aiStatus = 'Analyzing',
|
||||
this.reviewStatus = 'Pending',
|
||||
this.analysisSummary,
|
||||
this.severity,
|
||||
this.doctorComment,
|
||||
this.doctorRecommendation,
|
||||
@@ -77,12 +95,20 @@ class ReportAnalysis {
|
||||
final String reportType;
|
||||
final List<Indicator> indicators;
|
||||
final String summary;
|
||||
final String status;
|
||||
final String aiStatus;
|
||||
final String reviewStatus;
|
||||
final String? fileUrl;
|
||||
|
||||
ReportAnalysis({
|
||||
required this.reportId,
|
||||
required this.reportType,
|
||||
required this.indicators,
|
||||
required this.summary,
|
||||
this.status = 'PendingDoctor',
|
||||
this.aiStatus = 'Analyzing',
|
||||
this.reviewStatus = 'Pending',
|
||||
this.fileUrl,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -103,8 +129,11 @@ class Indicator {
|
||||
}
|
||||
|
||||
class ReportNotifier extends Notifier<ReportState> {
|
||||
Timer? _pollTimer;
|
||||
|
||||
@override
|
||||
ReportState build() {
|
||||
ref.onDispose(() => _pollTimer?.cancel());
|
||||
Future.microtask(() => loadReports());
|
||||
return ReportState();
|
||||
}
|
||||
@@ -129,8 +158,12 @@ class ReportNotifier extends Notifier<ReportState> {
|
||||
uploadedAt:
|
||||
DateTime.tryParse(m['createdAt']?.toString() ?? '') ??
|
||||
DateTime.now(),
|
||||
fileUrl: m['fileUrl']?.toString(),
|
||||
hasAnalysis: m['aiSummary'] != null,
|
||||
status: m['status']?.toString() ?? 'PendingDoctor',
|
||||
aiStatus: m['aiStatus']?.toString() ?? _deriveAiStatus(m),
|
||||
reviewStatus: m['reviewStatus']?.toString() ?? _deriveReviewStatus(m),
|
||||
analysisSummary: m['aiSummary']?.toString(),
|
||||
severity: m['severity']?.toString(),
|
||||
doctorComment: m['doctorComment']?.toString(),
|
||||
doctorRecommendation: m['doctorRecommendation']?.toString(),
|
||||
@@ -139,11 +172,25 @@ class ReportNotifier extends Notifier<ReportState> {
|
||||
);
|
||||
}).toList();
|
||||
state = state.copyWith(reports: reports);
|
||||
_syncAnalysisPolling(reports);
|
||||
} catch (e) {
|
||||
debugPrint('[Report] 加载报告列表失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void _syncAnalysisPolling(List<ReportItem> reports) {
|
||||
final hasAnalyzing = reports.any((r) => r.aiStatus == 'Analyzing');
|
||||
if (!hasAnalyzing) {
|
||||
_pollTimer?.cancel();
|
||||
_pollTimer = null;
|
||||
return;
|
||||
}
|
||||
_pollTimer ??= Timer.periodic(
|
||||
const Duration(seconds: 4),
|
||||
(_) => loadReports(),
|
||||
);
|
||||
}
|
||||
|
||||
void fetchReportDetail(String reportId) async {
|
||||
try {
|
||||
final api = ref.read(apiClientProvider);
|
||||
@@ -164,6 +211,10 @@ class ReportNotifier extends Notifier<ReportState> {
|
||||
reportType: displayType,
|
||||
indicators: indicators,
|
||||
summary: m['aiSummary']?.toString() ?? '',
|
||||
status: m['status']?.toString() ?? 'PendingDoctor',
|
||||
aiStatus: m['aiStatus']?.toString() ?? _deriveAiStatus(m),
|
||||
reviewStatus: m['reviewStatus']?.toString() ?? _deriveReviewStatus(m),
|
||||
fileUrl: m['fileUrl']?.toString(),
|
||||
);
|
||||
state = state.copyWith(currentAnalysis: analysis);
|
||||
} catch (_) {
|
||||
@@ -176,8 +227,21 @@ class ReportNotifier extends Notifier<ReportState> {
|
||||
reportType: '检查报告',
|
||||
indicators: [],
|
||||
summary: '暂无数据,请下拉刷新重试',
|
||||
status: 'AnalysisFailed',
|
||||
aiStatus: 'Failed',
|
||||
reviewStatus: 'Pending',
|
||||
);
|
||||
|
||||
String _deriveAiStatus(Map<String, dynamic> m) {
|
||||
final status = m['status']?.toString();
|
||||
if (status == 'Analyzing') return 'Analyzing';
|
||||
if (status == 'AnalysisFailed') return 'Failed';
|
||||
return m['aiSummary'] != null ? 'Succeeded' : 'Analyzing';
|
||||
}
|
||||
|
||||
String _deriveReviewStatus(Map<String, dynamic> m) =>
|
||||
m['status']?.toString() == 'DoctorReviewed' ? 'Reviewed' : 'Pending';
|
||||
|
||||
List<Indicator> _parseIndicators(String? jsonStr) {
|
||||
if (jsonStr == null || jsonStr.isEmpty) return [];
|
||||
try {
|
||||
@@ -198,7 +262,11 @@ class ReportNotifier extends Notifier<ReportState> {
|
||||
}
|
||||
|
||||
void uploadImage(String path) async {
|
||||
state = state.copyWith(uploadingImage: path, isAnalyzing: true);
|
||||
state = state.copyWith(
|
||||
uploadingImage: path,
|
||||
isAnalyzing: true,
|
||||
clearUploadError: true,
|
||||
);
|
||||
try {
|
||||
final api = ref.read(apiClientProvider);
|
||||
final file = File(path);
|
||||
@@ -211,14 +279,28 @@ class ReportNotifier extends Notifier<ReportState> {
|
||||
// 直接上传到报告端点,后端自动触发 VLM+LLM 分析
|
||||
final createRes = await api.dio.post('/api/reports', data: formData);
|
||||
final data = createRes.data;
|
||||
final reportId = data is Map
|
||||
? (data['data']?['id']?.toString() ?? '')
|
||||
: '';
|
||||
|
||||
state = state.copyWith(isAnalyzing: false, uploadingImage: null);
|
||||
if (data is Map && data['code'] != 0) {
|
||||
final message = data['message']?.toString() ?? '上传失败,请稍后重试';
|
||||
state = state.copyWith(
|
||||
isAnalyzing: false,
|
||||
clearUploadingImage: true,
|
||||
uploadError: message,
|
||||
);
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(
|
||||
isAnalyzing: false,
|
||||
clearUploadingImage: true,
|
||||
clearUploadError: true,
|
||||
);
|
||||
loadReports();
|
||||
} catch (_) {
|
||||
state = state.copyWith(isAnalyzing: false, uploadingImage: null);
|
||||
} catch (e) {
|
||||
debugPrint('[Report] 上传失败: $e');
|
||||
state = state.copyWith(
|
||||
isAnalyzing: false,
|
||||
clearUploadingImage: true,
|
||||
uploadError: '上传失败,请检查网络或文件格式后重试',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,8 +319,25 @@ class ReportNotifier extends Notifier<ReportState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> reanalyzeReport(String id) async {
|
||||
try {
|
||||
final res = await ref.read(apiClientProvider).post('/api/reports/$id/reanalyze');
|
||||
final data = res.data;
|
||||
if (data is Map && data['code'] != 0) {
|
||||
state = state.copyWith(uploadError: data['message']?.toString() ?? '重新分析失败');
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(clearCurrentAnalysis: true, clearUploadError: true);
|
||||
await loadReports();
|
||||
fetchReportDetail(id);
|
||||
} catch (e) {
|
||||
debugPrint('[Report] 重新分析失败: $e');
|
||||
state = state.copyWith(uploadError: '重新分析失败,请稍后重试');
|
||||
}
|
||||
}
|
||||
|
||||
void clearAnalysis() {
|
||||
state = state.copyWith(currentAnalysis: null);
|
||||
state = state.copyWith(clearCurrentAnalysis: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,15 +352,18 @@ class ReportListPage extends ConsumerWidget {
|
||||
if (state.isAnalyzing) {
|
||||
return GradientScaffold(
|
||||
appBar: AppBar(title: const Text('报告管理')),
|
||||
body: const Center(
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
CircularProgressIndicator(color: AppColors.primary),
|
||||
SizedBox(height: 16),
|
||||
const CircularProgressIndicator(color: AppColors.primary),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'AI 正在分析报告...',
|
||||
style: TextStyle(fontSize: 16, color: AppColors.textSecondary),
|
||||
state.uploadingImage == null ? 'AI 正在分析报告...' : '正在上传报告...',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -281,14 +383,48 @@ class ReportListPage extends ConsumerWidget {
|
||||
floatingActionButton: _buildUploadButton(context, ref),
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async => ref.read(reportProvider.notifier).loadReports(),
|
||||
child: state.reports.isEmpty
|
||||
? ListView(children: [_buildEmptyState(context)])
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: state.reports.length,
|
||||
itemBuilder: (context, index) =>
|
||||
_buildReportCard(context, ref, state.reports[index]),
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
if (state.uploadError != null) ...[
|
||||
_buildUploadError(state.uploadError!),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
if (state.reports.isEmpty)
|
||||
_buildEmptyState(context)
|
||||
else
|
||||
...state.reports.map(
|
||||
(report) => _buildReportCard(context, ref, report),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildUploadError(String message) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.error.withValues(alpha: 0.08),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.error.withValues(alpha: 0.22)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.error_outline, color: AppColors.error, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
message,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: AppColors.error,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -327,8 +463,9 @@ class ReportListPage extends ConsumerWidget {
|
||||
source: ImageSource.camera,
|
||||
imageQuality: 85,
|
||||
);
|
||||
if (picked != null)
|
||||
if (picked != null) {
|
||||
ref.read(reportProvider.notifier).uploadImage(picked.path);
|
||||
}
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
@@ -344,26 +481,9 @@ class ReportListPage extends ConsumerWidget {
|
||||
source: ImageSource.gallery,
|
||||
imageQuality: 85,
|
||||
);
|
||||
if (picked != null)
|
||||
if (picked != null) {
|
||||
ref.read(reportProvider.notifier).uploadImage(picked.path);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(
|
||||
Icons.picture_as_pdf_outlined,
|
||||
color: AppColors.primary,
|
||||
),
|
||||
title: const Text('上传PDF文件', style: TextStyle(fontSize: 17)),
|
||||
onTap: () async {
|
||||
Navigator.pop(ctx);
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ['pdf'],
|
||||
);
|
||||
if (result != null && result.files.isNotEmpty)
|
||||
ref
|
||||
.read(reportProvider.notifier)
|
||||
.uploadFile(result.files.first.path!);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
@@ -471,66 +591,24 @@ class ReportListPage extends ConsumerWidget {
|
||||
color: AppColors.textHint,
|
||||
),
|
||||
),
|
||||
if (report.aiStatus == 'Failed') ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
_failureSummary(report),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
height: 1.35,
|
||||
color: AppColors.error,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
if (report.status == 'DoctorReviewed')
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8,
|
||||
vertical: 3,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.successLight,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Text(
|
||||
'已审核',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: AppColors.success,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
)
|
||||
else if (!report.hasAnalysis)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8,
|
||||
vertical: 3,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.cardInner,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Text(
|
||||
'分析中',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: AppColors.textHint,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8,
|
||||
vertical: 3,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.warningLight,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Text(
|
||||
'待审核',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: AppColors.warning,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildStatusBadge(report),
|
||||
const SizedBox(width: 4),
|
||||
GestureDetector(
|
||||
onTap: () => _confirmDelete(context, ref, report.id),
|
||||
@@ -574,6 +652,40 @@ class ReportListPage extends ConsumerWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusBadge(ReportItem report) {
|
||||
final (label, bg, fg) = switch (report.status) {
|
||||
_ when report.reviewStatus == 'Reviewed' => ('已审核', AppColors.successLight, AppColors.success),
|
||||
_ when report.aiStatus == 'Analyzing' => ('分析中', AppColors.cardInner, AppColors.textHint),
|
||||
_ when report.aiStatus == 'Failed' => ('分析失败', AppColors.error.withValues(alpha: 0.08), AppColors.error),
|
||||
_ when report.aiStatus == 'Succeeded' => ('待审核', AppColors.warningLight, AppColors.warning),
|
||||
_ => ('分析中', AppColors.cardInner, AppColors.textHint),
|
||||
};
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
||||
decoration: BoxDecoration(
|
||||
color: bg,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: fg,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _failureSummary(ReportItem report) {
|
||||
final summary = report.analysisSummary?.trim();
|
||||
if (summary != null && summary.isNotEmpty) {
|
||||
return summary;
|
||||
}
|
||||
return 'AI 暂时无法解读,请稍后重试或重新上传清晰图片';
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
final now = DateTime.now();
|
||||
final diff = now.difference(date);
|
||||
@@ -593,3 +705,49 @@ String _catTitle(String c) => switch (c) {
|
||||
'Image' => '影像检查报告',
|
||||
_ => '检查报告',
|
||||
};
|
||||
|
||||
class ReportOriginalPage extends StatelessWidget {
|
||||
final String url;
|
||||
final String title;
|
||||
|
||||
const ReportOriginalPage({
|
||||
super.key,
|
||||
required this.url,
|
||||
this.title = '原始报告',
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final imageUrl = _absoluteUrl(url);
|
||||
return GradientScaffold(
|
||||
appBar: AppBar(title: Text(title)),
|
||||
body: Center(
|
||||
child: InteractiveViewer(
|
||||
minScale: 0.7,
|
||||
maxScale: 4,
|
||||
child: Image.network(
|
||||
imageUrl,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (_, _, _) => const Padding(
|
||||
padding: EdgeInsets.all(24),
|
||||
child: Text(
|
||||
'原始报告图片加载失败',
|
||||
style: TextStyle(color: AppColors.textSecondary),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _absoluteUrl(String value) {
|
||||
if (value.startsWith('http://') || value.startsWith('https://')) {
|
||||
return value;
|
||||
}
|
||||
if (value.startsWith('/')) {
|
||||
return '$baseUrl$value';
|
||||
}
|
||||
return '$baseUrl/$value';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user