Files
flutterApp/lib/features/remote_control/data/datasources/remote_http_datasource.dart

59 lines
1.5 KiB
Dart
Raw Normal View History

import 'dart:ffi';
2026-01-18 20:21:14 +08:00
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import '../../../../core/storage/user_storage.dart';
2026-01-18 20:21:14 +08:00
class RemoteHttpDatasource {
final Dio _dio;
final UserStorage _userStorage; // 🔥 新增字段
2026-01-18 20:21:14 +08:00
RemoteHttpDatasource(this._dio, this._userStorage);
2026-01-18 20:21:14 +08:00
/// 示例:获取控制授权 (对应之前逻辑中的权限申请)
/* Future<Map<String, dynamic>> requestControlAuth(String robotId) async {
2026-01-18 20:21:14 +08:00
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"});
try {
final data = response.data as Map<String, dynamic>;
final success = data['success'] as bool? ?? false;
if (success) {
return true;
} else {
return false;
}
} catch (e) {
debugPrint('❌ [RemoteHttp] 解析响应失败:$e');
return false;
}
2026-01-18 20:21:14 +08:00
}
}