import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:device_info/device_info.dart'; import 'dart:io'; import 'default_response_api.dart'; class FuncoesGerais{ //static const String urlLexLiteApi = "https://apis.lexstation.com/api/v1/"; static const String tokenPadrao = "6JN4NNEP6IDEK7I2BCXTA7D1DPJR78"; static const String urlBaseNewCore = "https://newcore.services/newcore/"; static const int appMobileCodigo = 18; static const String nomeApp = "Decpisos"; static const int versaoAtual = 100; static const int usuCodPadrao = 36718; static const String youtubeApiKey = "AIzaSyAmqjP0MxgASbt98Px1_hqaIvcsqeHO15k";//"AIzaSyA8jo3qsr-JJRm40iSHfkrzaMZXdMjo9v4"; static const String channelId = "UCJ_EUoswNTvRGotJ-ROxaKw"; static const String homeURL = "https://www.decpisos.com.br/"; static const String dominioPermitido = "https://www.decpisos.com.br"; static const String urlPDF = "http://app-cards.com/files/card-decpisos.pdf"; static Future checkInternetConnection() async{ bool retorno = true; try { final result = await InternetAddress.lookup('google.com').timeout(Duration(seconds: 5)); if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) { retorno = true; } else{ retorno = false; } } on SocketException catch (_) { retorno = false; } catch(err){ retorno = false; } return retorno; } static Future getUsuToken()async{ return Future.value(tokenPadrao); } static Future registraDevice(String token) async{ if(await FuncoesGerais.checkInternetConnection()){ if(token != null && token.isNotEmpty){ String url = FuncoesGerais.urlBaseNewCore+ "api/v1/user/${FuncoesGerais.tokenPadrao}/registra_device"; Map heads = Map(); heads["Content-type"] = "application/json; charset=utf-8"; heads["auth-key"] = token; DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); String model = ""; int platformCoddigo = 0; if (Platform.isAndroid) { platformCoddigo = 2; var info = await deviceInfo.androidInfo; model = info.model; } else if (Platform.isIOS) { platformCoddigo = 1; var info = await deviceInfo.iosInfo; model = info.utsname.machine; } var objPost = new RegistraDeviceRequest( aPPMOBILECODIGO: appMobileCodigo, dEVICETOKEN: token, mODELODEVICE: model, pLATAFORMAMOBILECODIGO: platformCoddigo, uSUCOD: FuncoesGerais.usuCodPadrao, ); try { var body = json.encode(objPost); //var response = await http.post(url, body: body,headers: heads); await http.post(url, body: body,headers: heads); //var r = await http.post(url, body: body,headers: heads); //print(r); // if (response != null) { // if (response.statusCode == 200) { // var data = jsonDecode(response.body); // retorno = LoginResponse.fromJson(data); // } // } } catch (e) { } } } // else{ // print('Nada'); // } } static Future checkVersaoMinimaOk() async{ VersaoMinimaResponse objRetorno = VersaoMinimaResponse( errorCode: 0, errorMessage: '', linkLoja: '', nome: '', versaoMinima: 0, vOk: true ); if(await FuncoesGerais.checkInternetConnection()){ String url = FuncoesGerais.urlBaseNewCore + "api/v1/appmobile/${FuncoesGerais.tokenPadrao}/versaominima/$appMobileCodigo"; Map heads = Map(); heads["Content-type"] = "application/json; charset=utf-8"; try { var response = await http.get(url,headers: heads); if (response != null) { if (response.statusCode == 200) { var data = jsonDecode(response.body); objRetorno = VersaoMinimaResponse.fromJson(data); // PackageInfo packageInfo = await PackageInfo.fromPlatform(); // var v = packageInfo.version.split('.'); // int versaoAtual = 0; //if(v.length > 0)versaoAtual = v[0] != null && v[0].isNotEmpty ? int.parse(v[0]) : 0; if(FuncoesGerais.versaoAtual != null && objRetorno.versaoMinima > FuncoesGerais.versaoAtual){ objRetorno.vOk = false; } } else{ objRetorno.errorCode = 4; objRetorno.errorMessage = 'Falha na comunição com a API error:${response.statusCode}.'; } } else{ objRetorno.errorCode = 3; objRetorno.errorMessage = 'Falha na comunição com o server.'; } } catch (e) { objRetorno.errorCode = 2; objRetorno.errorMessage = 'Falha na comunição com o server.'; } } else{ objRetorno.errorCode = 1; objRetorno.errorMessage = 'Sem Conexão com a internet.'; } return objRetorno; } }