feat: AI 意图路由/草稿存储 + Prompts 模块化 + UI 视觉统一 + AI 同意门 + 资源更新

后端:
- 新增 ai_intent_router 意图路由
- 新增 AiEntryDraft 草稿存储 + EF 迁移
- 新增 Prompts 模块化(global/modules/rag/router)
- AI Agent handlers 扩展

前端:
- 新增 AI 同意门 (ai_consent_gate/provider/details_page)
- HealthMetricVisuals 视觉统一
- 设备扫描/管理页面优化
- agent 插图替换 + 趋势指标图标

配置:
- DeepSeek 模型改 deepseek-v4-flash
- .gitignore 加 artifacts/audit
- build.gradle.kts 签名配置调整
This commit is contained in:
MingNian
2026-07-28 15:03:38 +08:00
parent 25a4078688
commit b4bc15b9b9
88 changed files with 5527 additions and 1240 deletions

View File

@@ -0,0 +1,337 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../core/app_colors.dart';
import '../core/app_design_tokens.dart';
import '../core/navigation_provider.dart';
import '../providers/ai_consent_provider.dart';
import '../providers/auth_provider.dart';
class AiConsentGate extends ConsumerStatefulWidget {
final Widget child;
const AiConsentGate({super.key, required this.child});
@override
ConsumerState<AiConsentGate> createState() => _AiConsentGateState();
}
class _AiConsentGateState extends ConsumerState<AiConsentGate> {
String? _loadedUserId;
@override
Widget build(BuildContext context) {
final user = ref.watch(authProvider).user;
if (user == null || user.id.isEmpty || user.role != 'User') {
return widget.child;
}
if (_loadedUserId != user.id) {
_loadedUserId = user.id;
Future<void>.microtask(
() => ref.read(aiConsentProvider.notifier).load(user.id),
);
}
final consent = ref.watch(aiConsentProvider);
final currentRoute = ref.watch(currentRouteProvider).name;
final isConsentInformationRoute =
currentRoute == 'aiConsentDetails' || currentRoute == 'staticText';
if (consent.userId != user.id || consent.isLoading) {
return const _ConsentLoadingPage();
}
if (consent.granted || isConsentInformationRoute) return widget.child;
return _ConsentPage(userId: user.id);
}
}
class _ConsentLoadingPage extends StatelessWidget {
const _ConsentLoadingPage();
@override
Widget build(BuildContext context) {
return Scaffold(
body: DecoratedBox(
decoration: const BoxDecoration(gradient: AppColors.bgGradient),
child: const SafeArea(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
width: 30,
height: 30,
child: CircularProgressIndicator(
strokeWidth: 3,
color: AppColors.primary,
),
),
SizedBox(height: 14),
Text(
'正在加载授权状态…',
style: TextStyle(
fontSize: 15,
color: AppColors.textSecondary,
),
),
],
),
),
),
),
);
}
}
class _ConsentPage extends ConsumerWidget {
final String userId;
const _ConsentPage({required this.userId});
Future<void> _grant(BuildContext context, WidgetRef ref) async {
final succeeded = await ref.read(aiConsentProvider.notifier).grant(userId);
if (!succeeded && context.mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('授权保存失败,请稍后重试')));
}
}
Future<void> _decline(WidgetRef ref) async {
await ref.read(authProvider.notifier).logout();
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final consent = ref.watch(aiConsentProvider);
return Scaffold(
body: DecoratedBox(
decoration: const BoxDecoration(gradient: AppColors.bgGradient),
child: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: Container(
padding: const EdgeInsets.fromLTRB(20, 10, 20, 20),
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.96),
borderRadius: AppRadius.cardBorder,
border: Border.all(color: AppColors.primaryLight, width: 1),
boxShadow: AppShadows.panel,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Align(
child: Image.asset(
'assets/branding/health_login_character_transparent.png',
height: 112,
fit: BoxFit.contain,
semanticLabel: '小脉健康智能助手',
),
),
const SizedBox(height: 2),
Align(
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 6,
),
decoration: BoxDecoration(
color: AppColors.primarySoft,
borderRadius: AppRadius.pillBorder,
),
child: const Text(
'小脉健康 · AI 服务',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: AppColors.primaryDark,
),
),
),
),
const SizedBox(height: 14),
const Text(
'AI 健康服务授权',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 22,
height: 1.25,
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
),
),
const SizedBox(height: 14),
const Text(
'小脉健康的核心功能使用第三方 AI 服务。经你同意后,你主动输入的健康信息、对话内容、图片和报告可能会发送给相关 AI 服务,用于健康记录、饮食识别、报告整理和 AI 回复。',
style: TextStyle(
fontSize: 15,
height: 1.6,
color: AppColors.textSecondary,
),
),
const SizedBox(height: 10),
const Text(
'AI 生成内容仅供健康管理参考,不能替代医生诊断或治疗。',
style: TextStyle(
fontSize: 14,
height: 1.5,
color: AppColors.textSecondary,
),
),
if (consent.error != null) ...[
const SizedBox(height: 14),
_ConsentErrorBanner(
message: consent.error!,
onRetry: consent.isSaving
? null
: () => ref
.read(aiConsentProvider.notifier)
.load(userId),
),
],
const SizedBox(height: 8),
TextButton(
onPressed: consent.isSaving
? null
: () => pushRoute(ref, 'aiConsentDetails'),
child: const Text('查看《AI 数据处理说明》'),
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: _GradientConsentButton(
loading: consent.isSaving,
onTap: consent.isSaving
? null
: () => _grant(context, ref),
),
),
const SizedBox(width: 12),
Expanded(
child: OutlinedButton(
onPressed: consent.isSaving
? null
: () => _decline(ref),
style: OutlinedButton.styleFrom(
minimumSize: const Size.fromHeight(50),
foregroundColor: AppColors.textSecondary,
side: const BorderSide(color: AppColors.border),
shape: RoundedRectangleBorder(
borderRadius: AppRadius.lgBorder,
),
),
child: const Text('不同意并退出'),
),
),
],
),
],
),
),
),
),
),
),
),
);
}
}
class _ConsentErrorBanner extends StatelessWidget {
final String message;
final VoidCallback? onRetry;
const _ConsentErrorBanner({required this.message, this.onRetry});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.fromLTRB(12, 10, 8, 10),
decoration: BoxDecoration(
color: AppColors.errorLight,
borderRadius: AppRadius.mdBorder,
),
child: Row(
children: [
const Icon(
Icons.error_outline_rounded,
size: 20,
color: AppColors.errorText,
),
const SizedBox(width: 8),
Expanded(
child: Text(
message,
style: const TextStyle(fontSize: 13, color: AppColors.errorText),
),
),
if (onRetry != null)
TextButton(onPressed: onRetry, child: const Text('重试')),
],
),
);
}
}
class _GradientConsentButton extends StatelessWidget {
final bool loading;
final VoidCallback? onTap;
const _GradientConsentButton({required this.loading, required this.onTap});
@override
Widget build(BuildContext context) {
return Semantics(
button: true,
enabled: onTap != null,
label: loading ? '正在保存授权' : '同意并继续',
child: IgnorePointer(
ignoring: onTap == null,
child: AnimatedOpacity(
duration: const Duration(milliseconds: 160),
opacity: onTap == null && !loading ? 0.55 : 1,
child: Container(
height: 50,
decoration: BoxDecoration(
gradient: AppColors.primaryGradient,
borderRadius: AppRadius.lgBorder,
boxShadow: AppColors.buttonShadow,
),
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
borderRadius: AppRadius.lgBorder,
child: Center(
child: loading
? const SizedBox(
width: 22,
height: 22,
child: CircularProgressIndicator(
strokeWidth: 2.4,
color: Colors.white,
),
)
: const Text(
'同意并继续',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
color: Colors.white,
),
),
),
),
),
),
),
),
);
}
}

View File

@@ -7,6 +7,9 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../core/api_client.dart' show baseUrl;
import '../providers/auth_provider.dart';
const int _maxProtectedImageCacheEntries = 32;
final Map<String, Uint8List> _protectedImageBytesCache = {};
String protectedMediaUrl(String value) {
final trimmed = value.trim();
if (trimmed.isEmpty) return trimmed;
@@ -58,6 +61,7 @@ class _AuthenticatedNetworkImageState
extends ConsumerState<AuthenticatedNetworkImage> {
late String _resolvedUrl;
Future<Uint8List>? _bytes;
Uint8List? _cachedBytes;
@override
void initState() {
@@ -73,11 +77,21 @@ class _AuthenticatedNetworkImageState
void _configure() {
_resolvedUrl = protectedMediaUrl(widget.imageUrl);
_bytes = mediaRequiresAuthentication(_resolvedUrl)
? _loadProtectedBytes(_resolvedUrl)
_cachedBytes = _protectedImageBytesCache[_resolvedUrl];
_bytes = mediaRequiresAuthentication(_resolvedUrl) && _cachedBytes == null
? _loadAndCacheProtectedBytes(_resolvedUrl)
: null;
}
Future<Uint8List> _loadAndCacheProtectedBytes(String url) async {
final data = await _loadProtectedBytes(url);
if (_protectedImageBytesCache.length >= _maxProtectedImageCacheEntries) {
_protectedImageBytesCache.remove(_protectedImageBytesCache.keys.first);
}
_protectedImageBytesCache[url] = data;
return data;
}
Future<Uint8List> _loadProtectedBytes(String url) async {
final response = await ref
.read(apiClientProvider)
@@ -91,6 +105,18 @@ class _AuthenticatedNetworkImageState
@override
Widget build(BuildContext context) {
final cachedBytes = _cachedBytes;
if (cachedBytes != null) {
return Image.memory(
cachedBytes,
fit: widget.fit,
width: widget.width,
height: widget.height,
gaplessPlayback: true,
errorBuilder: widget.errorBuilder,
);
}
final bytes = _bytes;
if (bytes == null) {
return Image.network(
@@ -98,6 +124,7 @@ class _AuthenticatedNetworkImageState
fit: widget.fit,
width: widget.width,
height: widget.height,
gaplessPlayback: true,
loadingBuilder: widget.loadingBuilder,
errorBuilder: widget.errorBuilder,
);
@@ -123,6 +150,7 @@ class _AuthenticatedNetworkImageState
fit: widget.fit,
width: widget.width,
height: widget.height,
gaplessPlayback: true,
errorBuilder: widget.errorBuilder,
);
},

View File

@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../core/app_colors.dart';
import '../core/app_module_visuals.dart';
import '../core/navigation_provider.dart';
import '../providers/auth_provider.dart';
import '../pages/doctor/doctor_home_page.dart' show doctorPageProvider;
@@ -64,7 +65,7 @@ class DoctorDrawer extends ConsumerWidget {
),
),
_DrawerItem(
icon: Icons.description_outlined,
icon: AppModuleVisuals.report.icon,
label: '报告审核',
selected: currentPage == 'reports',
onTap: () => runAfterDrawerClose(
@@ -73,7 +74,7 @@ class DoctorDrawer extends ConsumerWidget {
),
),
_DrawerItem(
icon: Icons.event_note_outlined,
icon: AppModuleVisuals.followup.icon,
label: '复查随访',
selected: currentPage == 'followups',
onTap: () => runAfterDrawerClose(

View File

@@ -10,6 +10,7 @@ import '../providers/auth_provider.dart';
import '../providers/chat_provider.dart';
import '../providers/conversation_history_provider.dart';
import 'app_toast.dart';
import 'authenticated_network_image.dart';
import '../providers/data_providers.dart';
import 'drawer_shell.dart';
@@ -136,9 +137,7 @@ class _AccountHeader extends StatelessWidget {
width: 66,
height: 66,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
border: Border.all(color: Colors.white, width: 2),
boxShadow: [
BoxShadow(
color: const Color(0xFF7C5CFF).withValues(alpha: 0.16),
@@ -147,10 +146,33 @@ class _AccountHeader extends StatelessWidget {
),
],
),
child: const Icon(
Icons.person_rounded,
color: Color(0xFF94A3B8),
size: 34,
child: Stack(
fit: StackFit.expand,
children: [
ClipOval(
clipBehavior: Clip.antiAlias,
child: ColoredBox(
color: const Color(0xFFF1F5F9),
child: user?.avatarUrl?.toString().isNotEmpty == true
? AuthenticatedNetworkImage(
imageUrl: user.avatarUrl.toString(),
fit: BoxFit.cover,
width: 66,
height: 66,
errorBuilder: (_, _, _) => const Icon(
Icons.person_rounded,
color: Color(0xFF94A3B8),
size: 34,
),
)
: const Icon(
Icons.person_rounded,
color: Color(0xFF94A3B8),
size: 34,
),
),
),
],
),
),
),
@@ -208,11 +230,6 @@ class _HealthDashboard extends StatelessWidget {
Widget build(BuildContext context) {
return _Panel(
title: '健康仪表盘',
trailing: _TextAction(
label: '详情',
light: true,
onTap: () => pushRoute(ref, 'trend'),
),
titleColor: Colors.white,
backgroundPainter: const _HealthDashboardBackgroundPainter(),
child: latestHealth.when(
@@ -313,7 +330,7 @@ class _MetricTile extends StatelessWidget {
onTap: onTap,
borderRadius: BorderRadius.circular(18),
child: Container(
height: elderMode ? 112 : 100,
height: elderMode ? 108 : 100,
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 10),
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.22),
@@ -627,13 +644,11 @@ class _LightSection extends StatelessWidget {
class _Panel extends StatelessWidget {
final String title;
final Widget child;
final Widget? trailing;
final CustomPainter? backgroundPainter;
final Color titleColor;
const _Panel({
required this.title,
required this.child,
this.trailing,
this.backgroundPainter,
this.titleColor = AppColors.textPrimary,
});
@@ -664,20 +679,13 @@ class _Panel extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: titleColor,
),
),
),
?trailing,
],
Text(
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: titleColor,
),
),
const SizedBox(height: 12),
child,
@@ -708,45 +716,6 @@ class _HealthDashboardBackgroundPainter extends CustomPainter {
bool shouldRepaint(_HealthDashboardBackgroundPainter oldDelegate) => false;
}
class _TextAction extends StatelessWidget {
final String label;
final VoidCallback onTap;
final bool light;
const _TextAction({
required this.label,
required this.onTap,
this.light = false,
});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(999),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 11, vertical: 6),
decoration: BoxDecoration(
color: light
? Colors.white.withValues(alpha: 0.24)
: const Color(0xFFEDE9FE),
borderRadius: BorderRadius.circular(999),
border: light
? Border.all(color: Colors.white.withValues(alpha: 0.34))
: null,
),
child: Text(
label,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w700,
color: light ? Colors.white : Color(0xFF6D28D9),
),
),
),
);
}
}
class _IconButton extends StatelessWidget {
final IconData icon;
final VoidCallback onTap;