去掉服务器同步

This commit is contained in:
DelLevin-Home
2026-06-21 10:28:09 +08:00
parent a84d4c03e8
commit e4ed73d604
10 changed files with 32 additions and 1588 deletions

View File

@@ -1,385 +0,0 @@
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<String, String> get _headers => {'Content-Type': 'application/json'};
Map<String, dynamic> _body([Map<String, dynamic>? extra]) {
return {'code': _code, ...?extra};
}
bool get isAvailable => _baseUrl.isNotEmpty && _code.isNotEmpty;
Future<dynamic> _post(String path, [Map<String, dynamic>? extra]) async {
try {
final url = '$_baseUrl$path';
debugPrint('[ServerData] POST $url');
final resp = await http.post(
Uri.parse(url),
headers: _headers,
body: jsonEncode(_body(extra)),
).timeout(const Duration(seconds: 30));
debugPrint('[ServerData] ${resp.statusCode} $path');
if (resp.statusCode != 200) return null;
return jsonDecode(resp.body);
} catch (e) {
debugPrint('[ServerData] ERROR $path: $e');
return null;
}
}
// ─── 影视 ────────────────────────────────────────────────────
Future<List<Movie>> getMovies({String? status, int? limit, int? offset}) async {
final body = <String, dynamic>{};
if (status != null && status.isNotEmpty) body['status'] = status;
if (limit != null) body['limit'] = limit;
if (offset != null) body['offset'] = offset;
final data = await _post('/api/data/movies', body.isEmpty ? null : body);
if (data == null || data['movies'] == null) return [];
return (data['movies'] as List).map((m) => Movie.fromJson(m as Map<String, dynamic>)).toList();
}
Future<bool> saveMovie(Movie movie) async {
final data = await _post('/api/data/movie/save', {'movie': movie.toJson()});
return data != null;
}
Future<bool> deleteMovie(String id) async {
final data = await _post('/api/data/movie/delete', {'id': id});
return data != null;
}
// ─── 书籍 ────────────────────────────────────────────────────
Future<List<Book>> getBooks({String? status, int? limit, int? offset}) async {
final body = <String, dynamic>{};
if (status != null && status.isNotEmpty) body['status'] = status;
if (limit != null) body['limit'] = limit;
if (offset != null) body['offset'] = offset;
final data = await _post('/api/data/books', body.isEmpty ? null : body);
if (data == null || data['books'] == null) return [];
return (data['books'] as List).map((b) => Book.fromJson(b as Map<String, dynamic>)).toList();
}
Future<bool> saveBook(Book book) async {
final data = await _post('/api/data/book/save', {'book': book.toJson()});
return data != null;
}
Future<bool> deleteBook(String id) async {
final data = await _post('/api/data/book/delete', {'id': id});
return data != null;
}
// ─── 笔记 ────────────────────────────────────────────────────
Future<List<Note>> getNotes({int? limit, int? offset}) async {
final body = <String, dynamic>{};
if (limit != null) body['limit'] = limit;
if (offset != null) body['offset'] = offset;
final data = await _post('/api/data/notes', body.isEmpty ? null : body);
if (data == null || data['notes'] == null) return [];
return (data['notes'] as List).map((n) => Note.fromJson(n as Map<String, dynamic>)).toList();
}
Future<bool> saveNote(Note note) async {
final data = await _post('/api/data/note/save', {'note': note.toJson()});
return data != null;
}
Future<bool> deleteNote(String id) async {
final data = await _post('/api/data/note/delete', {'id': id});
return data != null;
}
// ─── 回收站 ────────────────────────────────────────────────────
Future<List<Movie>> getDeletedMovies() async {
final data = await _post('/api/data/movies/deleted');
if (data == null || data['movies'] == null) return [];
return (data['movies'] as List).map((m) => Movie.fromJson(m as Map<String, dynamic>)).toList();
}
Future<bool> restoreMovie(String id) async {
final data = await _post('/api/data/movie/restore', {'id': id});
return data != null;
}
Future<bool> permanentDeleteMovie(String id) async {
final data = await _post('/api/data/movie/permanent_delete', {'id': id});
return data != null;
}
Future<List<Book>> getDeletedBooks() async {
final data = await _post('/api/data/books/deleted');
if (data == null || data['books'] == null) return [];
return (data['books'] as List).map((b) => Book.fromJson(b as Map<String, dynamic>)).toList();
}
Future<bool> restoreBook(String id) async {
final data = await _post('/api/data/book/restore', {'id': id});
return data != null;
}
Future<bool> permanentDeleteBook(String id) async {
final data = await _post('/api/data/book/permanent_delete', {'id': id});
return data != null;
}
Future<List<Note>> getDeletedNotes() async {
final data = await _post('/api/data/notes/deleted');
if (data == null || data['notes'] == null) return [];
return (data['notes'] as List).map((n) => Note.fromJson(n as Map<String, dynamic>)).toList();
}
Future<bool> restoreNote(String id) async {
final data = await _post('/api/data/note/restore', {'id': id});
return data != null;
}
Future<bool> permanentDeleteNote(String id) async {
final data = await _post('/api/data/note/permanent_delete', {'id': id});
return data != null;
}
// ─── 影评 ────────────────────────────────────────────────────
Future<List<MovieReview>> getMovieReviews(String movieId) async {
final data = await _post('/api/data/movie_reviews', {'movie_id': movieId});
if (data == null || data['reviews'] == null) return [];
return (data['reviews'] as List).map((r) => MovieReview.fromJson(r as Map<String, dynamic>)).toList();
}
Future<List<MovieReview>> getAllMovieReviews() async {
final data = await _post('/api/data/movie_reviews');
if (data == null || data['reviews'] == null) return [];
return (data['reviews'] as List).map((r) => MovieReview.fromJson(r as Map<String, dynamic>)).toList();
}
Future<bool> saveMovieReview(MovieReview review) async {
final data = await _post('/api/data/movie_review/save', {'review': review.toJson()});
return data != null;
}
Future<bool> deleteMovieReview(String id) async {
final data = await _post('/api/data/movie_review/delete', {'id': id});
return data != null;
}
// ─── 海报 ────────────────────────────────────────────────────
Future<List<MoviePoster>> getMoviePosters(String movieId) async {
final data = await _post('/api/data/movie_posters', {'movie_id': movieId});
if (data == null || data['posters'] == null) return [];
return (data['posters'] as List).map((p) => MoviePoster.fromJson(p as Map<String, dynamic>)).toList();
}
Future<List<MoviePoster>> getAllMoviePosters() async {
final data = await _post('/api/data/movie_posters');
if (data == null || data['posters'] == null) return [];
return (data['posters'] as List).map((p) => MoviePoster.fromJson(p as Map<String, dynamic>)).toList();
}
Future<bool> saveMoviePoster(MoviePoster poster) async {
final data = await _post('/api/data/movie_poster/save', {'poster': poster.toJson()});
return data != null;
}
Future<bool> deleteMoviePoster(String id) async {
final data = await _post('/api/data/movie_poster/delete', {'id': id});
return data != null;
}
// ─── 书评 ────────────────────────────────────────────────────
Future<List<BookReview>> getBookReviews(String bookId) async {
final data = await _post('/api/data/book_reviews', {'book_id': bookId});
if (data == null || data['reviews'] == null) return [];
return (data['reviews'] as List).map((r) => BookReview.fromJson(r as Map<String, dynamic>)).toList();
}
Future<List<BookReview>> getAllBookReviews() async {
final data = await _post('/api/data/book_reviews');
if (data == null || data['reviews'] == null) return [];
return (data['reviews'] as List).map((r) => BookReview.fromJson(r as Map<String, dynamic>)).toList();
}
Future<bool> saveBookReview(BookReview review) async {
final data = await _post('/api/data/book_review/save', {'review': review.toJson()});
return data != null;
}
Future<bool> deleteBookReview(String id) async {
final data = await _post('/api/data/book_review/delete', {'id': id});
return data != null;
}
// ─── 书摘 ────────────────────────────────────────────────────
Future<List<BookExcerpt>> getBookExcerpts(String bookId) async {
final data = await _post('/api/data/book_excerpts', {'book_id': bookId});
if (data == null || data['excerpts'] == null) return [];
return (data['excerpts'] as List).map((e) => BookExcerpt.fromJson(e as Map<String, dynamic>)).toList();
}
Future<List<BookExcerpt>> getAllBookExcerpts() async {
final data = await _post('/api/data/book_excerpts');
if (data == null || data['excerpts'] == null) return [];
return (data['excerpts'] as List).map((e) => BookExcerpt.fromJson(e as Map<String, dynamic>)).toList();
}
Future<bool> saveBookExcerpt(BookExcerpt excerpt) async {
final data = await _post('/api/data/book_excerpt/save', {'excerpt': excerpt.toJson()});
return data != null;
}
Future<bool> deleteBookExcerpt(String id) async {
final data = await _post('/api/data/book_excerpt/delete', {'id': id});
return data != null;
}
// ─── 批量同步 ────────────────────────────────────────────────
Future<Map<String, int>> batchSync({
List<Movie>? movies,
List<Book>? books,
List<Note>? notes,
List<Map<String, dynamic>>? tags,
List<Map<String, dynamic>>? movieReviews,
List<Map<String, dynamic>>? moviePosters,
List<Map<String, dynamic>>? bookReviews,
List<Map<String, dynamic>>? bookExcerpts,
}) async {
final data = await _post('/api/data/batch_sync', {
if (movies != null) 'movies': movies.map((m) => m.toJson()).toList(),
if (books != null) 'books': books.map((b) => b.toJson()).toList(),
if (notes != null) 'notes': notes.map((n) => n.toJson()).toList(),
if (tags != null) 'tags': tags,
if (movieReviews != null) 'movie_reviews': movieReviews,
if (moviePosters != null) 'movie_posters': moviePosters,
if (bookReviews != null) 'book_reviews': bookReviews,
if (bookExcerpts != null) 'book_excerpts': bookExcerpts,
});
if (data == null) return {};
return {
'movies': (data['movies'] as int?) ?? 0,
'books': (data['books'] as int?) ?? 0,
'notes': (data['notes'] as int?) ?? 0,
'tags': (data['tags'] as int?) ?? 0,
'movie_reviews': (data['movie_reviews'] as int?) ?? 0,
'movie_posters': (data['movie_posters'] as int?) ?? 0,
'book_reviews': (data['book_reviews'] as int?) ?? 0,
'book_excerpts': (data['book_excerpts'] as int?) ?? 0,
};
}
// ─── 标签 ────────────────────────────────────────────────────
Future<List<Map<String, dynamic>>> 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<String, dynamic>.from(t as Map)).toList();
}
Future<bool> saveTag(String name, String type) async {
final data = await _post('/api/data/tag/save', {'tag': {'name': name, 'type': type}});
return data != null;
}
Future<bool> deleteTag(String id) async {
final data = await _post('/api/data/tag/delete', {'id': id});
return data != null;
}
Future<bool> deleteTagByName(String name, String type) async {
final data = await _post('/api/data/tag/delete_by_name', {'name': name, 'type': type});
return data != null;
}
// ─── 图片 ────────────────────────────────────────────────────
/// 是否激活AppProvider 也会用这个检查)
static bool get isActive {
final p = UserPrefs();
return p.syncEnabled && p.syncServerUrl.isNotEmpty && p.syncActivationCode.isNotEmpty;
}
/// 将本地路径转为服务端图片 URL
static Future<String> 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<void> uploadLocalImages(List<String> filePaths) async {
debugPrint('[Sync] uploadLocalImages: isActive=$isActive count=${filePaths.length}');
if (!isActive || filePaths.isEmpty) return;
debugPrint('[Sync] 调用 uploadImages...');
final result = await instance.uploadImages(filePaths);
debugPrint('[Sync] uploadImages 返回: ${result.length} 个文件');
}
/// 上传单张图片到服务端
static Future<void> 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<List<String>> uploadImages(List<String> filePaths) async {
debugPrint('[Sync] uploadImages: 准备上传 ${filePaths.length} 个文件到 $_baseUrl/api/data/image/upload');
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);
final exists = await file.exists();
final size = exists ? await file.length() : 0;
debugPrint('[Sync] 图片: $relPath (存在=$exists 大小=$size)');
if (exists) {
final bytes = await file.readAsBytes();
request.files.add(http.MultipartFile.fromBytes(
'images', bytes,
filename: relPath,
));
}
}
debugPrint('[Sync] 发送 upload 请求 (${request.files.length} 个文件)...');
final resp = await request.send().timeout(const Duration(seconds: 60));
debugPrint('[Sync] upload 响应: ${resp.statusCode}');
if (resp.statusCode != 200) return [];
final body = await resp.stream.bytesToString();
debugPrint('[Sync] upload 响应体: $body');
final data = jsonDecode(body) as Map<String, dynamic>;
return (data['files'] as List?)?.cast<String>() ?? [];
}
Future<String?> uploadImage(String filePath) async {
final files = await uploadImages([filePath]);
return files.isNotEmpty ? files.first : null;
}
}

View File

@@ -1,445 +0,0 @@
import 'dart:async';
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 'package:sqflite/sqflite.dart';
import '../user_prefs.dart';
import '../database_helper.dart';
import '../../models/data_models.dart';
import '../movie/movie_dao.dart';
import '../book/book_dao.dart';
import '../note/note_dao.dart';
import 'server_data_service.dart';
/// 服务端实时同步服务
/// - 开启时:智能合并本地与服务端数据
/// - 关闭时:从服务器下载数据到本地
class ServerSyncService {
static final ServerSyncService instance = ServerSyncService._();
ServerSyncService._();
final UserPrefs _prefs = UserPrefs();
bool _isSyncing = false;
bool get isConfigured {
return _prefs.syncServerUrl.isNotEmpty && _prefs.syncActivationCode.isNotEmpty;
}
Future<Map<String, dynamic>?> checkActivation() async {
final url = _prefs.syncServerUrl;
final code = _prefs.syncActivationCode;
final deviceId = _prefs.deviceId;
if (url.isEmpty || code.isEmpty || deviceId.isEmpty) return null;
try {
final resp = await http.post(
Uri.parse('$url/api/activate'),
headers: {'Content-Type': 'application/json'},
body: '{"code":"$code","device_id":"$deviceId"}',
).timeout(const Duration(seconds: 5));
return resp.statusCode == 200
? _jsonDecode(resp.body)
: {'valid': false, 'error': '激活码无效'};
} catch (_) {
return {'valid': false, 'error': '无法连接服务器'};
}
}
Map<String, dynamic>? _jsonDecode(String s) {
try { final d = jsonDecode(s); return d is Map<String, dynamic> ? d : null; } catch (_) { return null; }
}
/// 开启同步:智能合并本地与服务端数据
Future<bool> syncWithServer() async {
if (!isConfigured || _isSyncing) {
debugPrint('[Sync] 跳过: configured=$isConfigured syncing=$_isSyncing');
return false;
}
_isSyncing = true;
try {
debugPrint('[Sync] ========== 开始同步 ==========');
final server = ServerDataService.instance;
// 读取本地数据
final localMovies = await MovieDao().getAllMovies();
final localBooks = await BookDao().getAllBooks();
final localNotes = await NoteDao().getAllNotes();
final db = await DatabaseHelper.instance.database;
final localMovieReviews = await db.query('movie_reviews');
final localMoviePosters = await db.query('movie_posters');
final localBookReviews = await db.query('book_reviews');
final localBookExcerpts = await db.query('book_excerpts');
debugPrint('[Sync] 本地: 影视${localMovies.length} 书籍${localBooks.length} 笔记${localNotes.length} '
'影评${localMovieReviews.length} 海报${localMoviePosters.length} '
'书评${localBookReviews.length} 书摘${localBookExcerpts.length}');
// 读取服务端数据
final remoteMovies = await server.getMovies();
final remoteBooks = await server.getBooks();
final remoteNotes = await server.getNotes();
debugPrint('[Sync] 服务端: 影视${remoteMovies.length} 书籍${remoteBooks.length} 笔记${remoteNotes.length}');
// 需要 push 到服务端的数据
final pushMovies = <Movie>[];
final pushBooks = <Book>[];
final pushNotes = <Note>[];
final pushMovieReviews = <Map<String, dynamic>>[];
final pushMoviePosters = <Map<String, dynamic>>[];
final pushBookReviews = <Map<String, dynamic>>[];
final pushBookExcerpts = <Map<String, dynamic>>[];
// 服务端无数据 → 全量 push
if (remoteMovies.isEmpty && remoteBooks.isEmpty && remoteNotes.isEmpty) {
pushMovies.addAll(localMovies);
pushBooks.addAll(localBooks);
pushNotes.addAll(localNotes);
pushMovieReviews.addAll(localMovieReviews);
pushMoviePosters.addAll(localMoviePosters);
pushBookReviews.addAll(localBookReviews);
pushBookExcerpts.addAll(localBookExcerpts);
} else {
// 按 updated_at 合并
final remoteMovieMap = {for (final m in remoteMovies) m.id: m};
for (final m in localMovies) {
final r = remoteMovieMap.remove(m.id);
if (r == null || m.updatedAt.isAfter(r.updatedAt)) {
pushMovies.add(m);
} else {
await _upsertLocalMovie(m: r);
}
}
for (final r in remoteMovieMap.values) {
await _upsertLocalMovie(m: r);
}
final remoteBookMap = {for (final b in remoteBooks) b.id: b};
for (final b in localBooks) {
final r = remoteBookMap.remove(b.id);
if (r == null || b.updatedAt.isAfter(r.updatedAt)) {
pushBooks.add(b);
} else {
await _upsertLocalBook(b: r);
}
}
for (final r in remoteBookMap.values) {
await _upsertLocalBook(b: r);
}
final remoteNoteMap = {for (final n in remoteNotes) n.id: n};
for (final n in localNotes) {
final r = remoteNoteMap.remove(n.id);
if (r == null || n.updatedAt.isAfter(r.updatedAt)) {
pushNotes.add(n);
} else {
await _upsertLocalNote(n: r);
}
}
for (final r in remoteNoteMap.values) {
await _upsertLocalNote(n: r);
}
// 子表合并movie_reviews / movie_posters / book_reviews / book_excerpts
await _mergeSubTable(db, server, 'movie_reviews', localMovieReviews, pushMovieReviews);
await _mergeSubTable(db, server, 'movie_posters', localMoviePosters, pushMoviePosters);
await _mergeSubTable(db, server, 'book_reviews', localBookReviews, pushBookReviews);
await _mergeSubTable(db, server, 'book_excerpts', localBookExcerpts, pushBookExcerpts);
}
// 收集所有本地图片路径
final allImagePaths = _collectAllLocalImages(
localMovies, localBooks, localNotes, localMoviePosters);
debugPrint('[Sync] 全部图片: ${allImagePaths.length}');
// 先下载本地缺失的图片
final missingImages = <String>[];
final existingImages = <String>[];
for (final p in allImagePaths) {
if (await File(p).exists()) {
existingImages.add(p);
} else {
missingImages.add(p);
}
}
debugPrint('[Sync] 缺失${missingImages.length}张 现有${existingImages.length}');
if (missingImages.isNotEmpty) {
debugPrint('[Sync] 下载缺失图片...');
final appDir = (await getApplicationDocumentsDirectory()).path;
for (final path in missingImages) {
try {
final relPath = p.relative(path, from: appDir).replaceAll('\\', '/');
final url = '${_prefs.syncServerUrl}/api/data/image/${_prefs.syncActivationCode}/$relPath';
final resp = await http.get(Uri.parse(url)).timeout(const Duration(seconds: 15));
if (resp.statusCode == 200) {
final dest = File(path);
await dest.parent.create(recursive: true);
await dest.writeAsBytes(resp.bodyBytes);
}
} catch (_) {}
}
debugPrint('[Sync] 缺失图片下载完成');
}
// 上传本地已有图片
if (existingImages.isNotEmpty) {
debugPrint('[Sync] 上传 ${existingImages.length} 张现有图片...');
await ServerDataService.uploadLocalImages(existingImages);
}
// 批量 push 数据到服务端
final hasPush = pushMovies.isNotEmpty || pushBooks.isNotEmpty || pushNotes.isNotEmpty ||
pushMovieReviews.isNotEmpty || pushMoviePosters.isNotEmpty ||
pushBookReviews.isNotEmpty || pushBookExcerpts.isNotEmpty;
debugPrint('[Sync] hasPush=$hasPush');
if (hasPush) {
final localTags = await db.query('tags');
final result = await server.batchSync(
movies: pushMovies.isEmpty ? null : pushMovies,
books: pushBooks.isEmpty ? null : pushBooks,
notes: pushNotes.isEmpty ? null : pushNotes,
tags: localTags.isEmpty ? null : localTags.cast<Map<String, dynamic>>(),
movieReviews: pushMovieReviews.isEmpty ? null : pushMovieReviews,
moviePosters: pushMoviePosters.isEmpty ? null : pushMoviePosters,
bookReviews: pushBookReviews.isEmpty ? null : pushBookReviews,
bookExcerpts: pushBookExcerpts.isEmpty ? null : pushBookExcerpts,
);
debugPrint('[Sync] batchSync 结果: $result');
}
debugPrint('[Sync] 合并完成');
return true;
} catch (e) {
debugPrint('[Sync] 合并异常: $e');
return false;
} finally {
_isSyncing = false;
}
}
// ─── 合并辅助方法 ────────────────────────────────────────────
Future<void> _upsertLocalMovie({required Movie m}) async {
final db = await DatabaseHelper.instance.database;
await db.insert('movies', m.toJson(), conflictAlgorithm: ConflictAlgorithm.replace);
}
Future<void> _upsertLocalBook({required Book b}) async {
final db = await DatabaseHelper.instance.database;
await db.insert('books', b.toJson(), conflictAlgorithm: ConflictAlgorithm.replace);
}
Future<void> _upsertLocalNote({required Note n}) async {
final db = await DatabaseHelper.instance.database;
await db.insert('notes', n.toJson(), conflictAlgorithm: ConflictAlgorithm.replace);
}
/// 收集所有实际存在的本地图片路径
List<String> _collectAllLocalImages(List<Movie> movies, List<Book> books,
List<Note> notes, List<Map<String, dynamic>> posters) {
final paths = <String>[];
for (final m in movies) {
if (m.posterPath != null && m.posterPath!.isNotEmpty) paths.add(m.posterPath!);
}
for (final b in books) {
if (b.coverPath != null && b.coverPath!.isNotEmpty) paths.add(b.coverPath!);
}
for (final n in notes) {
paths.addAll(n.images.where((i) => i.isNotEmpty));
}
for (final p in posters) {
final pp = p['poster_path'] as String?;
if (pp != null && pp.isNotEmpty) paths.add(pp);
}
return paths;
}
/// 合并子表reviews/posters/excerpts本地优先 push服务端补充
Future<void> _mergeSubTable(Database db, ServerDataService server, String table,
List<Map<String, dynamic>> local, List<Map<String, dynamic>> pushList) async {
// 尝试获取服务端数据
List<Map<String, dynamic>> remote = [];
bool serverOk = false;
try {
switch (table) {
case 'movie_reviews':
remote = (await server.getAllMovieReviews()).map((r) => r.toJson()).toList();
case 'movie_posters':
remote = (await server.getAllMoviePosters()).map((p) => p.toJson()).toList();
case 'book_reviews':
remote = (await server.getAllBookReviews()).map((r) => r.toJson()).toList();
case 'book_excerpts':
remote = (await server.getAllBookExcerpts()).map((e) => e.toJson()).toList();
}
serverOk = true;
} catch (_) {}
if (!serverOk) {
// 服务端不可用 → 全量 push 本地数据
pushList.addAll(local);
return;
}
final remoteMap = {for (final r in remote) r['id'] as String: r};
for (final l in local) {
final r = remoteMap.remove(l['id'] as String);
if (r == null) {
pushList.add(l);
} else {
final lTime = l['updated_at'] as String? ?? '';
final rTime = r['updated_at'] as String? ?? '';
if (lTime.compareTo(rTime) > 0) pushList.add(l);
}
}
// 服务端有、本地无 → 写入本地
for (final r in remoteMap.values) {
await db.insert(table, r, conflictAlgorithm: ConflictAlgorithm.replace);
}
}
/// 关闭同步:上传完整备份到服务器后切回本地
Future<bool> uploadBackupAndDisconnect() async {
if (!isConfigured || _isSyncing) return false;
_isSyncing = true;
try {
debugPrint('[Sync] ========== 关闭同步:上传备份 ==========');
final url = _prefs.syncServerUrl;
final code = _prefs.syncActivationCode;
final deviceId = _prefs.deviceId;
final appDir = (await getApplicationDocumentsDirectory()).path;
// 上传数据库(先关闭连接再读取,避免文件被占用)
await DatabaseHelper.instance.close();
final dbPath = await DatabaseHelper.instance.databasePath;
final request = http.MultipartRequest('POST', Uri.parse('$url/api/sync/upload'));
request.fields['code'] = code;
request.fields['device_id'] = deviceId;
if (dbPath != null && File(dbPath).existsSync()) {
final dbBytes = await File(dbPath).readAsBytes();
request.files.add(http.MultipartFile.fromBytes(
'database', dbBytes,
filename: 'mooknote.db',
));
debugPrint('[Sync] 上传数据库: ${dbBytes.length} bytes');
}
// 重新打开数据库
await DatabaseHelper.instance.reopen();
// 收集并上传所有图片
final imagePaths = _collectAllImageFiles(appDir);
debugPrint('[Sync] 上传 ${imagePaths.length} 张图片...');
for (final path in imagePaths) {
final relPath = p.relative(path, from: appDir).replaceAll('\\', '/');
if (await File(path).exists()) {
final bytes = await File(path).readAsBytes();
request.files.add(http.MultipartFile.fromBytes(
'images', bytes,
filename: relPath,
));
}
}
final resp = await request.send().timeout(const Duration(seconds: 300));
final body = await resp.stream.bytesToString();
debugPrint('[Sync] 上传备份响应: ${resp.statusCode} $body');
if (resp.statusCode == 200) {
debugPrint('[Sync] 备份上传成功,关闭同步');
}
return resp.statusCode == 200;
} catch (e) {
debugPrint('[Sync] 上传备份异常: $e');
return false;
} finally {
_isSyncing = false;
}
}
/// 收集所有图片文件(递归扫描 images 目录)
List<String> _collectAllImageFiles(String appDir) {
final paths = <String>[];
final imagesDir = Directory(p.join(appDir, 'images'));
if (!imagesDir.existsSync()) return paths;
for (final entity in imagesDir.listSync(recursive: true)) {
if (entity is File) {
paths.add(entity.path);
}
}
return paths;
}
Future<bool> downloadToLocal() async {
if (!isConfigured || _isSyncing) {
debugPrint('[Sync] downloadToLocal 跳过: configured=$isConfigured syncing=$_isSyncing');
return false;
}
_isSyncing = true;
try {
debugPrint('[Sync] ========== 关闭同步:从服务器下载 ==========');
final url = _prefs.syncServerUrl;
final code = _prefs.syncActivationCode;
final deviceId = _prefs.deviceId;
debugPrint('[Sync] 查询备份信息...');
final infoResp = await http.post(
Uri.parse('$url/api/sync/info'),
headers: {'Content-Type': 'application/json'},
body: '{"code":"$code","device_id":"$deviceId"}',
).timeout(const Duration(seconds: 15));
debugPrint('[Sync] /api/sync/info 响应: ${infoResp.statusCode}');
if (infoResp.statusCode != 200) return false;
final info = _jsonDecode(infoResp.body);
debugPrint('[Sync] info: $info');
if (info == null || info['has_backup'] != true) {
debugPrint('[Sync] 服务器无备份,跳过下载');
return false;
}
debugPrint('[Sync] 下载数据库...');
final dbResp = await http.post(
Uri.parse('$url/api/sync/download/database'),
headers: {'Content-Type': 'application/json'},
body: '{"code":"$code"}',
).timeout(const Duration(seconds: 120));
debugPrint('[Sync] 数据库下载响应: ${dbResp.statusCode} size=${dbResp.bodyBytes.length}');
if (dbResp.statusCode != 200) return false;
await DatabaseHelper.instance.reopenDatabaseFromBytes(dbResp.bodyBytes);
debugPrint('[Sync] 数据库已重写并重新打开');
final images = (info['images'] as List<dynamic>?)
?.map((e) => e is Map ? {'name': e['name'] as String, 'rel_path': e['rel_path'] as String} : null)
.where((e) => e != null).cast<Map<String, String>>().toList() ?? [];
debugPrint('[Sync] 下载 ${images.length} 张图片...');
final appDir = await getApplicationDocumentsDirectory();
int downloaded = 0;
for (final img in images) {
try {
final relPath = img['rel_path']!.replaceAll('\\', '/');
final imgResp = await http.get(
Uri.parse('$url/api/sync/download/image/$code/$relPath'),
).timeout(const Duration(seconds: 30));
if (imgResp.statusCode == 200) {
final dest = File(p.join(appDir.path, relPath));
await dest.parent.create(recursive: true);
await dest.writeAsBytes(imgResp.bodyBytes);
downloaded++;
}
} catch (_) {}
}
debugPrint('[Sync] 下载完成: 数据库 + $downloaded/${images.length} 张图片');
debugPrint('[Sync] 下载到本地完成');
return true;
} catch (e) {
debugPrint('[Sync] 下载到本地异常: $e');
return false;
} finally {
_isSyncing = false;
}
}
}