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
+116 -11
View File
@@ -53,10 +53,24 @@
</div>
<!-- Código QR Institucional -->
<div class="p-3 bg-white rounded-4 shadow-sm d-inline-block mb-3" style="min-width: 200px; min-height: 200px;">
<div class="p-3 bg-white rounded-4 shadow-sm d-inline-block mb-2" style="min-width: 200px; min-height: 200px;">
<div id="qrcode" class="d-flex justify-content-center align-items-center"></div>
</div>
<!-- Barra de Progreso y Cuenta Regresiva de Renovación Dinámica -->
<div class="mb-3 px-3" style="max-width: 240px; margin: 0 auto;">
<div class="progress" style="height: 4px; background-color: rgba(255, 255, 255, 0.2); border-radius: 4px;">
<div id="cardQrProgressBar" class="progress-bar bg-white" role="progressbar" style="width: 100%; transition: width 0.1s linear;"></div>
</div>
<div class="d-flex justify-content-between align-items-center mt-1 text-white-50 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-white fw-semibold">Anti-captura activo</span>
</span>
<span class="text-white">Renueva en <strong id="cardQrCountdown" class="font-monospace text-white">3s</strong></span>
</div>
</div>
<div class="d-flex justify-content-center align-items-center gap-3 text-start small opacity-90 mb-2">
<div>
<span class="d-block text-uppercase" style="font-size: 0.65rem;">Documento</span>
@@ -72,8 +86,8 @@
<!-- Footer de la Credencial -->
<div class="p-3 bg-black bg-opacity-25 border-top border-white border-opacity-10 d-flex align-items-center justify-content-between text-white-50 small">
<span><i class="bi bi-clock-history me-1"></i> QR renovable</span>
<span>Validez: {{ expiresInHours }}hs</span>
<span id="cardRenewalText"><i class="bi bi-arrow-repeat me-1"></i> QR dinámico ({{ renewalIntervalSeconds or 3 }}s)</span>
<span>Validez institucional</span>
</div>
</div>
@@ -104,16 +118,23 @@
<!-- Librería QRCode.js via CDN -->
<script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const qrContainer = document.getElementById('qrcode');
const token = "{{ qrToken }}";
const fallbackText = "UNICABA:USR:{{ user.id }}:DNI:{{ user.document_number or user.email }}";
const textToEncode = token && token.length > 10 ? token : fallbackText;
(function() {
var qrInstance = null;
var timerId = null;
var renewalInterval = parseInt("{{ renewalIntervalSeconds or 3 }}", 10) || 3;
var elapsedMs = 0;
var tickIntervalMs = 100;
var isFetching = false;
var initialToken = "{{ qrToken }}";
var fallbackText = "UNICABA:USR:{{ user.id }}:DNI:{{ user.document_number or user.email }}";
function drawQR(text) {
var qrContainer = document.getElementById('qrcode');
if (!qrContainer) return;
qrContainer.innerHTML = '';
try {
new QRCode(qrContainer, {
text: textToEncode,
qrInstance = new QRCode(qrContainer, {
text: text,
width: 180,
height: 180,
colorDark: "#0F172A",
@@ -124,6 +145,90 @@
console.error('Error generando QR:', e);
qrContainer.innerHTML = '<div class="alert alert-warning p-2 small">Código disponible en modo texto</div>';
}
}
function updateProgressUi() {
var progressBar = document.getElementById('cardQrProgressBar');
var countdownEl = document.getElementById('cardQrCountdown');
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() {
if (isFetching) return;
isFetching = true;
fetch('/credential/token')
.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);
var textEl = document.getElementById('cardRenewalText');
if (textEl) {
textEl.innerHTML = '<i class="bi bi-arrow-repeat me-1"></i> QR dinámico (' + renewalInterval + 's)';
}
} else {
drawQR(fallbackText);
}
})
.catch(function(err) {
console.warn('Error renovando QR de carnet:', err);
drawQR(fallbackText);
})
.finally(function() {
isFetching = false;
elapsedMs = 0;
updateProgressUi();
});
}
function startLoop() {
stopLoop();
elapsedMs = 0;
updateProgressUi();
timerId = setInterval(function() {
elapsedMs += tickIntervalMs;
updateProgressUi();
if (elapsedMs >= renewalInterval * 1000) {
elapsedMs = 0;
fetchNewToken();
}
}, tickIntervalMs);
}
function stopLoop() {
if (timerId) {
clearInterval(timerId);
timerId = null;
}
}
document.addEventListener('DOMContentLoaded', function() {
var textToEncode = (initialToken && initialToken.length > 10) ? initialToken : fallbackText;
drawQR(textToEncode);
startLoop();
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
stopLoop();
} else {
startLoop();
}
});
});
})();
</script>
{% endblock %}