This commit is contained in:
mmc
2026-03-02 10:19:23 +08:00
parent 41756e1f51
commit b0cf903e8c
4 changed files with 49 additions and 26 deletions

21
lib/common.dart Normal file
View File

@@ -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;
}
}

View File

@@ -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<String, dynamic>;
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<String, dynamic>;
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<dynamic> 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<dynamic> 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');
}
}
}

View File

@@ -744,7 +744,7 @@ packages:
source: hosted
version: "1.0.1"
logger:
dependency: transitive
dependency: "direct main"
description:
name: logger
sha256: a7967e31b703831a893bbc3c3dd11db08126fe5f369b5c648a36f821979f5be3

View File

@@ -52,6 +52,7 @@ dependencies:
bloc: ^9.2.0
image: ^4.1.7 # 用于图片格式转换
http: ^1.1.0
logger: ^2.0.0 # 最新版本可查 pub.dev
# ===== 依赖注入 =====