This commit is contained in:
J. A. Messias 2024-11-12 14:36:41 -03:00
parent 0f2c5c2fd4
commit 2829403814
2 changed files with 137 additions and 0 deletions

View File

@ -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<ApiCallResponse> 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<ApiCallResponse> 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<ApiCallResponse> call() async {
final String baseUrl = PhpGroup.getBaseUrl();

View File

@ -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<Module> _modules = [];
List<Module> get modules => List.unmodifiable(_modules);
Future<void> initLicenseService() async {
await _fetchLicenses();
}
Future<void> _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<String, dynamic>;
final dynamic license = isList
? (jsonBody).map<Module>((data) => Module.fromJson(data as Map<String, dynamic>)).toList()
: isMap
? [Module.fromJson(jsonBody)]
: [];
log('Modulos obtidos: $license');
if (license is List<Module> && license.isNotEmpty && license != _modules) {
_modules = license;
log('Licença obtida com sucesso');
log('$_modules');
} else if (license is List<Module> && license.isEmpty) {
throw Exception('Nenhum módulo encontrado.');
}
} catch (e, s) {
log('Erro ao obter licença', error: e, stackTrace: s);
}
}
Future<void> checkLicense() async {
log('Verificando licença...');
await _fetchLicenses();
}
Future<void> catchLicense() async {
await PhpGroup.getLicense();
}
}