修改设备别名接口+UI
This commit is contained in:
@@ -4,6 +4,7 @@ abstract class DeviceHttpDatasource {
|
||||
Future<List<DeviceEntity>> getUserDevices(String username);
|
||||
Future<int> bindDevice(String deviceId, String deviceAlias);
|
||||
Future<int> unbindDevice(String deviceId, String deviceName);
|
||||
Future<int> updateDeviceName(String deviceId, String deviceName);
|
||||
Future<int> switchDevice(String platform, String deviceId);
|
||||
|
||||
Future<dynamic> getDeviceLocation(String username) async {}
|
||||
|
||||
@@ -92,7 +92,27 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
throw Exception(data['msg']);
|
||||
}
|
||||
|
||||
return data['data'];
|
||||
return data['data'] == true ? 1 : 0; // 根据接口返回的布尔值转换为 int
|
||||
}
|
||||
|
||||
Future<int> updateDeviceName(String deviceId, String deviceName) async {
|
||||
final response = await dio.post(
|
||||
'/forward/device/updateDeviceAlias',
|
||||
data: {"deviceId": deviceId, "deviceAlias": deviceName},
|
||||
);
|
||||
print('修改别名响应: ${response}'); // 调试输出响应数据
|
||||
|
||||
final data = response.data;
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络错误');
|
||||
}
|
||||
|
||||
if (data['code'] != 200) {
|
||||
throw Exception(data['msg']);
|
||||
}
|
||||
|
||||
return data['data'] == true ? 1 : 0; // 根据接口返回的布尔值转换为 int
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -61,6 +61,19 @@ class DeviceRepositoryImpl implements DeviceRepository {
|
||||
return Left(DeviceFailure(cleanMessage));
|
||||
}
|
||||
}
|
||||
@override
|
||||
Future<Either<DeviceFailure, int>> updateDeviceName(String deviceId, String deviceName) async {
|
||||
try {
|
||||
var result = await _deviceHttpDatasource.updateDeviceName(deviceId, deviceName);
|
||||
return Right(result);
|
||||
} on DioException catch (e) {
|
||||
final String serverMessage = e.response?.data['msg'] ?? "网络连接异常";
|
||||
return Left(DeviceFailure(serverMessage));
|
||||
} catch (e) {
|
||||
final cleanMessage = e.toString().replaceFirst('Exception: ', '');
|
||||
return Left(DeviceFailure(cleanMessage));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<DeviceFailure, int>> switchDevice(
|
||||
|
||||
@@ -15,6 +15,10 @@ abstract class DeviceRepository {
|
||||
String deviceId,
|
||||
String deviceName,
|
||||
);
|
||||
Future<Either<DeviceFailure, int>> updateDeviceName(
|
||||
String deviceId,
|
||||
String deviceAlias,
|
||||
);
|
||||
Future<Either<DeviceFailure, int>> switchDevice(
|
||||
String platform,
|
||||
String deviceId,
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:maibu_satabot_v2/core/domain/usecases/base_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/core/error/failure.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
||||
|
||||
class UpdateDevicenameUsecase
|
||||
implements BaseUseCase<int, UpdateDevicenameParams> {
|
||||
final DeviceRepository repository;
|
||||
|
||||
UpdateDevicenameUsecase(this.repository);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, int>> call(UpdateDevicenameParams params) async {
|
||||
return await repository.updateDeviceName(
|
||||
params.deviceId,
|
||||
params.deviceAlias,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateDevicenameParams {
|
||||
final String deviceId;
|
||||
final String deviceAlias;
|
||||
|
||||
UpdateDevicenameParams(this.deviceId, this.deviceAlias);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/usecases/get_user_device_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/usecases/unbind_device_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/usecases/update_devicename_usecase.dart';
|
||||
|
||||
import '../../domain/usecases/delete_work_record_usecase.dart';
|
||||
import '../../domain/usecases/get_device_location_usecase.dart';
|
||||
@@ -16,6 +17,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
final GetWorkRecordUseCase _getWorkRecordUseCase;
|
||||
final DeleteWorkRecordUseCase _deleteWorkRecordUseCase;
|
||||
final UnbindDeviceUseCase _unbindDeviceUseCase;
|
||||
final UpdateDevicenameUsecase _updateDevicename;
|
||||
|
||||
DevicesCubit(
|
||||
this.repository,
|
||||
@@ -24,10 +26,17 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
this._getWorkRecordUseCase,
|
||||
this._deleteWorkRecordUseCase,
|
||||
this._unbindDeviceUseCase,
|
||||
this._updateDevicename,
|
||||
) : super(const DevicesState());
|
||||
|
||||
Future<void> unbindDevice(String deviceId, String deviceName) async {
|
||||
emit(state.copyWith(isLoading: true, errorMessage: ''));
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: true,
|
||||
errorMessage: '',
|
||||
operationType: DeviceOperationType.unbind,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
final params = UnbindDeviceParams(deviceId, deviceName);
|
||||
@@ -38,6 +47,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '解绑设备失败',
|
||||
operationType: DeviceOperationType.none, // 操作结束重置
|
||||
),
|
||||
),
|
||||
(successCode) {
|
||||
@@ -56,6 +66,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
? null
|
||||
: state.selectedDevice,
|
||||
errorMessage: '',
|
||||
operationType: DeviceOperationType.none,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
@@ -63,6 +74,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '解绑失败:状态码 $successCode',
|
||||
operationType: DeviceOperationType.none,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -70,7 +82,68 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(isLoading: false, errorMessage: '解绑异常:${e.toString()}'),
|
||||
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,
|
||||
),
|
||||
);
|
||||
|
||||
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();
|
||||
|
||||
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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
enum DeviceOperationType {
|
||||
none, // 无操作
|
||||
unbind, // 解绑设备
|
||||
updateName, // 修改设备名称
|
||||
}
|
||||
|
||||
|
||||
class DevicesState extends Equatable {
|
||||
final List<DeviceEntity> devices; // 名下所有设备列表
|
||||
@@ -9,6 +15,7 @@ class DevicesState extends Equatable {
|
||||
final double? deviceLatitude; // 新增字段
|
||||
final double? deviceLongitude; // 新增字段
|
||||
final List<Map<String, dynamic>>? workRecords;
|
||||
final DeviceOperationType operationType; // 标记当前操作类型
|
||||
const DevicesState({
|
||||
this.devices = const [],
|
||||
this.selectedDevice,
|
||||
@@ -16,7 +23,8 @@ class DevicesState extends Equatable {
|
||||
this.errorMessage,
|
||||
this.deviceLatitude,
|
||||
this.deviceLongitude,
|
||||
this.workRecords
|
||||
this.workRecords,
|
||||
this.operationType = DeviceOperationType.none, // 默认无操作
|
||||
});
|
||||
|
||||
// 使用 copyWith 方便局部更新状态
|
||||
@@ -28,6 +36,7 @@ class DevicesState extends Equatable {
|
||||
double? deviceLatitude,
|
||||
double? deviceLongitude,
|
||||
List<Map<String, dynamic>>? workRecords,
|
||||
DeviceOperationType? operationType,
|
||||
}) {
|
||||
return DevicesState(
|
||||
devices: devices ?? this.devices,
|
||||
@@ -37,6 +46,7 @@ class DevicesState extends Equatable {
|
||||
deviceLatitude: deviceLatitude ?? this.deviceLatitude,
|
||||
deviceLongitude: deviceLongitude ?? this.deviceLongitude,
|
||||
workRecords: workRecords ?? this.workRecords,
|
||||
operationType: operationType ?? this.operationType, // 更新操作类型
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user