完成扫码绑定设备-初步测试正确完成

This commit is contained in:
2026-03-04 17:36:28 +08:00
parent 786d11c936
commit 1cff8f9ee2
5 changed files with 157 additions and 58 deletions

View File

@@ -26,17 +26,43 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
@override
Future<int> bindDevice(String deviceId, String deviceAlias) async {
final logger = GetIt.I<ILoggerService>() as SentryLoggerImpl;
logger.logWithLevel('用户点击bindDevice方法开始触发', level: 'INFO');
logger.logWithLevel(
'用户点击bindDevice方法API 请求开始',
level: 'INFO',
data: {'url': 'https://serviceri.satabot.com/iot/device/bindDevice'}
);
var response = await dio.post(
HttpApiConsts.bindDevice,
data: {'deviceId': deviceId, 'deviceAlias': deviceAlias},
);
logger.logWithLevel(
'请求详细参数',
level: 'DEBUG',
data: {
'url': 'https://serviceri.satabot.com/iot/device/bindDevice',
'deviceId': deviceId,
'deviceAlias': deviceAlias
}
);
if (response.statusCode != 200) {
logger.logWithLevel(
'API 响应失败',
level: 'ERROR',
data: {'statusCode': response.statusCode, 'data': response.data}
);
throw Exception('网络请求失败:${response.statusCode}');
}
final responseData = response.data;
if (responseData['code'] != 200 || responseData['data'] != true) {
logger.logWithLevel(
'API 响应失败',
level: 'ERROR',
data: {'statusCode': response.statusCode, 'data': response.data}
);
throw Exception(responseData['msg'] ?? '业务异常');
}

View File

@@ -180,16 +180,21 @@ class DevicesCubit extends Cubit<DevicesState> {
}
/// 绑定设备
Future<void> bindDevice(String deviceId, String deviceAlias) async {
emit(state.copyWith(isLoading: true, errorMessage: ''));
try {
final params = BindDeviceParams(deviceId, deviceAlias);
final result = await _bindDeviceUseCase.call(params);
result.fold(
// 失败处理
(failure) => emit(state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '绑定设备失败',
)),
(failure) {
emit(state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '绑定设备失败',
));
// 抛出异常,携带后端返回的错误消息(如“设备不存在”)
throw Exception(failure.message ?? '绑定设备失败');
},
// 成功处理(返回 int 状态码)
(successCode) {
if (successCode == 1) {
@@ -201,14 +206,17 @@ class DevicesCubit extends Cubit<DevicesState> {
isLoading: false,
errorMessage: '绑定失败:状态码 $successCode',
));
throw Exception('绑定失败:状态码 $successCode');
}
},
);
} catch (e) {
emit(state.copyWith(
isLoading: false,
errorMessage: '绑定异常:${e.toString()}',
));
// emit(state.copyWith(
// isLoading: false,
// errorMessage: '绑定异常:${e.toString()}',
// ));
// 🔥 重新抛出,传递给 UI
rethrow;
}
}