空洞逻辑

This commit is contained in:
mmc
2026-03-02 13:25:21 +08:00
parent 991c4fe174
commit fe9b763783
4 changed files with 300 additions and 168 deletions

View File

@@ -15,6 +15,33 @@ class PathHttpDatasourceImpl implements PathHttpDatasource {
@override
Future<List<DeviceAddPathPointModel>> generatePathRaw({required Map<String, dynamic> body}) async {
try {
// 1. 先打印基础信息
print('=== 最终发送给后端的请求体 ===');
print('请求体类型:${body.runtimeType}');
print('是否包含holes:${body.containsKey('holes')}, holes长度:${(body['holes'] as List).length}');
// 2. 格式化打印JSON(带缩进,清晰展示嵌套结构)
final jsonString = const JsonEncoder.withIndent(' ').convert(body);
print('完整JSON请求体:\n$jsonString');
// 3. 可选:单独打印holes的JSON(重点关注)
final holesJson = body['holes'] as List;
print('=== 单独打印holes的JSON ===');
if (holesJson.isEmpty) {
print('holes 为空数组');
} else {
for (int i = 0; i < holesJson.length; i++) {
print('第${i + 1}组hole:\n${const JsonEncoder.withIndent(' ').convert(holesJson[i])}');
}
}
} catch (e) {
// 防止JSON序列化失败导致崩溃
print('打印请求体失败:$e');
// 降级打印原始body(虽然格式乱,但能看基础数据)
print('原始body数据:$body');
}
final token = (await _userStorage.getUser())?.token;
if (token == null) throw Exception('Token missing');
@@ -23,7 +50,6 @@ class PathHttpDatasourceImpl implements PathHttpDatasource {
headers: {'Content-Type': 'application/json', 'Authorization': token},
body: jsonEncode(body),
);
//debugPrint('请求体: ${jsonEncode(body)}');
// 🔥 完整打印响应体
//printFullString(formatJson(jsonEncode(response.body)));

View File

@@ -12,9 +12,7 @@ import '../models/device_work_area_param_model.dart';
class PathRepositoryImpl implements PathRepository {
final PathHttpDatasource _datasource;
PathRepositoryImpl({
required PathHttpDatasource datasource,
}) : _datasource = datasource;
PathRepositoryImpl({required PathHttpDatasource datasource}) : _datasource = datasource;
// 生成路径
@override
Future<List<DeviceAddPathPointModel>> generatePath({
@@ -28,11 +26,11 @@ class PathRepositoryImpl implements PathRepository {
'reference': reference.toJson(),
'heading': heading,
'outer': outer.toJson(),
'holes': holes.map((h) => h.toJson()).toList(),
'holes': holes.map((hole) => hole.toJson()).toList(),
'workType': workType,
};
return await _datasource.generatePathRaw(body: body);
return await _datasource.generatePathRaw(body: body);
}
// 保存工作记录

View File

@@ -37,17 +37,11 @@ class DevicesCubit extends Cubit<DevicesState> {
this._updateDevicename,
this._selectWorkRecordUseCase,
this._saveWorkRecordUseCase,
this._generatePathUseCase
this._generatePathUseCase,
) : super(const DevicesState());
Future<void> unbindDevice(String deviceId, String deviceName) async {
emit(
state.copyWith(
isLoading: true,
errorMessage: '',
operationType: DeviceOperationType.unbind,
),
);
emit(state.copyWith(isLoading: true, errorMessage: '', operationType: DeviceOperationType.unbind));
try {
final params = UnbindDeviceParams(deviceId, deviceName);
@@ -73,93 +67,54 @@ class DevicesCubit extends Cubit<DevicesState> {
state.copyWith(
isLoading: false,
devices: updatedDevices,
selectedDevice: state.selectedDevice?.deviceName == deviceId
? null
: state.selectedDevice,
selectedDevice: state.selectedDevice?.deviceName == deviceId ? null : state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑失败:状态码 $successCode',
operationType: DeviceOperationType.none,
),
);
emit(state.copyWith(isLoading: false, errorMessage: '解绑失败:状态码 $successCode', operationType: DeviceOperationType.none));
}
},
);
} catch (e) {
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑异常:${e.toString()}',
operationType: DeviceOperationType.none,
),
);
emit(state.copyWith(isLoading: false, errorMessage: '解绑异常:${e.toString()}', operationType: DeviceOperationType.none));
}
}
Future<void> updateDeviceName(String deviceId, String deviceName) async {
emit(
state.copyWith(
isLoading: true,
errorMessage: '',
operationType: DeviceOperationType.updateName,
),
);
emit(state.copyWith(isLoading: true, errorMessage: '', operationType: DeviceOperationType.updateName));
try {
final params = UpdateDevicenameParams(deviceId, deviceName);
final result = await _updateDevicename.call(params);
result.fold(
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '更新设备名称失败',
operationType: DeviceOperationType.none,
),
),
(successCode) {
// 核心修复:int 转 bool 条件判断
print('更新设备名称结果代码: $successCode'); // 调试输出结果代码
final isSuccess = successCode == 1; // 显式转为 bool
if (isSuccess) {
//final updatedDevices = state.devices?.map((device) {
// return device.deviceName == deviceId ? device.copyWith(deviceName: deviceName) : device;
//}).toList();
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '更新设备名称失败', operationType: DeviceOperationType.none)), (
successCode,
) {
// 核心修复:int 转 bool 条件判断
print('更新设备名称结果代码: $successCode'); // 调试输出结果代码
final isSuccess = successCode == 1; // 显式转为 bool
if (isSuccess) {
//final updatedDevices = state.devices?.map((device) {
// return device.deviceName == deviceId ? device.copyWith(deviceName: deviceName) : device;
//}).toList();
emit(
state.copyWith(
isLoading: false,
//devices: updatedDevices,
//selectedDevice: state.selectedDevice?.deviceName == deviceId ? state.selectedDevice?.copyWith(deviceName: deviceName) : state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(
state.copyWith(
isLoading: false,
errorMessage: '更新设备名称失败:状态码 $successCode',
operationType: DeviceOperationType.none,
),
);
}
},
);
emit(
state.copyWith(
isLoading: false,
//devices: updatedDevices,
//selectedDevice: state.selectedDevice?.deviceName == deviceId ? state.selectedDevice?.copyWith(deviceName: deviceName) : state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(state.copyWith(isLoading: false, errorMessage: '更新设备名称失败:状态码 $successCode', operationType: DeviceOperationType.none));
}
});
} catch (e) {
emit(
state.copyWith(
isLoading: false,
errorMessage: '更新设备名称异常:${e.toString()}',
operationType: DeviceOperationType.none,
),
);
emit(state.copyWith(isLoading: false, errorMessage: '更新设备名称异常:${e.toString()}', operationType: DeviceOperationType.none));
}
}
@@ -168,9 +123,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
try {
// 网络请求获取列表
var resultEither = await _getUserDeviceUseCase.call(
GetUserDeviceParams(username),
);
var resultEither = await _getUserDeviceUseCase.call(GetUserDeviceParams(username));
resultEither.fold(
(failure) => emit(
@@ -180,13 +133,7 @@ class DevicesCubit extends Cubit<DevicesState> {
),
),
(deviceList) {
emit(
state.copyWith(
devices: deviceList,
selectedDevice: deviceList.isNotEmpty ? deviceList.first : null,
isLoading: false,
),
);
emit(state.copyWith(devices: deviceList, selectedDevice: deviceList.isNotEmpty ? deviceList.first : null, isLoading: false));
},
);
} catch (e) {
@@ -206,10 +153,7 @@ class DevicesCubit extends Cubit<DevicesState> {
}).toList();
// 如果更新的是当前选中的设备,也要同步更新 selectedDevice
final newSelected =
state.selectedDevice?.deviceName == updatedDevice.deviceName
? updatedDevice
: state.selectedDevice;
final newSelected = state.selectedDevice?.deviceName == updatedDevice.deviceName ? updatedDevice : state.selectedDevice;
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
}
@@ -232,15 +176,8 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
final result = await _getDeviceLocationUseCase.call(device.deviceName);
result.fold(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(location) => emit(
state.copyWith(
isLoading: false,
deviceLatitude: location.latitude,
deviceLongitude: location.longitude,
),
),
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(location) => emit(state.copyWith(isLoading: false, deviceLatitude: location.latitude, deviceLongitude: location.longitude)),
);
}
@@ -249,8 +186,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
final result = await _getWorkRecordUseCase(userId);
result.fold(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(records) => emit(state.copyWith(isLoading: false, workRecords: records)),
);
}
@@ -260,16 +196,8 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
final result = await _deleteWorkRecordUseCase(workName);
result.fold(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(_) => emit(
state.copyWith(
isLoading: false,
workRecords: state.workRecords
?.where((record) => record != workName)
.toList(),
),
),
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(_) => emit(state.copyWith(isLoading: false, workRecords: state.workRecords?.where((record) => record != workName).toList())),
);
}
@@ -279,28 +207,21 @@ class DevicesCubit extends Cubit<DevicesState> {
try {
final result = await _selectWorkRecordUseCase.call(workName);
result.fold(
(failure) => emit(
state.copyWith(isLoading: false, errorMessage: failure.message),
),
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(data) => emit(state.copyWith(isLoading: false, pathData: data)),
);
} catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
}
}
/// 保存路径数据
Future<void> saveWorkRecord(String workName, String userId, String jsonData) async {
emit(state.copyWith(isLoading: true));
final result = await _saveWorkRecordUseCase.call(
workName: workName,
userId: userId,
jsonData: jsonData,
);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(data) => emit(state.copyWith(isLoading: false)),
);
final result = await _saveWorkRecordUseCase.call(workName: workName, userId: userId, jsonData: jsonData);
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)), (data) => emit(state.copyWith(isLoading: false)));
}
/// generatePath
Future<void> generatePath({
required ReferencePoint reference,
@@ -309,22 +230,13 @@ class DevicesCubit extends Cubit<DevicesState> {
required List<HoleBoundary> holes,
required int workType,
}) async {
holes.asMap().forEach((index, hole) {
final pointsStr = hole.position.map((p) => "(${p.lat.toStringAsFixed(6)}, ${p.lon.toStringAsFixed(6)})").join(', ');
debugPrint('第${index + 1}组holes:$pointsStr');
});
emit(state.copyWith(isLoading: true));
final result = await _generatePathUseCase.execute(
reference: reference,
heading: heading,
outer: outer,
holes: holes,
workType: workType,
);
result.fold(
(failure) => emit(state.copyWith(errorMessage: failure.message)),
(pathData) => emit(state.copyWith(generatedPath: pathData)),
);
final result = await _generatePathUseCase.execute(reference: reference, heading: heading, outer: outer, holes: holes, workType: workType);
result.fold((failure) => emit(state.copyWith(errorMessage: failure.message)), (pathData) => emit(state.copyWith(generatedPath: pathData)));
}
}