重构路径规划生成接口使其完全符合 Clean Architecture 规范

This commit is contained in:
2026-02-28 17:18:27 +08:00
parent bcaebade9a
commit 7ff79ee0b5
4 changed files with 73 additions and 43 deletions

View File

@@ -0,0 +1,48 @@
import 'dart:convert';
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 {
final UserStorage _userStorage;
PathHttpDatasourceImpl(this._userStorage);
@override
Future<List<DeviceAddPathPointModel>> generatePathRaw({required Map<String, dynamic> body}) async {
final token = (await _userStorage.getUser())?.token;
if (token == null) throw Exception('Token missing');
final response = await http.post(
Uri.parse('https://servicepathplan.satabot.com/api/path'),
headers: {
'Content-Type': 'application/json',
'Authorization': token,
},
body: jsonEncode(body),
);
print('响应体: ${response.body}');
try {
final decoded = jsonDecode(response.body) as Map<String, dynamic>;
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<dynamic> jsonData = pathJson;
return jsonData.map((item) => DeviceAddPathPointModel.fromJson(item)).toList();
} else {
throw Exception('Failed to load path: ${response.statusCode}');
}
} catch (e) {
throw Exception('Network error: $e');
}
}
}