fix(credential): desacople modal qr vs carnet completo, dni y usuario en perfil, y resolucion robusta en dni parser v3.8.54
This commit is contained in:
@@ -52,9 +52,9 @@
|
||||
<span class="badge bg-primary-subtle text-primary border border-primary-subtle font-monospace text-uppercase" style="font-size: 0.74rem;">
|
||||
{{ auth_user.role_badge_display.label if (auth_user and auth_user.role_badge_display) else (auth_user.role|upper if auth_user else 'ALUMNO') }}
|
||||
</span>
|
||||
{% if auth_user and auth_user.document_number %}
|
||||
{% if auth_user and (auth_user.document_number or auth_user.dni) %}
|
||||
<span class="badge bg-body-tertiary text-body-secondary border font-monospace" style="font-size: 0.74rem;">
|
||||
DNI: {{ auth_user.document_number }}
|
||||
DNI: {{ auth_user.document_number or auth_user.dni }}
|
||||
</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
@@ -99,7 +99,7 @@
|
||||
|
||||
<!-- Botón de Acción -->
|
||||
<div class="d-grid">
|
||||
<a href="/credential/my-card" class="btn btn-primary py-2 rounded-3 shadow-xs d-flex align-items-center justify-content-center">
|
||||
<a href="/credential/my-card" id="modalBtnVerCarnet" class="btn btn-primary py-2 rounded-3 shadow-xs d-flex align-items-center justify-content-center">
|
||||
<i class="bi bi-person-badge me-2"></i>Ver Carnet Completo
|
||||
</a>
|
||||
</div>
|
||||
@@ -122,54 +122,50 @@
|
||||
(function() {
|
||||
let qrModalInstance = null;
|
||||
let timerId = null;
|
||||
let renewalInterval = 3; // Segundos (default 3s configurable)
|
||||
let renewalInterval = 3;
|
||||
let elapsedMs = 0;
|
||||
const tickIntervalMs = 100;
|
||||
let isFetching = false;
|
||||
|
||||
function ensureQrLibrary(callback) {
|
||||
if (typeof QRCode !== 'undefined') {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
var script = document.createElement('script');
|
||||
script.src = 'https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js';
|
||||
script.onload = callback;
|
||||
script.onerror = function() {
|
||||
var container = document.getElementById('globalQrModalContainer');
|
||||
if (container) {
|
||||
container.innerHTML = '<div class="alert alert-warning p-2 small">Error al cargar librería QR</div>';
|
||||
}
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
// Detectar si la página activa es /credential/my-card
|
||||
const isCarnetPage = window.location.pathname.includes('/credential/my-card') ||
|
||||
window.location.pathname.endsWith('/credential') ||
|
||||
window.location.pathname.includes('/mi-credencial');
|
||||
|
||||
function renderModalQR(token) {
|
||||
var container = document.getElementById('globalQrModalContainer');
|
||||
const container = document.getElementById('globalQrModalContainer');
|
||||
if (!container) return;
|
||||
container.innerHTML = '';
|
||||
|
||||
try {
|
||||
qrModalInstance = new QRCode(container, {
|
||||
text: token,
|
||||
width: 180,
|
||||
height: 180,
|
||||
colorDark: "#0F172A",
|
||||
colorLight: "#FFFFFF",
|
||||
correctLevel: QRCode.CorrectLevel.M
|
||||
});
|
||||
if (qrModalInstance && typeof qrModalInstance.makeCode === 'function') {
|
||||
qrModalInstance.makeCode(token);
|
||||
} else {
|
||||
container.innerHTML = '';
|
||||
if (typeof QRCode !== 'undefined') {
|
||||
qrModalInstance = new QRCode(container, {
|
||||
text: token,
|
||||
width: 180,
|
||||
height: 180,
|
||||
colorDark: "#0F172A",
|
||||
colorLight: "#FFFFFF",
|
||||
correctLevel: QRCode.CorrectLevel.M
|
||||
});
|
||||
} else {
|
||||
container.innerHTML = '<div class="alert alert-warning p-2 small">Código disponible: ' + token.substring(0, 16) + '...</div>';
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error generando QR en modal:', err);
|
||||
container.innerHTML = '<div class="alert alert-danger p-2 small">Error generando código QR</div>';
|
||||
container.innerHTML = '<div class="alert alert-secondary p-2 small font-monospace">Credencial institucional activa</div>';
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
const progressBar = document.getElementById('modalQrProgressBar');
|
||||
const countdownEl = document.getElementById('modalQrCountdown');
|
||||
const totalMs = renewalInterval * 1000;
|
||||
const remainingMs = Math.max(0, totalMs - elapsedMs);
|
||||
const pct = Math.max(0, Math.min(100, (remainingMs / totalMs) * 100));
|
||||
|
||||
if (progressBar) {
|
||||
progressBar.style.width = pct + '%';
|
||||
@@ -179,26 +175,22 @@
|
||||
}
|
||||
}
|
||||
|
||||
function fetchAndRenderToken(showSpinner) {
|
||||
function fetchAndRenderToken() {
|
||||
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>';
|
||||
}
|
||||
|
||||
fetch('/credential/token')
|
||||
.then(function(res) { return res.json(); })
|
||||
.then(function(res) {
|
||||
if (!res.ok) throw new Error('HTTP ' + res.status);
|
||||
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;
|
||||
}
|
||||
ensureQrLibrary(function() {
|
||||
renderModalQR(data.qr_token);
|
||||
});
|
||||
var valText = document.getElementById('modalQrValidityText');
|
||||
renderModalQR(data.qr_token);
|
||||
const 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';
|
||||
}
|
||||
@@ -208,10 +200,8 @@
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.warn('Fallback a token local para QR modal:', err);
|
||||
var fallback = "UNICABA:USR:{{ auth_user.id if auth_user else 0 }}:DNI:{{ auth_user.document_number or auth_user.dni or auth_user.id or 'N/A' }}";
|
||||
ensureQrLibrary(function() {
|
||||
renderModalQR(fallback);
|
||||
});
|
||||
const fallback = "UNICABA:USR:{{ auth_user.id if auth_user else 0 }}:DNI:{{ auth_user.document_number or auth_user.dni or auth_user.id or 'N/A' }}";
|
||||
renderModalQR(fallback);
|
||||
})
|
||||
.finally(function() {
|
||||
isFetching = false;
|
||||
@@ -224,7 +214,7 @@
|
||||
stopAutoRefreshLoop();
|
||||
elapsedMs = 0;
|
||||
updateProgressUi();
|
||||
fetchAndRenderToken(false);
|
||||
fetchAndRenderToken();
|
||||
|
||||
timerId = setInterval(function() {
|
||||
elapsedMs += tickIntervalMs;
|
||||
@@ -232,7 +222,7 @@
|
||||
|
||||
if (elapsedMs >= renewalInterval * 1000) {
|
||||
elapsedMs = 0;
|
||||
fetchAndRenderToken(false);
|
||||
fetchAndRenderToken();
|
||||
}
|
||||
}, tickIntervalMs);
|
||||
}
|
||||
@@ -242,63 +232,72 @@
|
||||
clearInterval(timerId);
|
||||
timerId = null;
|
||||
}
|
||||
qrModalInstance = null;
|
||||
}
|
||||
|
||||
window.refreshGlobalQrModal = function() {
|
||||
var icon = document.getElementById('globalRefreshQrIcon');
|
||||
if (icon) icon.classList.add('spin-animation');
|
||||
elapsedMs = 0;
|
||||
fetchAndRenderToken(false);
|
||||
setTimeout(function() {
|
||||
if (icon) icon.classList.remove('spin-animation');
|
||||
}, 600);
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var modalEl = document.getElementById('userQrCredentialModal');
|
||||
if (modalEl) {
|
||||
// Impedir que eventos de scroll de rueda o táctiles se propaguen a la página de fondo
|
||||
modalEl.addEventListener('wheel', function(e) {
|
||||
if (e.target === modalEl) {
|
||||
const modalEl = document.getElementById('userQrCredentialModal');
|
||||
if (!modalEl) return;
|
||||
|
||||
// Desacople total si estamos en la vista de carnet completo:
|
||||
if (isCarnetPage) {
|
||||
// El usuario ya está viendo su credencial en pantalla completa:
|
||||
// Interceptamos los botones de barra y menú para evitar solapamientos y bloqueos
|
||||
document.querySelectorAll('[data-bs-target="#userQrCredentialModal"]').forEach(function(btn) {
|
||||
btn.removeAttribute('data-bs-toggle');
|
||||
btn.removeAttribute('data-bs-target');
|
||||
btn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
}
|
||||
}, { passive: false });
|
||||
|
||||
modalEl.addEventListener('touchmove', function(e) {
|
||||
if (e.target === modalEl) {
|
||||
e.preventDefault();
|
||||
}
|
||||
}, { passive: false });
|
||||
|
||||
modalEl.addEventListener('show.bs.modal', function() {
|
||||
// Bloqueo total de scroll en el elemento raíz (html) y cuerpo (body)
|
||||
document.documentElement.classList.add('modal-open');
|
||||
document.body.classList.add('modal-open');
|
||||
document.documentElement.style.overflow = 'hidden';
|
||||
document.body.style.overflow = 'hidden';
|
||||
|
||||
// Si el sidebar móvil estuviera desplegado, cerrarlo para evitar solapamientos
|
||||
if (typeof window.closeMobileSidebar === 'function') {
|
||||
window.closeMobileSidebar();
|
||||
} else {
|
||||
var sb = document.querySelector('.sidebar.mobile-open');
|
||||
if (sb) sb.classList.remove('mobile-open');
|
||||
var bd = document.getElementById('sidebarBackdrop');
|
||||
if (bd) bd.classList.remove('show');
|
||||
}
|
||||
|
||||
startAutoRefreshLoop();
|
||||
// Scroll suave y destello elegante en la credencial ya visible
|
||||
const card = document.querySelector('.card[style*="linear-gradient"]');
|
||||
if (card) {
|
||||
card.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||
card.style.transform = 'scale(1.02)';
|
||||
card.style.transition = 'transform 0.25s ease, box-shadow 0.25s ease';
|
||||
card.style.boxShadow = '0 0 25px rgba(180, 62, 142, 0.6)';
|
||||
setTimeout(function() {
|
||||
card.style.transform = '';
|
||||
card.style.boxShadow = '';
|
||||
}, 600);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
modalEl.addEventListener('hidden.bs.modal', function() {
|
||||
// Restaurar scroll de página
|
||||
document.documentElement.classList.remove('modal-open');
|
||||
document.body.classList.remove('modal-open');
|
||||
document.documentElement.style.overflow = '';
|
||||
document.body.style.overflow = '';
|
||||
stopAutoRefreshLoop();
|
||||
// Si por algún enlace previo o history el modal estuviese abierto, cerrarlo inmediatamente
|
||||
const existingModal = bootstrap.Modal.getInstance(modalEl);
|
||||
if (existingModal) {
|
||||
existingModal.hide();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Si estamos en cualquier otra página del sistema:
|
||||
const btnVerCarnet = document.getElementById('modalBtnVerCarnet');
|
||||
if (btnVerCarnet) {
|
||||
btnVerCarnet.addEventListener('click', function() {
|
||||
const inst = bootstrap.Modal.getInstance(modalEl);
|
||||
if (inst) inst.hide();
|
||||
});
|
||||
}
|
||||
|
||||
// Utilizar shown.bs.modal (cuando la animación fade terminó y el modal es 100% visible)
|
||||
modalEl.addEventListener('shown.bs.modal', function() {
|
||||
document.documentElement.classList.add('modal-open');
|
||||
document.body.classList.add('modal-open');
|
||||
startAutoRefreshLoop();
|
||||
});
|
||||
|
||||
// Limpieza innegociable al ocultar el modal:
|
||||
modalEl.addEventListener('hidden.bs.modal', function() {
|
||||
stopAutoRefreshLoop();
|
||||
document.documentElement.classList.remove('modal-open');
|
||||
document.body.classList.remove('modal-open');
|
||||
document.documentElement.style.overflow = '';
|
||||
document.body.style.overflow = '';
|
||||
document.querySelectorAll('.modal-backdrop').forEach(function(b) {
|
||||
b.remove();
|
||||
});
|
||||
});
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user