var smartpos = new Smartpos(); setTimeout('smartpos.init(0)', 10 ); // No POS nada pode sair do App: clique em link externo nao faz nada. document.addEventListener('click', function(e) { if ( (!smartpos) || (!smartpos.pos) ) { return; } var a = e.target; while ( (a) && (a.tagName!='A') ) { a = a.parentNode; } if ( (a) && (smartpos.urlSaiDoApp( a.getAttribute('href') )) ) { console.error('POS: link externo bloqueado', a.getAttribute('href')); e.preventDefault(); e.stopPropagation(); } }, true); // window.open com URL externa tambem nao abre (ex.: QR do chatbot, topico do Wiki). var windowOpenOriginal = window.open; window.open = function( url ) { if ( (typeof smartpos != 'undefined') && (smartpos) && (smartpos.pos) && (smartpos.urlSaiDoApp(url)) ) { console.error('POS: window.open externo bloqueado', url); return null; } return windowOpenOriginal.apply(window, arguments); } function Smartpos() { this.initiated = false; this.init = function(n) { console.log('smartpos.init(' + n + ')'); if (typeof http === 'undefined') { setTimeout('smartpos.init(' + n+1 + ')', (n>100 ? 1000 : (n>10 ? 100 : 10) ) ); return; } this.wv3 = ("AndroidBridge" in window) ? true : false; this.wv2 = ("SmartPOSJSInterface" in window) ? true : false; this.wv1 = ("JSInterface" in window) ? true : false; var j = {}; if (this.wv3) { j = JSON.parse( window.AndroidBridge.getFeatures() ); j.pos = http.getGetParameter('pos'); j.emp = http.getGetParameter('emp'); j.sessao = http.getGetParameter('pos_sessao'); j.adiquirente = http.getGetParameter('pos_adquirente'); j.header = !(http.getGetParameter('_noheader')); } else if ( http.getGetParameter( 'pos' ) ) { console.log('pos= ' + http.getGetParameter( 'pos' )); j = { pos: http.getGetParameter( 'pos' ) ,emp: http.getGetParameter( 'emp' ) ,sessao: http.getGetParameter( 'pos_sessao' ) ,adiquirente: http.getGetParameter( 'pos_adquirente' ) ,webview: http.getGetParameter( '_wv' ) ,gecko: http.getGetParameter( '_gk' ) ,pay: http.getGetParameter( '_pay' ) ,barcode: http.getGetParameter( '_bar' ) ,print: http.getGetParameter( '_prt' ) ,header: !(http.getGetParameter( '_noheader' )) }; } if ( (!j.pos) && ("JSInterface" in window) && (window.JSInterface.getEmp)) // TODO - tirar isso quando a LIO estiver pronta { j.pos = window.JSInterface.getPos(); var emp = window.JSInterface.getEmp(); j.emp = http.getGetParameter( 'emp' ); if (!j.emp) { j.emp = emp; } if ( (j.emp) && (emp!=j.emp) ) { window.alert( j.emp + '!=' + emp); } j.webview = 1; j.pay = !!(window.JSInterface.pay); j.payAvailable = !!(window.JSInterface.pay); // TODO j.barcode = !!(window.JSInterface.barcode); j.print = !!(window.JSInterface.print); // TODO j.printAvailable = !!(window.JSInterface.print); j.header = !(http.getGetParameter( '_noheader' )); j.sessao = 'xyz'; // TODO } if (j.pos) { console.error(j); http.setCookie('pos', j); j.config = this.getPos(); } var xx = Object.keys(j); for(var i=0; i < xx.length; i++) { this[xx[i]] = j[xx[i]]; } this.initiated = true; console.log( "smartpos:", j ); setTimeout('smartpos.readFPGs()',1000); // Ficha tecnica do aparelho: assincrona, fora do caminho do boot (ver enviaFichaDoAparelho). // TEMPORARIO: o app 4.0.3+ envia o setPosFeatures sozinho (FichaNoServidor); so' app mais antigo depende deste JSP. // RETIRAR este envio (e fichaDoAparelho/enviaFichaDoAparelho) quando todo o parque estiver na 4.0.3. var versionCode = 0; try { versionCode = parseInt( window.AndroidBridge.getAppVersionCode(), 10 ) || 0; } catch (e) { } if ( this.wv3 && j.pos && (versionCode < 403) ) { setTimeout('smartpos.enviaFichaDoAparelho()', 5000); } } // Ficha do aparelho: app novo (4.0.2+) devolve o JSON completo em getDeviceInfo; app antigo // (sem getDeviceInfo) tem uma ficha minima montada com os metodos que a ponte ja oferece. // Cada chamada e' isolada: o que faltar fica undefined e some do JSON. Nunca lanca. this.fichaDoAparelho = function() { var b = window.AndroidBridge; if ( !b ) { return ''; } if ( b.getDeviceInfo ) { try { var f = b.getDeviceInfo(); if ( f && f.length > 2 ) { return f; } } catch (e) { } } function t(nome) { try { return b[nome] ? String( b[nome]() ) : undefined; } catch (e) { return undefined; } } function j(nome) { try { return b[nome] ? JSON.parse( b[nome]() ) : undefined; } catch (e) { return undefined; } } // O que o proprio WebView sabe dizer do aparelho (nao depende da versao do app): o // userAgent traz Android, modelo e versao do WebView; o resto, tela, idioma e rede. function web() { try { var n = window.navigator || {}; var c = n.connection || {}; return { userAgent: n.userAgent , plataforma: n.platform , idioma: n.language , fusoHorario: (window.Intl && Intl.DateTimeFormat) ? Intl.DateTimeFormat().resolvedOptions().timeZone : undefined , tela: (window.screen ? (screen.width + 'x' + screen.height) : undefined) , janela: (window.innerWidth + 'x' + window.innerHeight) , dpr: window.devicePixelRatio , cpus: n.hardwareConcurrency , memoriaGb: n.deviceMemory , toque: n.maxTouchPoints , conexao: (c.effectiveType || c.type) }; } catch (e) { return undefined; } } try { return JSON.stringify( { origem: 'ponte-antiga' , app_versao: t('getAppVersionName'), app_versionCode: t('getAppVersionCode') , app_pacote: t('getApplicationId'), app_datahora: t('getAppDateTime') , adquirente: t('getAcquirerName') , fabricante: t('getManufacturer') , android: t('getAndroidVersion'), api: t('getDeviceApiLevel') , java_source: t('getJavaSourceVersion'), java_target: t('getJavaTargetVersion') , app_minSdk: t('getAppMinSdk'), app_targetSdk: t('getAppTargetApiLevel') , modelo: t('getModel'), marca: t('getBrand'), codinome: t('getDeviceCodeName') , hardware: t('getHardware'), board: t('getBoard'), produto: t('getProduct') , build_id: t('getBuildId'), build_display: t('getBuildDisplay'), abis: t('getSupportedAbis') , tela_app: t('getScreenInfo'), uptime_ms: t('getUptimeMs'), bateria_pct: t('getBatteryLevel') , armazenamento_livre_mb: t('getInternalStorageAvailableMb') , app_tamanho: t('getAppSize'), app_assets: t('getAppAssetsSize'), app_cache: t('getAppCacheSize') , build: j('getPosBuildInfo') , features: j('getFeatures') , cache: j('getCacheInfo') , web: web() } ); } catch (e) { return ''; } } // Envia a ficha tecnica do aparelho ao PosCmd.setPosFeatures, uma vez por execucao. // Dispara e esquece: servidor antigo (sem o metodo) ou qualquer erro simplesmente nao // fazem nada. // POST pelo http.callHTTP (o wv3 leva o corpo "de carona" na URL, apos "|||"); o JSON vai // em base64url para nao ter caractere problematico ('#', '&', '%', acento) nesse caminho. this.enviaFichaDoAparelho = function() { try { if ( window.smartposFichaEnviada || !window.AndroidBridge || !window.AndroidBridge.getAndroidId ) { return; } var ficha = this.fichaDoAparelho(); if ( !ficha || ficha.length < 3 ) { return; } window.smartposFichaEnviada = true; var b64 = btoa( unescape( encodeURIComponent( ficha ) ) ).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); var url = http.getUrl('contahub.a.PosCmd', 'setPosFeatures') + '?pos_serial=' + encodeURIComponent( window.AndroidBridge.getPosSerial ? window.AndroidBridge.getPosSerial() : window.AndroidBridge.getAndroidId() ) + '&pos=' + encodeURIComponent( this.pos || '' ) + '&emp=' + encodeURIComponent( this.emp || '' ); http.callHTTP( url , function() { } , function() { } , [ { id: 'pos_features', value: b64 } ] ); } catch (e) { console.log('enviaFichaDoAparelho falhou (ignorado)', e); } } // URL que tira o usuario do App: absoluta (http/https, com ou sem esquema) ou esquema // de outro aplicativo. A navegacao interna do ContaHUB e' sempre relativa. this.urlSaiDoApp = function( url ) { if (!url) { return false; } return ( /^(https?:)?\/\//i.test(url) ) || ( /^(whatsapp|tg|telegram|mailto|tel|sms|intent|market|geo|maps):/i.test(url) ); } this.fakePos = function(pos) { this.wv3 = false; this.wv2 = false; this.wv1 = false; var j = { pos: pos ,emp: 433 ,sessao: 1 }; http.setCookie('pos', j); var xx = Object.keys(j); for(var i=0; i < xx.length; i++) { this[xx[i]] = j[xx[i]]; } this.initiated = true; console.error( "fake smartpos:", j ); } this.readFPGs = function() { if ( ( (typeof index)==='undefined') || (!index) || (!index.emp) ) { return; } var url = http.getUrl('contahub.a.PosCmd', 'getFPGs') + "?emp=" + index.emp + "&pos=" + smartpos.pos + "&pos_sessao=" + smartpos.sessao ; http.callHTTP( url, function( dados ) { console.error('getFPGs',dados); smartpos.fpgs = dados.list; } , http.alert ); } this.getPos = function() { if (this.wv3) { result = window.AndroidBridge.pos('getPos', ''); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('getPos', ''); } else if (this.wv1) { result = {erro:"comandoInvalido"}; } else if (this.gecko) { result = window.prompt("pos:getPos", ''); } else { result = null; } return result; } this.status = function() { if (this.wv3) { return window.AndroidBridge.pos('status', ''); } else if (this.wv2) { return window.SmartPOSJSInterface.pos('status', ''); } return ''; } this.getLog = function() { if (this.wv3) { result = window.AndroidBridge.pos('getLog', ''); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('getLog', ''); } else if (this.wv1) { result = {erro:"comandoInvalido"}; } else if (this.gecko) { result = window.prompt("pos:getLog", ''); } else { result = null; } return result; } this.getResult = function() { if (this.wv3) { result = window.AndroidBridge.pos('result', ''); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('result', ''); } else if (this.wv1) { result = {erro:"comandoInvalido"}; } else { result = window.prompt("pos:result", ''); } return result; } this.getBarcode = function() { if (this.wv3) { result = window.AndroidBridge.pos('barcode', ''); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('barcode', ''); } else if (this.wv1) { result = {erro:"comandoInvalido"}; } else { result = window.prompt("pos:barcode", ''); } return result; } this.doPrint = function(dados) { if (this.wv3) { result = window.AndroidBridge.pos('print', JSON.stringify(dados)); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('print', JSON.stringify(dados)); } else if (this.wv1) { result = {erro:"comandoInvalido"}; window.alert("comando inválido"); } else if (this.gecko) { // window.alert(JSON.stringify(this)); result = window.SmartPOSJSInterface.pos('print', JSON.stringify(dados)); } else { window.alert("???"); result = window.prompt("pos:print", JSON.stringify(dados)); } return result; } this.doFicha = function(dados) { if (this.wv3) { result = window.AndroidBridge.pos('print', JSON.stringify(dados)); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('print', JSON.stringify(dados)); } else if (this.wv1) { result = {erro:"comandoInvalido"}; } else { result = window.prompt("pos:print", JSON.stringify(dados)); } return result; } this.doPay = function(dados) { if (this.wv3) { result = window.AndroidBridge.pos('pay', JSON.stringify(dados)); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('pay', JSON.stringify(dados)); } else if (this.wv1) { result = {erro:"comandoInvalido"}; } else { result = window.prompt("pos:pay", JSON.stringify(dados)); } return result; } // Rodape do app ("#18" | empresa). So o app 4.0 (wv3) tem; nos demais nao faz nada. this.setRodape = function(pos, nome, nfe) { if (this.wv3 && window.AndroidBridge.setRodape) { window.AndroidBridge.setRodape(String(pos || 0), nome || '', Number(nfe) || 0); } } this.goFullScreen = function() { if (this.wv3) { result = window.AndroidBridge.pos('fullScreen', ''); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('fullScreen', ''); } else if (this.wv1) { result = {erro:"comandoInvalido"}; } else { result = window.prompt("pos:fullScreen", ''); } return result; } this.mifare = function(dados) { if (this.wv3) { result = window.AndroidBridge.pos('mifare', JSON.stringify(dados)); } else if (this.wv2) { result = window.SmartPOSJSInterface.pos('mifare', JSON.stringify(dados)); } else if (this.wv1) { // result = {erro:"comandoInvalido"}; // window.alert("comando inválido"); } else if (this.gecko) { result = window.SmartPOSJSInterface.pos('mifare', JSON.stringify(dados)); } else { // window.alert("???"); // result = window.prompt("pos:mifare", JSON.stringify(dados)); result = "pos:mifare"; } return result; } }