一次大的提交
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import '../../domain/entities/bind_device_entities.dart';
|
||||
|
||||
abstract class BindDeviceDatasource {
|
||||
Future<List<OrgEntity>> getOrgList();
|
||||
Future<List<SiteEntity>> getSitesByOrgId(int orgId);
|
||||
Future<List<UserSimpleEntity>> getUsersBySiteId(int siteId);
|
||||
Future<bool> isDeviceAtSite({required String deviceId, required int siteId});
|
||||
Future<void> bindDevice({
|
||||
required List<String> deviceIds,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
required int userId,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:maibu_satabot_v2/core/consts/http_api_consts.dart';
|
||||
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/storage/user_storage.dart';
|
||||
|
||||
import '../../../domain/entities/bind_device_entities.dart';
|
||||
import '../bind_device_datasource.dart';
|
||||
|
||||
class BindDeviceDatasourceImpl implements BindDeviceDatasource {
|
||||
final Dio _dio;
|
||||
|
||||
BindDeviceDatasourceImpl(this._dio);
|
||||
|
||||
Future<String?> _getToken() async {
|
||||
final token = sl<AppUserCubit>().state.user?.token;
|
||||
if (token != null) return token;
|
||||
final user = await sl<UserStorage>().getUser();
|
||||
return user?.token;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<OrgEntity>> getOrgList() async {
|
||||
final token = await _getToken();
|
||||
final response = await _dio.get(
|
||||
HttpApiConsts.orgList,
|
||||
queryParameters: {'pageNum': 1, 'pageSize': 1000},
|
||||
options: Options(
|
||||
headers: {'Authorization': token != null ? 'Bearer $token' : ''},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final data = response.data;
|
||||
if (data['code'] != 200) {
|
||||
throw Exception(data['msg'] ?? '获取组织列表失败');
|
||||
}
|
||||
|
||||
final List<dynamic> rows = data['rows'] ?? data['data'] ?? [];
|
||||
return rows
|
||||
.map((e) => OrgEntity.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SiteEntity>> getSitesByOrgId(int orgId) async {
|
||||
final token = await _getToken();
|
||||
final response = await _dio.get(
|
||||
HttpApiConsts.siteListByOrgId,
|
||||
queryParameters: {'id': orgId},
|
||||
options: Options(
|
||||
headers: {'Authorization': token != null ? 'Bearer $token' : ''},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final data = response.data;
|
||||
if (data['code'] != 200) {
|
||||
throw Exception(data['msg'] ?? '获取场站列表失败');
|
||||
}
|
||||
|
||||
final List<dynamic> rows = data['rows'] ?? data['data'] ?? [];
|
||||
return rows
|
||||
.map((e) => SiteEntity.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<UserSimpleEntity>> getUsersBySiteId(int siteId) async {
|
||||
final token = await _getToken();
|
||||
final response = await _dio.get(
|
||||
HttpApiConsts.userListBySiteId,
|
||||
queryParameters: {'siteId': siteId, 'pageNum': 1, 'pageSize': 1000},
|
||||
options: Options(
|
||||
headers: {'Authorization': token != null ? 'Bearer $token' : ''},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final data = response.data;
|
||||
if (data['code'] != 200) {
|
||||
throw Exception(data['msg'] ?? '获取人员列表失败');
|
||||
}
|
||||
|
||||
final List<dynamic> rows = data['rows'] ?? data['data'] ?? [];
|
||||
return rows
|
||||
.map((e) => UserSimpleEntity.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isDeviceAtSite({
|
||||
required String deviceId,
|
||||
required int siteId,
|
||||
}) async {
|
||||
final token = await _getToken();
|
||||
try {
|
||||
final response = await _dio.get(
|
||||
HttpApiConsts.getSiteDeviceList,
|
||||
queryParameters: {'siteId': siteId, 'pageSize': 9999, 'pageNum': 1},
|
||||
options: Options(
|
||||
headers: {'Authorization': token != null ? 'Bearer $token' : ''},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) return false;
|
||||
final data = response.data;
|
||||
if (data['code'] != 200) return false;
|
||||
|
||||
final List<dynamic> rows = data['rows'] ?? data['data'] ?? [];
|
||||
return rows.any((e) => e['deviceId']?.toString() == deviceId);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> bindDevice({
|
||||
required List<String> deviceIds,
|
||||
required int orgId,
|
||||
required int siteId,
|
||||
required int userId,
|
||||
}) async {
|
||||
final token = await _getToken();
|
||||
final response = await _dio.post(
|
||||
HttpApiConsts.bindDevice,
|
||||
data: {
|
||||
'deviceIds': deviceIds,
|
||||
'orgId': orgId,
|
||||
'siteId': siteId,
|
||||
'userId': userId,
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': token != null ? 'Bearer $token' : '',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final data = response.data;
|
||||
if (data['code'] != 200) {
|
||||
throw Exception(data['msg'] ?? '绑定设备失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user