diff --git a/lib/features/devices/data/repositories/generate_path_repository_Impl.dart b/lib/features/devices/data/repositories/generate_path_repository_Impl.dart index 1943778a..1a24dbb1 100644 --- a/lib/features/devices/data/repositories/generate_path_repository_Impl.dart +++ b/lib/features/devices/data/repositories/generate_path_repository_Impl.dart @@ -3,12 +3,18 @@ import 'dart:math'; import 'package:http/http.dart' as http; import 'dart:convert'; +import '../../../../core/di/injection.dart'; +import '../../../../core/storage/user_storage.dart'; import '../../domain/repositories/path_repository.dart'; import '../models/device_add_path_point_model.dart'; import '../models/device_work_area_param_model.dart'; class PathRepositoryImpl implements PathRepository { - final String baseUrl = 'https://serviceri.satabot.com'; // 后端接口地址 + /* final String baseUrl = 'https://serviceri.satabot.com'; // 后端接口地址*/ + /* final UserStorage localTokenStorage; + PathRepositoryImpl({ + required this.localTokenStorage, // 👈 关键:注入依赖 + });*/ // 生成路径 @override Future> generatePath({ @@ -17,9 +23,20 @@ class PathRepositoryImpl implements PathRepository { required OuterBoundary outer, required List holes, required int workType, + }) async { - final url = Uri.parse('$baseUrl/api/path'); - final headers = {'Content-Type': 'application/json'}; + final url = Uri.parse('https://servicepathplan.satabot.com/api/path'); + // ✅ 直接从 GetIt 获取 UserStorage(无需构造函数传参) + final userStorage = sl(); + final userEntity = await userStorage.getUser(); + final String? token = userEntity?.token; + if (token == null) throw Exception('请先登录'); + + // ✅ 设置带 token 的 headers + final headers = { + 'Content-Type': 'application/json', + 'Authorization': token, + }; final body = jsonEncode({ 'reference': reference.toJson(), @@ -32,11 +49,18 @@ class PathRepositoryImpl implements PathRepository { try { final response = await http.post(url, headers: headers, body: body); - if (response.statusCode == 200) { - final List jsonData = jsonDecode(response.body)['path']; - return jsonData - .map((item) => DeviceAddPathPointModel.fromJson(item)) - .toList(); + print('响应体: ${response.body}'); + final decoded = jsonDecode(response.body) as Map; + if (decoded['code'] == 200) { + final pathJson = decoded['path']; + 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 { throw Exception('Failed to load path: ${response.statusCode}'); }