From e7f579b423867a4212fdc5f280a2541458f842e0 Mon Sep 17 00:00:00 2001 From: Songzex <2402265378@qq.com> Date: Sat, 28 Feb 2026 16:05:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=AF=E5=BE=84=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=8E=A5=E5=8F=A3=E5=9F=9F=E5=90=8D+token=E6=90=BA?= =?UTF-8?q?=E5=B8=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generate_path_repository_Impl.dart | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) 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 c8b156ba..365a1254 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}'); }