import 'dart:convert'; import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; import 'package:path_provider/path_provider.dart'; import 'package:path/path.dart' as p; import '../../models/data_models.dart'; import '../user_prefs.dart'; /// 服务端数据服务 - 所有数据操作通过远程 API class ServerDataService { static final ServerDataService instance = ServerDataService._(); ServerDataService._(); final UserPrefs _prefs = UserPrefs(); String get _baseUrl => _prefs.syncServerUrl; String get _code => _prefs.syncActivationCode; Map get _headers => {'Content-Type': 'application/json'}; Map _body([Map? extra]) { return {'code': _code, ...?extra}; } bool get isAvailable => _baseUrl.isNotEmpty && _code.isNotEmpty; Future _post(String path, [Map? extra]) async { final resp = await http.post( Uri.parse('$_baseUrl$path'), headers: _headers, body: jsonEncode(_body(extra)), ).timeout(const Duration(seconds: 30)); if (resp.statusCode != 200) return null; return jsonDecode(resp.body); } // ─── 影视 ──────────────────────────────────────────────────── Future> getMovies() async { final data = await _post('/api/data/movies'); if (data == null || data['movies'] == null) return []; return (data['movies'] as List).map((m) => Movie.fromJson(m as Map)).toList(); } Future saveMovie(Movie movie) async { final data = await _post('/api/data/movie/save', {'movie': movie.toJson()}); return data != null; } Future deleteMovie(String id) async { final data = await _post('/api/data/movie/delete', {'id': id}); return data != null; } // ─── 书籍 ──────────────────────────────────────────────────── Future> getBooks() async { final data = await _post('/api/data/books'); if (data == null || data['books'] == null) return []; return (data['books'] as List).map((b) => Book.fromJson(b as Map)).toList(); } Future saveBook(Book book) async { final data = await _post('/api/data/book/save', {'book': book.toJson()}); return data != null; } Future deleteBook(String id) async { final data = await _post('/api/data/book/delete', {'id': id}); return data != null; } // ─── 笔记 ──────────────────────────────────────────────────── Future> getNotes() async { final data = await _post('/api/data/notes'); if (data == null || data['notes'] == null) return []; return (data['notes'] as List).map((n) => Note.fromJson(n as Map)).toList(); } Future saveNote(Note note) async { final data = await _post('/api/data/note/save', {'note': note.toJson()}); return data != null; } Future deleteNote(String id) async { final data = await _post('/api/data/note/delete', {'id': id}); return data != null; } // ─── 标签 ──────────────────────────────────────────────────── Future>> getTags(String? type) async { final data = await _post('/api/data/tags', type != null ? {'type': type} : null); if (data == null || data['tags'] == null) return []; return (data['tags'] as List).map((t) => Map.from(t as Map)).toList(); } Future saveTag(String name, String type) async { final data = await _post('/api/data/tag/save', {'tag': {'name': name, 'type': type}}); return data != null; } Future deleteTag(String id) async { final data = await _post('/api/data/tag/delete', {'id': id}); return data != null; } // ─── 图片 ──────────────────────────────────────────────────── /// 是否激活(AppProvider 也会用这个检查) static bool get isActive { final p = UserPrefs(); return p.syncEnabled && p.syncServerUrl.isNotEmpty && p.syncActivationCode.isNotEmpty; } /// 将本地路径转为服务端图片 URL static Future toImageUrl(String localPath) async { if (!isActive) return localPath; final appDir = (await getApplicationDocumentsDirectory()).path; final relPath = p.relative(localPath, from: appDir).replaceAll('\\', '/'); final prefs = UserPrefs(); return '${prefs.syncServerUrl}/api/data/image/${prefs.syncActivationCode}/$relPath'; } /// 批量上传图片到服务端(自动计算相对路径) static Future uploadLocalImages(List filePaths) async { if (!isActive || filePaths.isEmpty) return; final result = await instance.uploadImages(filePaths); debugPrint('[Sync] 上传 ${result.length}/${filePaths.length} 张图片'); } /// 上传单张图片到服务端 static Future uploadLocalImage(String filePath) async { if (!isActive || filePath.isEmpty) return; final result = await instance.uploadImage(filePath); debugPrint('[Sync] 上传图片: ${result ?? "失败"}'); } String imageUrl(String relPath) { return '$_baseUrl/api/data/image/$_code/$relPath'; } Future> uploadImages(List filePaths) async { final request = http.MultipartRequest('POST', Uri.parse('$_baseUrl/api/data/image/upload')); request.fields['code'] = _code; final appDir = (await getApplicationDocumentsDirectory()).path; for (final path in filePaths) { final relPath = p.relative(path, from: appDir).replaceAll('\\', '/'); final file = File(path); request.files.add(await http.MultipartFile( 'images', file.readAsBytes().asStream(), await file.length(), filename: relPath, )); } final resp = await request.send().timeout(const Duration(seconds: 60)); if (resp.statusCode != 200) return []; final body = await resp.stream.bytesToString(); final data = jsonDecode(body) as Map; return (data['files'] as List?)?.cast() ?? []; } Future uploadImage(String filePath) async { final files = await uploadImages([filePath]); return files.isNotEmpty ? files.first : null; } }