Files
App/js/layout.js
2026-03-24 19:52:20 +00:00

87 lines
3.9 KiB
JavaScript

/* ...
AQUÍ ARRIBA ESTÁ TODO TU CÓDIGO ORIGINAL DEL LAYOUT
(El que dibuja el sidebar, el header, el menú, etc.)
... */
// ==========================================
// 🎨 INYECTOR DE LOGO CORPORATIVO (APP)
// ==========================================
setTimeout(async () => {
if (!localStorage.getItem("token")) return;
try {
// Pedimos la configuración de la empresa a tu servidor
const TRACK_API = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
? 'http://localhost:3000'
: 'https://integrarepara-api.integrarepara.es';
const res = await fetch(`${TRACK_API}/config/company`, {
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
});
const data = await res.json();
if (data.ok && data.config && data.config.company_logo) {
// Buscamos el cuadradito de la cabecera (el que tiene la inicial)
// Normalmente en tu plantilla es el div naranja con flex center
const headerIcon = document.querySelector('header .w-10.h-10.rounded-xl, #header-container .w-10.h-10');
if (headerIcon) {
// Vaciamos la letra inicial
headerIcon.innerHTML = '';
// Le quitamos el fondo de color
headerIcon.className = "w-10 h-10 flex shrink-0";
headerIcon.style.background = "transparent";
headerIcon.style.boxShadow = "none";
// Le inyectamos la imagen del logo
const img = document.createElement('img');
img.src = data.config.company_logo;
img.className = "w-full h-full object-contain drop-shadow-sm";
headerIcon.appendChild(img);
}
}
} catch (e) {
console.error("No se pudo cargar el logo de la empresa", e);
}
}, 300); // Le damos 300ms de ventaja para que el HTML se dibuje primero
// ==========================================
// RASTREADOR FANTASMA GPS (MODO DE CAMINO)
// ==========================================
setInterval(async () => {
if (!localStorage.getItem("token") || localStorage.getItem("role") !== "operario") return;
const TRACK_API = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
? 'http://localhost:3000'
: 'https://integrarepara-api.integrarepara.es';
try {
const resSt = await fetch(`${TRACK_API}/statuses`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
const dataSt = await resSt.json();
if (!dataSt.ok) return;
const caminoSt = dataSt.statuses.find(s => s.name.toLowerCase().includes('camino'));
if (!caminoSt) return;
const resSvc = await fetch(`${TRACK_API}/services/active`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
const dataSvc = await resSvc.json();
if (!dataSvc.ok) return;
const enRuta = dataSvc.services.filter(s => String(s.raw_data?.status_operativo) === String(caminoSt.id));
if (enRuta.length > 0) {
// Alta velocidad activada (enableHighAccuracy: false)
navigator.geolocation.getCurrentPosition(async (pos) => {
const lat = pos.coords.latitude;
const lng = pos.coords.longitude;
for (let s of enRuta) {
await fetch(`${TRACK_API}/services/${s.id}/location`, {
method: 'POST',
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
body: JSON.stringify({ lat, lng })
});
}
}, () => {}, { enableHighAccuracy: false, timeout: 10000, maximumAge: 30000 });
}
} catch (e) { }
}, 15000); // Se ejecuta cada 15 segundos