$("#user-exp-alerts-wrapper").css('visibility','hidden'); scripts_dinamicos_carregados = false; scripts_dinamicos_click = false; function scripts_add(){ if( scripts_dinamicos_carregados === true){ return; } console.log("ADD SCRIPTS"); const script_0 = document.createElement('script'); script_0.src = 'https://news.google.com/swg/js/v1/swg.js?ver=2.2.14'; document.body.appendChild(script_0); const script_1 = document.createElement('script'); script_1.src = 'https://veja.abril.com.br/wp-content/plugins/abril-plugins/abril-paywall/js/abril-firebase-auth/abril-swg.js?ver=2.2.14'; document.body.appendChild(script_1); const script_2 = document.createElement('script'); script_2.src = 'https://veja.abril.com.br/wp-content/plugins/abril-plugins/abril-paywall/js/abril-firebase-auth/abril-interface-middleware.js?ver=2.2.14'; document.body.appendChild(script_2); const script_3 = document.createElement('script'); script_3.src = 'https://veja.abril.com.br/wp-content/plugins/abril-plugins/abril-paywall/js/abril-firebase-auth/abril-paywall.js?ver=2.2.14'; document.body.appendChild(script_3); const script_4 = document.createElement('script'); script_4.src = 'https://veja.abril.com.br/wp-content/plugins/abril-plugins/abril-paywall/js/abril-firebase-auth/abril-login.js?ver=2.2.14'; document.body.appendChild(script_4); const script_5 = document.createElement('script'); script_5.src = 'https://veja.abril.com.br/wp-content/plugins/abril-plugins/abril-paywall/js/abril-firebase-auth/abril-paywall-ofertas.js?ver=2.2.14'; document.body.appendChild(script_5); const script_6 = document.createElement('script'); script_6.src = 'https://accounts.google.com/gsi/client?ver=2.2.14'; script_6.async = true; document.body.appendChild(script_6); const script_7 = document.createElement('script'); script_7.src = 'https://veja.abril.com.br/wp-content/plugins/abril-plugins/abril-paywall/js/abril-firebase-auth/page-interactions.js?ver=2.2.14'; document.body.appendChild(script_7); const script_8 = document.createElement('script'); script_8.src = 'https://veja.abril.com.br/wp-content/plugins/abril-plugins/abril-paywall/js/abril-firebase-auth/bundle.js?ver=2.2.14'; document.body.appendChild(script_8); scripts_dinamicos_carregados = true; return ; } $(window).on('scroll mousemove',function() { scripts_add(); }); function scripts_add_click(clallback, param){ if( scripts_dinamicos_carregados !== true){ scripts_add(); scripts_dinamicos_carregados = true; } if( typeof AbrilLogin === 'undefined' ){ return setTimeout(() => { scripts_add_click(clallback, param); }, "1000"); } pcallback = eval("(" + clallback + ")"); return pcallback(param); } $(".menuOpener").click( () =>{ if( scripts_dinamicos_carregados !== true){ scripts_add(); scripts_dinamicos_carregados = true; } return; } ); $("#user-exp-ctrl").on("click", function () { $("#user-exp-login-spinner").addClass("loadExpLoggedSpinner"); $('#user-exp-ctrl').css("pointerEvents", "none"); $("#header-user-exp-ctrl").css("pointerEvents", "none"); $("#user-exp-login-spinner").toggleClass("show"); $("html").toggleClass("user-exp-overflow"); // refatora $('#user-exp-ctrl').css("pointerEvents", "none"); $("#header-user-exp-ctrl").css("pointerEvents", "none"); $("#user-exp-menu").toggleClass("show"); $("#user-exp-menu-fade").toggleClass("show"); $("#user-exp-login-spinner").removeClass("loadExpLoggedSpinner"); if( scripts_dinamicos_carregados !== true){ scripts_add(); scripts_dinamicos_carregados = true; } return; } ); $(".user-alert-close img").on("click", function () { $('#user-exp-ctrl').css("pointerEvents", "auto") $("#header-user-exp-ctrl").css("pointerEvents", "auto"); $("#user-exp-menu").removeClass("show"); $("#user-exp-menu-fade").removeClass("show"); $("html").toggleClass("user-exp-overflow"); $("#user-exp-login-spinner").toggleClass("show"); if($(".user-alert-icon img").length > 0){ $(".user-alert-icon img").css("display", "none"); } if($(".user-alert-icon").length > 0){ $(".user-alert-icon").css("display", "none"); } setTimeout(() => { let userStorage = JSON.parse(localStorage.getItem('abrilAssetsUser')); console.log(userStorage.crm); AbrilNotificacoes.ler(userStorage.crm); }, 2000); }); var AbrilNotificacoes = (function () { var cokieName = "notificationsControl"; var storageName = "abrilNotificacoes"; function init(){ const data_info = JSON.parse(localStorage.getItem('abrilNotificacoes')); console.log('AbrilNotificacoes.init()', data_info); if(data_info===null){ $('.user-avatar-name').css('display','inline'); $('#user-exp-ctrl .user-avatar').css('display', 'none'); $('.user-avatar-name').css('display','inline-block') $('#user-exp-ctrl .user-avatar').css('display','none'); $('.user-avatar.user-avatar-notification').css('display', 'none'); $("#user-exp-alerts-wrapper").css('visibility','hidden'); $(".user-exp-detail").css('border-radius','0 0 4px 4px'); return; } $("#user-exp-login-spinner").removeClass("loadExpLoggedSpinner"); if (data_info.avatar) { $('.user-avatar-name').css('display','none'); $('#user-exp-ctrl .user-avatar').attr('src', data_info.avatar).css('display', 'inline'); $('.user-avatar.user-avatar-notification').attr('src', data_info.avatar).css('display', 'inline'); $(".user-alert-icon").addClass("user-alert-foto"); } else { $('.user-avatar-name').css('display','inline-block') $('#user-exp-ctrl .user-avatar').css('display','none'); $('.user-avatar.user-avatar-notification').css('display', 'none'); $(".user-alert-icon").removeClass("user-alert-foto"); } if(typeof data_info.visualizado !=="undefined"){ if(data_info.visualizado === true){ $("#user-exp-ctrl").attr("data-visualizado", true); $(".user-alert-icon").css("display", "none"); }else{ $("#user-exp-ctrl").attr("data-visualizado", false); $(".user-alert-icon").css("display", "inline-block"); } } let perfilUrl = 'https://perfil.abril.com.br'; if(assine_environment[0]==='sandbox'){ let pageUrl = window.location.href; perfilUrl = 'https://gutenberg.perfil.abril.com.br'; if( pageUrl.indexOf('beta-develop') !== -1 ){ perfilUrl = 'https://beta-develop.perfil.abril.com.br'; } if( pageUrl.indexOf('gutenberg') !== -1 ){ perfilUrl = 'https://gutenberg.perfil.abril.com.br'; } if( pageUrl.indexOf('preprod') !== -1 ){ perfilUrl = 'https://preprod.perfil.abril.com.br'; } if( pageUrl.indexOf('go-') !== -1 ){ perfilUrl = 'https://go-perfil.abril.com.br'; } } $('.user-exp-account a').attr('href', perfilUrl); if(typeof data_info.notifications === 'undefined'){ $("#user-exp-alerts-wrapper").css('visibility','hidden'); $(".user-exp-detail").css('border-radius','0 0 4px 4px'); return; } if(data_info.notifications === null){ $("#user-exp-alerts-wrapper").css({'visibility':'hidden'}); $(".user-exp-detail").css({'border-radius':'0 0 4px 4px'}); return; } return AbrilNotificacoes.montar(data_info.notifications, perfilUrl) } function montar(notifications, perfilUrl){ let htmlMsg = ''; let temCritico = ''; if(notifications.length >= 1){ $("#user-exp-alerts-wrapper").css("visibility", "visible"); let total = notifications.length; const unicNotifications = Object.values( notifications.reduce((acc, obj) => ({ ...acc, [obj.titulo]: obj }), {}) ); unicNotifications.forEach((msg, i) => { let titulo = msg.titulo? msg.titulo : msg.codTipoAlerta; let critico = msg.critico; if ('sim' === critico) { temCritico = 'sim'; } let bg = msg.cor? ' style="background-color: '+msg.cor+'"': ''; if(i<=1){ if ('sim' === critico) { htmlMsg += '
'; } else { htmlMsg += '
'; } htmlMsg += '
'; htmlMsg += '

'+titulo+'

'; htmlMsg += '
'; if(msg.mensagem != null){ htmlMsg += ''; } htmlMsg += '
'; } if(i==2){ htmlMsg += '
'; htmlMsg += '

Você tem + alertas. '; htmlMsg += 'Saiba mais...

'; } }); } $("#user-exp-alerts-wrapper").html('
' +htmlMsg); return; } function criar(userID, mostrar=false) { console.log("AbrilNotificacoes.cria notificações storage", mostrar); if (Number.isInteger(userID) !== true) { console.log("AbrilNotificacoes.cria userID invalido"); return; } sendAjax(userID, false); if(mostrar === true){ AbrilNotificacoes.init(); } } async function sendAjax(userID, visualizar=false){ if( checkCokieNotifications() && checkStorageNotifications() ){ return; } let url = 'https://us-central1-abril-id-testes-1528223718961.cloudfunctions.net/userNotifications' let url_perfil = 'https://perfil.abril.com.br/notificacoes/'; let crm_exist = true; if (assine_environment == "sandbox") { url = 'https://us-central1-abril-id-homolog.cloudfunctions.net/userNotifications'; url_perfil = 'https://gutenberg.perfil.abril.com.br/notificacoes/'; } try { console.log("Tenta acessar Notifications 1"); const response = await fetch(url_perfil + '?crm=' + userID,{ signal: AbortSignal.timeout(3000), }); if (!response.ok) { console.log("Erro Notifications 1"); console.log(response.status); } const json = await response.json(); console.log("RESPOSTA Notifications 1"); console.log(json); if( json == null){ crm_exist = false; } } catch (error) { console.log("Erro Notifications"); console.error(error.message); } /* $.ajax({ url: url_perfil + '?crm=' + userID, type: 'get', dataType: 'json', async: false, success: data2 => { if( data2 == null){ crm_exist = false; } }, }); */ if( !crm_exist ){ console.log(" CANCELA CHAMADA NA FUNCTION "); return; } let varUrl = '?codCrm=' + userID + '&show=true'; if (visualizar === true){ varUrl = '?codCrm=' + userID + '&visualizado=sim'; } try { console.log("Tenta acessar Notifications 2"); const response = await fetch(url + varUrl,{ signal: AbortSignal.timeout(3000), }); if (!response.ok) { console.log("Erro Notifications 2"); console.log(response.status); } const json = await response.json(); console.log("RESPOSTA Notifications 2"); console.log(json); if (typeof json.visualizado == "undefined") { try { console.log("Tenta acessar Notifications 3"); const response = await fetch(url + '?codCrm=' + userID,{ signal: AbortSignal.timeout(3000), }); if (!response.ok) { console.log("Erro Notifications 3"); console.log(response.status); } const json = await response.json(); console.log("RESPOSTA Notifications 3"); console.log(json); if( json == null){ salvar(json); } } catch (error) { console.log("Erro Notifications 3"); console.error(error.message); } }else{ return salvar(json); } } catch (error) { console.log("Erro Notifications"); console.error(error.message); } console.log("RESPOSTA Notifications FINALIZADA"); createCokieNotifications(); } function ler(userID){ console.log("AbrilNotificacoes.ler notificações storage"); if (Number.isInteger(userID) !== true) { console.log("AbrilNotificacoes.ler userID invalido"); return; } $("#user-exp-ctrl").attr("data-visualizado", true); $(".user-alert-icon").css("display", "none"); console.log("sava 3"); changeStorageStatusLida(); //sendAjax(userID, true); } function salvar(data){ if (!data) { return; } localStorage.setItem("abrilNotificacoes", JSON.stringify(data)); } function excluir(){ localStorage.removeItem('abrilNotificacoes'); $("#user-exp-ctrl").attr("data-visualizado", true); $(".user-alert-icon").css("display", "none"); console.log('AbrilNotificacoes.excluir'); return init(); } function convertHtml(text){ text = text.replaceAll(/</g,'<'); text = text.replaceAll(/>/g,'>'); text = text.replaceAll(/&/g,'&'); text = text.replaceAll(/"/g,'"'); return text; } function createCokieNotifications(){ var d = ''; var value = true; d = new Date(); d.setTime( d.getTime() + (1000*60*60*24)) ; expires = "expires="+ d.toUTCString(); return document.cookie = cokieName+"="+value+";" + expires + ";domain=.abril.com.br;path=/"; } function checkCokieNotifications(){ var nameEQ = cokieName + "="; var ca = document.cookie.split(";"); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == " ") c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) { return true; } } return false; } function checkStorageNotifications(){ if( localStorage.getItem(storageName) ){ return true; } return false; } function changeStorageStatusLida(){ if( data = localStorage.getItem(storageName) ){ data = JSON.parse(localStorage.getItem(storageName)); data.visualizado =true; localStorage.setItem(storageName, JSON.stringify(data)); } return ; } return { init: init, montar, montar, criar: criar, excluir: excluir, ler: ler, changeStorageStatusLida: changeStorageStatusLida, }; })();