临时
This commit is contained in:
21
lib/common.dart
Normal file
21
lib/common.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:maibu_satabot_v2/common.dart';
|
||||||
|
|
||||||
import '../../../../../core/storage/user_storage.dart';
|
import '../../../../../core/storage/user_storage.dart';
|
||||||
import '../../models/device_add_path_point_model.dart';
|
import '../../models/device_add_path_point_model.dart';
|
||||||
import '../path_http_datasource.dart';
|
import '../path_http_datasource.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
class PathHttpDatasourceImpl implements PathHttpDatasource {
|
import 'package:logger/logger.dart';
|
||||||
|
|
||||||
|
class PathHttpDatasourceImpl implements PathHttpDatasource {
|
||||||
final UserStorage _userStorage;
|
final UserStorage _userStorage;
|
||||||
|
|
||||||
PathHttpDatasourceImpl(this._userStorage);
|
PathHttpDatasourceImpl(this._userStorage);
|
||||||
@@ -17,35 +20,33 @@ class PathHttpDatasourceImpl implements PathHttpDatasource {
|
|||||||
|
|
||||||
final response = await http.post(
|
final response = await http.post(
|
||||||
Uri.parse('https://servicepathplan.satabot.com/api/path'),
|
Uri.parse('https://servicepathplan.satabot.com/api/path'),
|
||||||
headers: {
|
headers: {'Content-Type': 'application/json', 'Authorization': token},
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': token,
|
|
||||||
},
|
|
||||||
body: jsonEncode(body),
|
body: jsonEncode(body),
|
||||||
);
|
);
|
||||||
|
//debugPrint('请求体: ${jsonEncode(body)}');
|
||||||
|
|
||||||
print('响应体: ${response.body}');
|
// 🔥 完整打印响应体
|
||||||
|
//printFullString(formatJson(jsonEncode(response.body)));
|
||||||
try {
|
try {
|
||||||
final decoded = jsonDecode(response.body) as Map<String, dynamic>;
|
final decoded = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
if (decoded['success'] == true) {
|
if (decoded['success'] == true) {
|
||||||
final pathJson = decoded['data']?['path'];
|
final pathJson = decoded['data']?['path'];
|
||||||
print('路径数据: $pathJson'); // 调试输出路径数据
|
print('路径数据: $pathJson'); // 调试输出路径数据
|
||||||
if (pathJson == null) {
|
if (pathJson == null) {
|
||||||
throw Exception('API returned null for "path"');
|
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) {
|
} catch (e) {
|
||||||
throw Exception('Expected "path" to be a List, but got ${pathJson.runtimeType}');
|
throw Exception('Network error: $e');
|
||||||
}
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -744,7 +744,7 @@ packages:
|
|||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.1"
|
version: "1.0.1"
|
||||||
logger:
|
logger:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: logger
|
name: logger
|
||||||
sha256: a7967e31b703831a893bbc3c3dd11db08126fe5f369b5c648a36f821979f5be3
|
sha256: a7967e31b703831a893bbc3c3dd11db08126fe5f369b5c648a36f821979f5be3
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ dependencies:
|
|||||||
bloc: ^9.2.0
|
bloc: ^9.2.0
|
||||||
image: ^4.1.7 # 用于图片格式转换
|
image: ^4.1.7 # 用于图片格式转换
|
||||||
http: ^1.1.0
|
http: ^1.1.0
|
||||||
|
logger: ^2.0.0 # 最新版本可查 pub.dev
|
||||||
|
|
||||||
|
|
||||||
# ===== 依赖注入 =====
|
# ===== 依赖注入 =====
|
||||||
|
|||||||
Reference in New Issue
Block a user