import 'package:get_storage/get_storage.dart'; import '../../data/services/network/api.dart'; class Synchrodata { static final GetStorage _storage = GetStorage(); static List _records = _storage.read("operation_records") ?? []; static List _delids = _storage.read("operation_delids") ?? []; static bool _isSynchro = false; // 保存操作记录 static Future saveOperationRecord( String method, dynamic params, ) async { String userId = ''; Map? userInfo = _storage.read("user_info"); if (userInfo != null) { userId = userInfo['user']['uid']; } String id = '${userId}_${params['id']}'; if (params['type'] != null) { params['rtype'] = params['type']; } switch (method) { case 'insert': _records.add({ ...params, 'id': id, 'number': params['id'], 'uid': userId, }); _storage.write("operation_records", _records); break; case 'update': Map? foundElement = _records.firstWhere( (element) => element['id'] == id, orElse: () => null, ); if (foundElement != null) { foundElement.addAll({ ...params, 'id': id, }); } else { _records.add({ ...params, 'id': id, 'number': params['id'], 'uid': userId, }); } _storage.write("operation_records", _records); break; case 'delete': _delids.add(id); _storage.write("operation_delids", _delids); break; default: break; } } // 获取操作记录 static Future startSynchro() async { if (_isSynchro) return; _isSynchro = true; if (_records.isNotEmpty || _delids.isNotEmpty) { var result = await Api.putOperationRecord({ 'delids': _delids, 'records': _records, }); if (result != null) { _records = []; _delids = []; _storage.write("operation_records", _records); _storage.write("operation_delids", _delids); } } var result = await Api.getOperationRecord(); _isSynchro = false; } }