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:
99
health_app/lib/core/api_client.dart
Normal file
99
health_app/lib/core/api_client.dart
Normal file
@@ -0,0 +1,99 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'secure_storage.dart';
|
||||
|
||||
/// API 基础地址
|
||||
const String baseUrl = 'http://10.4.172.93:5000';
|
||||
|
||||
/// Dio HTTP 客户端封装——带 token 注入、401 自动刷新
|
||||
class ApiClient {
|
||||
final Dio _dio;
|
||||
final SecureStorage _storage;
|
||||
|
||||
ApiClient({required SecureStorage storage})
|
||||
: _storage = storage,
|
||||
_dio = Dio(BaseOptions(
|
||||
baseUrl: baseUrl,
|
||||
connectTimeout: const Duration(seconds: 15),
|
||||
receiveTimeout: const Duration(seconds: 60),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
)) {
|
||||
_dio.interceptors.add(_AuthInterceptor(this));
|
||||
_dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: false));
|
||||
}
|
||||
|
||||
Dio get dio => _dio;
|
||||
|
||||
Future<String?> get accessToken => _storage.readAccessToken();
|
||||
Future<String?> get refreshToken => _storage.readRefreshToken();
|
||||
|
||||
Future<void> saveTokens(String access, String refresh) async {
|
||||
await _storage.writeAccessToken(access);
|
||||
await _storage.writeRefreshToken(refresh);
|
||||
}
|
||||
|
||||
Future<void> clearTokens() async {
|
||||
await _storage.deleteAll();
|
||||
}
|
||||
|
||||
/// 带 token 的 GET 请求
|
||||
Future<Response> get(String path, {Map<String, dynamic>? queryParameters}) async {
|
||||
return _dio.get(path, queryParameters: queryParameters);
|
||||
}
|
||||
|
||||
/// 带 token 的 POST 请求
|
||||
Future<Response> post(String path, {dynamic data}) async {
|
||||
return _dio.post(path, data: data);
|
||||
}
|
||||
|
||||
/// 带 token 的 PUT 请求
|
||||
Future<Response> put(String path, {dynamic data}) async {
|
||||
return _dio.put(path, data: data);
|
||||
}
|
||||
|
||||
/// 带 token 的 DELETE 请求
|
||||
Future<Response> delete(String path) async {
|
||||
return _dio.delete(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// 认证拦截器:自动注入 token + 401 刷新
|
||||
class _AuthInterceptor extends Interceptor {
|
||||
final ApiClient _client;
|
||||
|
||||
_AuthInterceptor(this._client);
|
||||
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
|
||||
if (!options.path.contains('/auth/')) {
|
||||
final token = await _client.accessToken;
|
||||
if (token != null) {
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
}
|
||||
handler.next(options);
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) async {
|
||||
if (err.response?.statusCode == 401) {
|
||||
final refresh = await _client.refreshToken;
|
||||
if (refresh != null) {
|
||||
try {
|
||||
final response = await Dio(BaseOptions(baseUrl: baseUrl))
|
||||
.post('/api/auth/refresh', data: {'refreshToken': refresh});
|
||||
final data = response.data['data'];
|
||||
if (data != null) {
|
||||
await _client.saveTokens(data['accessToken'], data['refreshToken']);
|
||||
final opts = err.requestOptions;
|
||||
final token = data['accessToken'];
|
||||
opts.headers['Authorization'] = 'Bearer $token';
|
||||
final retryResponse = await Dio(BaseOptions(baseUrl: baseUrl)).fetch(opts);
|
||||
return handler.resolve(retryResponse);
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
await _client.clearTokens();
|
||||
}
|
||||
handler.next(err);
|
||||
}
|
||||
}
|
||||
57
health_app/lib/core/app_router.dart
Normal file
57
health_app/lib/core/app_router.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../pages/auth/login_page.dart';
|
||||
import '../pages/home/home_page.dart';
|
||||
import '../pages/chart/trend_page.dart';
|
||||
import '../pages/medication/medication_list_page.dart';
|
||||
import '../pages/report/report_pages.dart';
|
||||
import '../pages/consultation/consultation_pages.dart';
|
||||
import '../pages/settings/settings_pages.dart';
|
||||
import '../pages/profile/profile_page.dart';
|
||||
import '../pages/remaining_pages.dart';
|
||||
|
||||
/// 应用路由配置
|
||||
class AppRouter {
|
||||
AppRouter._();
|
||||
|
||||
static final GoRouter router = GoRouter(
|
||||
initialLocation: '/login',
|
||||
routes: [
|
||||
GoRoute(path: '/login', builder: (_, _) => const LoginPage()),
|
||||
GoRoute(path: '/home', builder: (_, _) => const HomePage()),
|
||||
GoRoute(path: '/trend/:type', builder: (_, state) => TrendPage(metricType: state.pathParameters['type']!)),
|
||||
GoRoute(path: '/calendar', builder: (_, _) => const HealthCalendarPage()),
|
||||
|
||||
// 用药
|
||||
GoRoute(path: '/medications', builder: (_, _) => const MedicationListPage()),
|
||||
GoRoute(path: '/medications/add', builder: (_, _) => const MedicationEditPage()),
|
||||
GoRoute(path: '/medications/:id/edit', builder: (_, state) => MedicationEditPage(id: state.pathParameters['id'])),
|
||||
|
||||
// 报告
|
||||
GoRoute(path: '/reports', builder: (_, _) => const ReportListPage()),
|
||||
GoRoute(path: '/reports/:id', builder: (_, state) => ReportDetailPage(id: state.pathParameters['id']!)),
|
||||
|
||||
// 问诊
|
||||
GoRoute(path: '/doctors', builder: (_, _) => const DoctorListPage()),
|
||||
GoRoute(path: '/consultation/:id', builder: (_, state) => DoctorChatPage(id: state.pathParameters['id']!)),
|
||||
|
||||
// 运动
|
||||
GoRoute(path: '/exercise-plan', builder: (_, _) => const ExercisePlanPage()),
|
||||
|
||||
// 饮食
|
||||
GoRoute(path: '/diet-records', builder: (_, _) => const DietRecordListPage()),
|
||||
|
||||
// 个人中心
|
||||
GoRoute(path: '/profile', builder: (_, _) => const ProfilePage()),
|
||||
GoRoute(path: '/profile/edit', builder: (_, _) => const EditProfilePage()),
|
||||
GoRoute(path: '/health-archive', builder: (_, _) => const HealthArchivePage()),
|
||||
|
||||
// 复查
|
||||
GoRoute(path: '/followups', builder: (_, _) => const FollowUpListPage()),
|
||||
|
||||
// 设置
|
||||
GoRoute(path: '/settings', builder: (_, _) => const SettingsPage()),
|
||||
GoRoute(path: '/settings/notifications', builder: (_, _) => const NotificationPrefsPage()),
|
||||
GoRoute(path: '/page/:type', builder: (_, state) => StaticTextPage(type: state.pathParameters['type']!)),
|
||||
],
|
||||
);
|
||||
}
|
||||
77
health_app/lib/core/app_theme.dart
Normal file
77
health_app/lib/core/app_theme.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 健康管家主题配置——薰衣草紫 + 温暖治愈风
|
||||
class AppTheme {
|
||||
AppTheme._();
|
||||
|
||||
static const Color primaryColor = Color(0xFF635BFF);
|
||||
static const Color primaryLight = Color(0xFFEDEBFF);
|
||||
static const Color primaryDark = Color(0xFF4B44D6);
|
||||
static const Color background = Color(0xFFF8F9FF);
|
||||
static const Color cardWhite = Color(0xFFFFFFFF);
|
||||
static const Color textPrimary = Color(0xFF1A1A1A);
|
||||
static const Color textSecondary = Color(0xFF666666);
|
||||
static const Color textPlaceholder = Color(0xFF999999);
|
||||
static const Color successGreen = Color(0xFF43A047);
|
||||
static const Color errorRed = Color(0xFFE53935);
|
||||
static const Color warningYellow = Color(0xFFF9A825);
|
||||
static const Color secondaryButton = Color(0xFFE5E5F7);
|
||||
|
||||
static ThemeData get lightTheme => ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: primaryColor,
|
||||
primary: primaryColor,
|
||||
surface: background,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
scaffoldBackgroundColor: background,
|
||||
appBarTheme: const AppBarTheme(
|
||||
backgroundColor: cardWhite,
|
||||
foregroundColor: textPrimary,
|
||||
elevation: 0,
|
||||
centerTitle: true,
|
||||
),
|
||||
cardTheme: CardThemeData(
|
||||
color: cardWhite,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
),
|
||||
inputDecorationTheme: InputDecorationTheme(
|
||||
filled: true,
|
||||
fillColor: cardWhite,
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: const BorderSide(color: secondaryButton, width: 1.5),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: const BorderSide(color: secondaryButton, width: 1.5),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: const BorderSide(color: primaryColor, width: 1.5),
|
||||
),
|
||||
hintStyle: const TextStyle(color: textPlaceholder, fontSize: 16),
|
||||
),
|
||||
elevatedButtonTheme: ElevatedButtonThemeData(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
minimumSize: const Size(double.infinity, 48),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)),
|
||||
textStyle: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
textTheme: const TextTheme(
|
||||
headlineLarge: TextStyle(fontSize: 24, fontWeight: FontWeight.w600, color: textPrimary),
|
||||
titleLarge: TextStyle(fontSize: 20, fontWeight: FontWeight.w600, color: textPrimary),
|
||||
bodyLarge: TextStyle(fontSize: 18, fontWeight: FontWeight.w400, color: textPrimary),
|
||||
bodyMedium: TextStyle(fontSize: 16, fontWeight: FontWeight.w400, color: textSecondary),
|
||||
labelMedium: TextStyle(fontSize: 14, fontWeight: FontWeight.w400, color: textSecondary),
|
||||
labelSmall: TextStyle(fontSize: 12, fontWeight: FontWeight.w400, color: textSecondary),
|
||||
),
|
||||
);
|
||||
}
|
||||
35
health_app/lib/core/secure_storage.dart
Normal file
35
health_app/lib/core/secure_storage.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
/// Token 安全存储(iOS Keychain / Android EncryptedSharedPreferences / Web 内存)
|
||||
class SecureStorage {
|
||||
final FlutterSecureStorage _storage;
|
||||
static final Map<String, String> _webFallback = {};
|
||||
|
||||
SecureStorage() : _storage = const FlutterSecureStorage();
|
||||
static const _access = 'access_token';
|
||||
static const _refresh = 'refresh_token';
|
||||
|
||||
bool get _isWeb => kIsWeb;
|
||||
|
||||
Future<void> writeAccessToken(String t) async {
|
||||
if (_isWeb) { _webFallback[_access] = t; return; }
|
||||
await _storage.write(key: _access, value: t);
|
||||
}
|
||||
Future<String?> readAccessToken() async {
|
||||
if (_isWeb) return _webFallback[_access];
|
||||
return _storage.read(key: _access);
|
||||
}
|
||||
Future<void> writeRefreshToken(String t) async {
|
||||
if (_isWeb) { _webFallback[_refresh] = t; return; }
|
||||
await _storage.write(key: _refresh, value: t);
|
||||
}
|
||||
Future<String?> readRefreshToken() async {
|
||||
if (_isWeb) return _webFallback[_refresh];
|
||||
return _storage.read(key: _refresh);
|
||||
}
|
||||
Future<void> deleteAll() async {
|
||||
if (_isWeb) { _webFallback.clear(); return; }
|
||||
await _storage.deleteAll();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user