From 28294038148b07bc1f5f3d2d8a2e670bfe34c72e Mon Sep 17 00:00:00 2001 From: "J. A. Messias" Date: Tue, 12 Nov 2024 14:36:41 -0300 Subject: [PATCH] WIP --- lib/backend/api_requests/api_calls.dart | 63 ++++++++++++++++ .../services/license/license_service.dart | 74 +++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 lib/shared/services/license/license_service.dart diff --git a/lib/backend/api_requests/api_calls.dart b/lib/backend/api_requests/api_calls.dart index e1dac19a..9d4484f8 100644 --- a/lib/backend/api_requests/api_calls.dart +++ b/lib/backend/api_requests/api_calls.dart @@ -56,8 +56,71 @@ class PhpGroup { static GetPets getPets = GetPets(); static GetPetPhoto getPetPhoto = GetPetPhoto(); static UnregisterDevice unregisterDevice = UnregisterDevice(); + static GetLicense getLicense = GetLicense(); + static TestCall testCall = TestCall(); } +class TestCall { + Future call() async { + const String accessKey = 'af7483f'; + const String usuEmail = 'email_app@exemplo.com'; + const String idDestino = '1'; + + return ApiManager.instance.makeApiCall( + callName: 'getLicense', + apiUrl: 'https://residenceapi.fre.com.br/getLicenca.php', + callType: ApiCallType.POST, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + params: { + 'AccessKey': accessKey, + 'UsuEmail': usuEmail, + 'idDestino': idDestino, + }, + bodyType: BodyType.X_WWW_FORM_URL_ENCODED, + returnBody: true, + encodeBodyUtf8: false, + decodeUtf8: false, + cache: false, + isStreamingApi: false, + alwaysAllowBody: false, + ); + } +} + +class GetLicense { + Future call() async { + final String baseUrl = PhpGroup.getBaseUrl(); + final String devUUID = (await StorageHelper().get(SQLiteStorageKey.devUUID.value, Storage.SQLiteStorage)) ?? ''; + final String userUUID = (await StorageHelper().get(SQLiteStorageKey.userUUID.value, Storage.SQLiteStorage)) ?? ''; + final String cliID = (await StorageHelper().get(SQLiteStorageKey.clientUUID.value, Storage.SQLiteStorage)) ?? ''; + + return ApiManager.instance.makeApiCall( + callName: 'getLicense', + apiUrl: '$baseUrl/processRequest.php', + callType: ApiCallType.POST, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + params: { + 'devUUID': devUUID, + 'userUUID': userUUID, + 'cliID': cliID, + 'atividade': 'getLicenca', + }, + bodyType: BodyType.X_WWW_FORM_URL_ENCODED, + returnBody: true, + encodeBodyUtf8: false, + decodeUtf8: false, + cache: false, + isStreamingApi: false, + alwaysAllowBody: false, + ); + } +} + + class UnregisterDevice { Future call() async { final String baseUrl = PhpGroup.getBaseUrl(); diff --git a/lib/shared/services/license/license_service.dart b/lib/shared/services/license/license_service.dart new file mode 100644 index 00000000..51f234c4 --- /dev/null +++ b/lib/shared/services/license/license_service.dart @@ -0,0 +1,74 @@ +// ignore_for_file: curly_braces_in_flow_control_structures, prefer_is_empty + +import 'dart:developer'; + +import 'package:hub/backend/api_requests/api_calls.dart'; +import 'dart:convert'; + +class LicenseService { + static final LicenseService _instance = LicenseService._internal(); + + factory LicenseService() => _instance; + LicenseService._internal(); + + List _modules = []; + + List get modules => List.unmodifiable(_modules); + + Future initLicenseService() async { + await _fetchLicenses(); + } + + Future _fetchLicenses() async { + try { + log('Obtendo licenças...'); + final ApiCallResponse response = await PhpGroup.testCall(); + final String responseBody = response.jsonBody.toString(); + + if (responseBody.isEmpty) { + throw Exception('Licensa ausente ou vazia.'); + } + + // if (responseBody == "[]") { + // log('Nenhuma licença encontrada.'); + // _modules = []; + // log('() MOD => $_modules'); + // return; + // } + + final dynamic jsonBody = jsonDecode(responseBody); + + if (jsonBody is Map && jsonBody['error'] == true) { + final String error = jsonBody['error_msg'] ?? 'Erro desconhecido'; + throw Exception('Erro ao consultar licenças: $error'); + } + + final bool isList = jsonBody is List; + final bool isMap = jsonBody is Map; + final dynamic license = isList + ? (jsonBody).map((data) => Module.fromJson(data as Map)).toList() + : isMap + ? [Module.fromJson(jsonBody)] + : []; + log('Modulos obtidos: $license'); + if (license is List && license.isNotEmpty && license != _modules) { + _modules = license; + log('Licença obtida com sucesso'); + log('$_modules'); + } else if (license is List && license.isEmpty) { + throw Exception('Nenhum módulo encontrado.'); + } + } catch (e, s) { + log('Erro ao obter licença', error: e, stackTrace: s); + } + } + + Future checkLicense() async { + log('Verificando licença...'); + await _fetchLicenses(); + } + + Future catchLicense() async { + await PhpGroup.getLicense(); + } +}