feat(qr): add 3-second dynamic QR auto-rotation and global settings configuration (v3.8.46)

This commit is contained in:
2026-10-28 08:11:45 -03:00
parent 54c221e20e
commit 2d1fefa856
16 changed files with 600 additions and 64 deletions
+81 -19
View File
@@ -53,6 +53,20 @@
</div>
</div>
<!-- Barra de Progreso y Cuenta Regresiva de Renovación Dinámica -->
<div class="mt-2 mb-2 px-3" style="max-width: 260px; margin: 0 auto;">
<div class="progress" style="height: 4px; background-color: rgba(180, 62, 142, 0.15); border-radius: 4px;">
<div id="modalQrProgressBar" class="progress-bar" role="progressbar" style="width: 100%; background: linear-gradient(90deg, #4338CA, #B43E8E); transition: width 0.1s linear;"></div>
</div>
<div class="d-flex justify-content-between align-items-center mt-1 text-muted small" style="font-size: 0.72rem;">
<span class="d-flex align-items-center gap-1">
<span class="spinner-grow spinner-grow-sm text-success" style="width: 6px; height: 6px;" role="status"></span>
<span class="text-success fw-semibold">Anti-captura activo</span>
</span>
<span>Renueva en <strong id="modalQrCountdown" class="font-monospace text-primary">3s</strong></span>
</div>
</div>
<!-- Información de Validez y Seguridad -->
<div class="mt-2 mb-3">
<div class="d-inline-flex align-items-center gap-1 text-success small fw-semibold">
@@ -60,7 +74,7 @@
<span>Firma Criptográfica Verificada</span>
</div>
<div class="text-muted small mt-1" style="font-size: 0.73rem;" id="modalQrValidityText">
<i class="bi bi-clock-history me-1"></i>QR Dinámico renovable cada 72hs
<i class="bi bi-arrow-repeat me-1"></i>Código dinámico con rotación cada 3 seg
</div>
</div>
@@ -68,7 +82,7 @@
<div class="d-grid gap-2">
<div class="btn-group">
<button type="button" class="btn btn-outline-secondary btn-sm py-2" onclick="refreshGlobalQrModal()" id="btnRefreshGlobalQr">
<i class="bi bi-arrow-clockwise me-1" id="globalRefreshQrIcon"></i>Actualizar Código QR
<i class="bi bi-arrow-clockwise me-1" id="globalRefreshQrIcon"></i>Rotar Ahora
</button>
<a href="/credential/my-card" class="btn btn-primary btn-sm py-2">
<i class="bi bi-person-badge me-1"></i>Ver Carnet Completo
@@ -93,7 +107,11 @@
<script>
(function() {
let qrModalInstance = null;
let cachedQrToken = null;
let timerId = null;
let renewalInterval = 3; // Segundos (default 3s configurable)
let elapsedMs = 0;
const tickIntervalMs = 100;
let isFetching = false;
function ensureQrLibrary(callback) {
if (typeof QRCode !== 'undefined') {
@@ -132,16 +150,27 @@
}
}
function loadAndRenderQR(forceRefresh) {
var spinner = document.getElementById('globalQrSpinner');
var container = document.getElementById('globalQrModalContainer');
function updateProgressUi() {
var progressBar = document.getElementById('modalQrProgressBar');
var countdownEl = document.getElementById('modalQrCountdown');
var totalMs = renewalInterval * 1000;
var remainingMs = Math.max(0, totalMs - elapsedMs);
var pct = Math.max(0, Math.min(100, (remainingMs / totalMs) * 100));
if (!forceRefresh && cachedQrToken) {
renderModalQR(cachedQrToken);
return;
if (progressBar) {
progressBar.style.width = pct + '%';
}
if (countdownEl) {
countdownEl.textContent = (remainingMs / 1000).toFixed(1) + 's';
}
}
if (container && (!qrModalInstance || forceRefresh)) {
function fetchAndRenderToken(showSpinner) {
if (isFetching) return;
isFetching = true;
var container = document.getElementById('globalQrModalContainer');
if (showSpinner && container && !qrModalInstance) {
container.innerHTML = '<div class="spinner-border text-primary spinner-border-sm" role="status"></div>';
}
@@ -149,15 +178,15 @@
.then(function(res) { return res.json(); })
.then(function(data) {
if (data && data.qr_token) {
cachedQrToken = data.qr_token;
if (data.renewal_interval_seconds && data.renewal_interval_seconds > 0) {
renewalInterval = data.renewal_interval_seconds;
}
ensureQrLibrary(function() {
renderModalQR(data.qr_token);
});
if (data.expires_in_hours) {
var valText = document.getElementById('modalQrValidityText');
if (valText) {
valText.innerHTML = '<i class="bi bi-clock-history me-1"></i>QR Dinámico renovable cada ' + data.expires_in_hours + 'hs';
}
var valText = document.getElementById('modalQrValidityText');
if (valText) {
valText.innerHTML = '<i class="bi bi-arrow-repeat me-1"></i>Código dinámico con rotación cada ' + renewalInterval + ' seg';
}
} else {
throw new Error('Token no disponible');
@@ -169,23 +198,56 @@
ensureQrLibrary(function() {
renderModalQR(fallback);
});
})
.finally(function() {
isFetching = false;
elapsedMs = 0;
updateProgressUi();
});
}
function startAutoRefreshLoop() {
stopAutoRefreshLoop();
elapsedMs = 0;
updateProgressUi();
fetchAndRenderToken(false);
timerId = setInterval(function() {
elapsedMs += tickIntervalMs;
updateProgressUi();
if (elapsedMs >= renewalInterval * 1000) {
elapsedMs = 0;
fetchAndRenderToken(false);
}
}, tickIntervalMs);
}
function stopAutoRefreshLoop() {
if (timerId) {
clearInterval(timerId);
timerId = null;
}
}
window.refreshGlobalQrModal = function() {
var icon = document.getElementById('globalRefreshQrIcon');
if (icon) icon.classList.add('spin-animation');
loadAndRenderQR(true);
elapsedMs = 0;
fetchAndRenderToken(false);
setTimeout(function() {
if (icon) icon.classList.remove('spin-animation');
}, 800);
}, 600);
};
document.addEventListener('DOMContentLoaded', function() {
var modalEl = document.getElementById('userQrCredentialModal');
if (modalEl) {
modalEl.addEventListener('show.bs.modal', function() {
loadAndRenderQR(false);
startAutoRefreshLoop();
});
modalEl.addEventListener('hide.bs.modal', function() {
stopAutoRefreshLoop();
});
}
});