diff --git a/lib/common.dart b/lib/common.dart new file mode 100644 index 00000000..9a44f21b --- /dev/null +++ b/lib/common.dart @@ -0,0 +1,21 @@ +// 工具方法 +import 'dart:convert'; + +import 'package:flutter/material.dart'; + +void printFullString(String text, {int chunkSize = 800}) { + if (text.isEmpty) return; + for (int i = 0; i < text.length; i += chunkSize) { + int end = (i + chunkSize < text.length) ? i + chunkSize : text.length; + debugPrint('[$i-$end/${text.length}]: ${text.substring(i, end)}'); + } +} + +String formatJson(String jsonStr) { + try { + final jsonObj = jsonDecode(jsonStr); + return const JsonEncoder.withIndent(' ').convert(jsonObj); + } catch (e) { + return jsonStr; + } +} diff --git a/lib/features/devices/data/datasources/impl/path_http_datasource_impl.dart b/lib/features/devices/data/datasources/impl/path_http_datasource_impl.dart index a74cfc54..bda773c1 100644 --- a/lib/features/devices/data/datasources/impl/path_http_datasource_impl.dart +++ b/lib/features/devices/data/datasources/impl/path_http_datasource_impl.dart @@ -1,11 +1,14 @@ import 'dart:convert'; +import 'package:maibu_satabot_v2/common.dart'; + import '../../../../../core/storage/user_storage.dart'; import '../../models/device_add_path_point_model.dart'; import '../path_http_datasource.dart'; import 'package:http/http.dart' as http; -class PathHttpDatasourceImpl implements PathHttpDatasource { +import 'package:logger/logger.dart'; +class PathHttpDatasourceImpl implements PathHttpDatasource { final UserStorage _userStorage; PathHttpDatasourceImpl(this._userStorage); @@ -17,35 +20,33 @@ class PathHttpDatasourceImpl implements PathHttpDatasource { final response = await http.post( Uri.parse('https://servicepathplan.satabot.com/api/path'), - headers: { - 'Content-Type': 'application/json', - 'Authorization': token, - }, + headers: {'Content-Type': 'application/json', 'Authorization': token}, body: jsonEncode(body), ); + //debugPrint('请求体: ${jsonEncode(body)}'); - print('响应体: ${response.body}'); + // 🔥 完整打印响应体 + //printFullString(formatJson(jsonEncode(response.body))); try { - final decoded = jsonDecode(response.body) as Map; - if (decoded['success'] == true) { - final pathJson = decoded['data']?['path']; - print('路径数据: $pathJson'); // 调试输出路径数据 - if (pathJson == null) { - throw Exception('API returned null for "path"'); + final decoded = jsonDecode(response.body) as Map; + if (decoded['success'] == true) { + final pathJson = decoded['data']?['path']; + print('路径数据: $pathJson'); // 调试输出路径数据 + if (pathJson == null) { + throw Exception('API returned null for "path"'); + } + if (pathJson is! List) { + throw Exception('Expected "path" to be a List, but got ${pathJson.runtimeType}'); + } + final List jsonData = pathJson; + return jsonData.map((item) => DeviceAddPathPointModel.fromJson(item)).toList(); + } else { + final apiCode = decoded['code'] ?? -1; + final msg = decoded['msg'] ?? '未知错误'; + throw Exception('API 错误 [$apiCode]: $msg'); } - if (pathJson is! List) { - throw Exception('Expected "path" to be a List, but got ${pathJson.runtimeType}'); - } - final List jsonData = pathJson; - return jsonData.map((item) => DeviceAddPathPointModel.fromJson(item)).toList(); - } else { - final apiCode = decoded['code'] ?? -1; - final msg = decoded['msg'] ?? '未知错误'; - throw Exception('API 错误 [$apiCode]: $msg'); + } catch (e) { + throw Exception('Network error: $e'); } - } catch (e) { - throw Exception('Network error: $e'); - } } } - diff --git a/pubspec.lock b/pubspec.lock index 27d43824..6d50973f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -744,7 +744,7 @@ packages: source: hosted version: "1.0.1" logger: - dependency: transitive + dependency: "direct main" description: name: logger sha256: a7967e31b703831a893bbc3c3dd11db08126fe5f369b5c648a36f821979f5be3 diff --git a/pubspec.yaml b/pubspec.yaml index 273374d3..0c7a5fdb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -52,6 +52,7 @@ dependencies: bloc: ^9.2.0 image: ^4.1.7 # 用于图片格式转换 http: ^1.1.0 + logger: ^2.0.0 # 最新版本可查 pub.dev # ===== 依赖注入 =====