109 lines
3.5 KiB
Dart
109 lines
3.5 KiB
Dart
import 'dart:ffi';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
import '../../../../core/logging/i_logger_service.dart';
|
|
import '../../../../core/storage/user_storage.dart';
|
|
|
|
class RemoteHttpDatasource {
|
|
final Dio _dio;
|
|
final UserStorage _userStorage; // 🔥 新增字段
|
|
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
|
|
|
|
|
RemoteHttpDatasource(this._dio, this._userStorage);
|
|
|
|
/// 示例:获取控制授权 (对应之前逻辑中的权限申请)
|
|
/* Future<Map<String, dynamic>> requestControlAuth(String robotId) async {
|
|
try {
|
|
final response = await _dio.post(
|
|
'/robot/auth/request',
|
|
data: {'robotId': robotId},
|
|
);
|
|
return response.data;
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}*/
|
|
|
|
|
|
Future<bool> requestRemoteControlViaHttp( String deviceId, String currentDeviceId) async {
|
|
final user = await _userStorage.getUser();
|
|
if (user == null) {
|
|
return false;
|
|
}
|
|
final token = user.token;
|
|
final response = await _dio.post(
|
|
'/forward/device/remoteControl',
|
|
options: Options(
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ${token}',
|
|
},
|
|
),
|
|
data: {'deviceId': deviceId, 'platform': "app"});
|
|
print("请求权限的数据,$deviceId,");
|
|
try {
|
|
final responseData = response.data as Map<String, dynamic>;
|
|
//debugPrint("📊 [HTTP 响应] 完整数据:$responseData");
|
|
_logger.log("📊 [HTTP 响应] 完整数据:$responseData");
|
|
|
|
// 🔥 关键:先获取响应的 data 字段,再获取 remoteControl
|
|
final dataField = responseData['data'] as Map<String, dynamic>?;
|
|
if (dataField != null) {
|
|
final bool hasRemoteControl = dataField['remoteControl'] as bool? ?? false;
|
|
|
|
//debugPrint("✅ [HTTP 响应] remoteControl=$hasRemoteControl");
|
|
_logger.log("✅ [HTTP 响应] remoteControl=$hasRemoteControl");
|
|
// 🔥 直接返回 remoteControl 的布尔值
|
|
return hasRemoteControl;
|
|
} else {
|
|
//debugPrint("❌ [HTTP 响应] 缺少 data 字段");
|
|
_logger.log("❌ [HTTP 响应] 缺少 data 字段");
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
// debugPrint('❌ [RemoteHttp] 解析响应失败:$e');
|
|
_logger.log('❌ [RemoteHttp] 解析响应失败:$e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
///app退出远程控制后释放权限
|
|
Future<bool> releaseRemoteControlViaHttp( String platform) async {
|
|
// _logger.logWithLevel("app退出远程控制后释放权限 开始");
|
|
final user = await _userStorage.getUser();
|
|
if (user == null) {
|
|
return false;
|
|
}
|
|
final token = user.token;
|
|
final response = await _dio.post(
|
|
'/forward/device/releaseControl',
|
|
options: Options(
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ${token}',
|
|
},
|
|
),
|
|
data: {'platform': "app"});
|
|
try {
|
|
final responseData = response.data as Map<String, dynamic>;
|
|
//debugPrint("📊 [HTTP 响应] 完整数据:$responseData");
|
|
//_logger.log("releaseRemoteControlViaHttp 响应] 完整数据:$responseData");
|
|
if(responseData['code']==200){
|
|
// _logger.logWithLevel("app退出远程控制后释放权限 成功");
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch (e) {
|
|
// debugPrint('❌ [RemoteHttp] 解析响应失败:$e');
|
|
_logger.log('❌ [RemoteHttp] 解析响应失败:$e');
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
}
|