mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
188 lines
5.5 KiB
Dart
188 lines
5.5 KiB
Dart
import 'dart:convert';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:syncrow_web/pages/access_management/model/password_model.dart';
|
|
import 'package:syncrow_web/pages/visitor_password/model/device_model.dart';
|
|
import 'package:syncrow_web/pages/visitor_password/model/schedule_model.dart';
|
|
import 'package:syncrow_web/services/api/http_service.dart';
|
|
import 'package:syncrow_web/utils/constants/api_const.dart';
|
|
|
|
class AccessMangApi {
|
|
Future<List<PasswordModel>> fetchVisitorPassword() async {
|
|
try {
|
|
final response = await HTTPService().get(
|
|
path: ApiEndpoints.visitorPassword,
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
List<dynamic> jsonData = json;
|
|
List<PasswordModel> passwordList = jsonData.map((jsonItem) {
|
|
return PasswordModel.fromJson(jsonItem);
|
|
}).toList();
|
|
return passwordList;
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching visitor passwords: $e');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future fetchDevices() async {
|
|
try {
|
|
final response = await HTTPService().get(
|
|
path: ApiEndpoints.getDevices,
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
List<dynamic> jsonData = json;
|
|
List<DeviceModel> passwordList = jsonData.map((jsonItem) {
|
|
return DeviceModel.fromJson(jsonItem);
|
|
}).toList();
|
|
return passwordList;
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching $e');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<bool> postOnlineOneTime(
|
|
{String? email,
|
|
String? passwordName,
|
|
String? password,
|
|
String? effectiveTime,
|
|
String? invalidTime,
|
|
List<String>? devicesUuid}) async {
|
|
try {
|
|
final response = await HTTPService().post(
|
|
path: ApiEndpoints.sendOnlineOneTime,
|
|
body: jsonEncode({
|
|
"email": email,
|
|
"passwordName": passwordName,
|
|
"password": password,
|
|
"devicesUuid": devicesUuid,
|
|
"effectiveTime": effectiveTime,
|
|
"invalidTime": invalidTime
|
|
}),
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
if (json['statusCode'].toString() == '201') {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
},
|
|
);
|
|
return response;
|
|
} on DioException catch (e) {
|
|
debugPrint('Error: ${e.message}');
|
|
debugPrint('Error fetching ${e.response!.statusMessage}');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future postOnlineMultipleTime(
|
|
{String? effectiveTime,
|
|
String? invalidTime,
|
|
String? email,
|
|
String? password,
|
|
String? passwordName,
|
|
List<Schedule>? scheduleList,
|
|
List<String>? devicesUuid}) async {
|
|
try {
|
|
Map<String, dynamic> body = {
|
|
"email": email,
|
|
"devicesUuid": devicesUuid,
|
|
"passwordName": passwordName,
|
|
"password": password,
|
|
"effectiveTime": effectiveTime,
|
|
"invalidTime": invalidTime,
|
|
};
|
|
if (scheduleList != null) {
|
|
body["scheduleList"] =
|
|
scheduleList.map((schedule) => schedule.toJson()).toList();
|
|
}
|
|
final response = await HTTPService().post(
|
|
path: ApiEndpoints.sendOnlineMultipleTime,
|
|
body: jsonEncode(body),
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
if (json['data']['successOperations'][0]['success'].toString() ==
|
|
'true') {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
},
|
|
);
|
|
return response;
|
|
} on DioException catch (e) {
|
|
debugPrint('Error fetching ${e.type.name}');
|
|
debugPrint('Error fetching ${e.response!.statusMessage}');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// OffLine One Time Password
|
|
|
|
Future postOffLineOneTime(
|
|
{String? email, String? passwordName, List<String>? devicesUuid}) async {
|
|
try {
|
|
final response = await HTTPService().post(
|
|
path: ApiEndpoints.sendOffLineOneTime,
|
|
body: jsonEncode({
|
|
"email": email,
|
|
"passwordName": passwordName,
|
|
"devicesUuid": devicesUuid
|
|
}),
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
if (json['data']['successOperations'][0]['success'].toString() ==
|
|
'true') {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
});
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching $e');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future postOffLineMultipleTime(
|
|
{String? email,
|
|
String? passwordName,
|
|
String? effectiveTime,
|
|
String? invalidTime,
|
|
List<String>? devicesUuid}) async {
|
|
try {
|
|
final response = await HTTPService().post(
|
|
path: ApiEndpoints.sendOffLineOneTime,
|
|
body: jsonEncode({
|
|
"email": email,
|
|
"devicesUuid": devicesUuid,
|
|
"passwordName": passwordName,
|
|
"effectiveTime": effectiveTime,
|
|
"invalidTime": invalidTime
|
|
}),
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
if (json['data']['successOperations'][0]['success'].toString() ==
|
|
'true') {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
});
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching $e');
|
|
return [];
|
|
}
|
|
}
|
|
}
|