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
+95 -15
View File
@@ -73,8 +73,23 @@
</div>
</div>
</div>
<div class="mt-2 text-muted small" style="font-size: 0.72rem;" id="profileQrValidity">
<i class="bi bi-clock-history me-1"></i>QR Dinámico renovable ({{ expiresInHours or 72 }}hs)
<!-- Barra de Progreso y Cuenta Regresiva de Renovación Dinámica -->
<div class="mt-2 mb-2 px-2" style="max-width: 220px; margin: 0 auto;">
<div class="progress" style="height: 4px; background-color: rgba(180, 62, 142, 0.15); border-radius: 4px;">
<div id="profileQrProgressBar" 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.70rem;">
<span class="d-flex align-items-center gap-1">
<span class="spinner-grow spinner-grow-sm text-success" style="width: 5px; height: 5px;" role="status"></span>
<span class="text-success fw-semibold">Anti-captura</span>
</span>
<span>Renueva en <strong id="profileQrCountdown" class="font-monospace text-primary">3s</strong></span>
</div>
</div>
<div class="mt-1 text-muted small" style="font-size: 0.72rem;" id="profileQrValidity">
<i class="bi bi-arrow-repeat me-1"></i>Código dinámico con rotación cada 3 seg
</div>
</div>
<div class="col-md-7 col-lg-8">
@@ -307,6 +322,11 @@
<script>
(function() {
var profileQrInstance = null;
var timerId = null;
var renewalInterval = 3; // Segundos (default 3s configurable)
var elapsedMs = 0;
var tickIntervalMs = 100;
var isFetching = false;
var initialToken = "{{ qrToken }}";
var fallbackToken = "UNICABA:USR:{{ user.id }}:DNI:{{ user.document_number or user.email }}";
@@ -329,11 +349,27 @@
}
}
window.refreshProfileQrCode = function() {
var icon = document.getElementById('profileRefreshQrIcon');
function updateProgressUi() {
var progressBar = document.getElementById('profileQrProgressBar');
var countdownEl = document.getElementById('profileQrCountdown');
var totalMs = renewalInterval * 1000;
var remainingMs = Math.max(0, totalMs - elapsedMs);
var pct = Math.max(0, Math.min(100, (remainingMs / totalMs) * 100));
if (progressBar) {
progressBar.style.width = pct + '%';
}
if (countdownEl) {
countdownEl.textContent = (remainingMs / 1000).toFixed(1) + 's';
}
}
function fetchNewToken(showSpinner) {
if (isFetching) return;
isFetching = true;
var container = document.getElementById('profileQrCodeDisplay');
if (icon) icon.classList.add('spin-animation');
if (container) {
if (showSpinner && container && !profileQrInstance) {
container.innerHTML = '<div class="spinner-border text-primary spinner-border-sm" role="status"></div>';
}
@@ -341,31 +377,75 @@
.then(function(res) { return res.json(); })
.then(function(data) {
if (data && data.qr_token) {
if (data.renewal_interval_seconds && data.renewal_interval_seconds > 0) {
renewalInterval = data.renewal_interval_seconds;
}
drawQR(data.qr_token);
if (data.expires_in_hours) {
var valEl = document.getElementById('profileQrValidity');
if (valEl) {
valEl.innerHTML = '<i class="bi bi-clock-history me-1"></i>QR Dinámico renovable (' + data.expires_in_hours + 'hs)';
}
var valEl = document.getElementById('profileQrValidity');
if (valEl) {
valEl.innerHTML = '<i class="bi bi-arrow-repeat me-1"></i>Código dinámico con rotación cada ' + renewalInterval + ' seg';
}
} else {
drawQR(fallbackToken);
}
})
.catch(function(err) {
console.warn('Error refrescando token:', err);
console.warn('Error refrescando token en perfil:', err);
drawQR(fallbackToken);
})
.finally(function() {
if (icon) {
setTimeout(function() { icon.classList.remove('spin-animation'); }, 700);
}
isFetching = false;
elapsedMs = 0;
updateProgressUi();
});
}
function startLoop() {
stopLoop();
elapsedMs = 0;
updateProgressUi();
fetchNewToken(false);
timerId = setInterval(function() {
elapsedMs += tickIntervalMs;
updateProgressUi();
if (elapsedMs >= renewalInterval * 1000) {
elapsedMs = 0;
fetchNewToken(false);
}
}, tickIntervalMs);
}
function stopLoop() {
if (timerId) {
clearInterval(timerId);
timerId = null;
}
}
window.refreshProfileQrCode = function() {
var icon = document.getElementById('profileRefreshQrIcon');
if (icon) icon.classList.add('spin-animation');
elapsedMs = 0;
fetchNewToken(false);
setTimeout(function() {
if (icon) icon.classList.remove('spin-animation');
}, 600);
};
document.addEventListener('DOMContentLoaded', function() {
var tokenToUse = (initialToken && initialToken.length > 10) ? initialToken : fallbackToken;
drawQR(tokenToUse);
startLoop();
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
stopLoop();
} else {
startLoop();
}
});
});
})();
</script>