feat(qr): add dynamic QR credential to user profile and global access icon next to theme button (v3.8.45)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "frontend",
|
||||
"version": "3.8.44",
|
||||
"version": "3.8.45",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
+3
-3
@@ -86,9 +86,9 @@ const env = nunjucks.configure(path.join(__dirname, '../views'), {
|
||||
});
|
||||
|
||||
// Versiones institucionales del sistema
|
||||
const APP_VERSION = '3.8.44';
|
||||
const FRONTEND_VERSION = '3.8.44';
|
||||
const BACKEND_VERSION = '3.8.44';
|
||||
const APP_VERSION = '3.8.45';
|
||||
const FRONTEND_VERSION = '3.8.45';
|
||||
const BACKEND_VERSION = '3.8.45';
|
||||
|
||||
env.addGlobal('app_version', APP_VERSION);
|
||||
env.addGlobal('frontend_version', FRONTEND_VERSION);
|
||||
|
||||
@@ -241,18 +241,30 @@ router.post(['/change_password', '/change-password'], requireAuth, async (req, r
|
||||
// Mi Perfil de Usuario (GET)
|
||||
router.get('/profile', requireAuth, async (req, res) => {
|
||||
let userProfile = req.user || {};
|
||||
let qrToken = '';
|
||||
let expiresInHours = 72;
|
||||
|
||||
try {
|
||||
const resp = await req.apiClient.get('/auth/profile');
|
||||
if (resp.data && resp.data.user) {
|
||||
userProfile = { ...userProfile, ...resp.data.user };
|
||||
const [profResp, qrResp] = await Promise.allSettled([
|
||||
req.apiClient.get('/auth/profile'),
|
||||
req.apiClient.get('/reception/my-qr-token')
|
||||
]);
|
||||
if (profResp.status === 'fulfilled' && profResp.value?.data?.user) {
|
||||
userProfile = { ...userProfile, ...profResp.value.data.user };
|
||||
}
|
||||
if (qrResp.status === 'fulfilled' && qrResp.value?.data?.qr_token) {
|
||||
qrToken = qrResp.value.data.qr_token;
|
||||
expiresInHours = qrResp.value.data.expires_in_hours || 72;
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Could not fetch latest user profile from API:', err.message);
|
||||
console.warn('Profile data load notice:', err.message);
|
||||
}
|
||||
|
||||
res.render('auth/profile', {
|
||||
title: 'Mi Perfil — Edu-Space',
|
||||
user: userProfile,
|
||||
qrToken,
|
||||
expiresInHours,
|
||||
success: req.query.success || null,
|
||||
error: req.query.error || null
|
||||
});
|
||||
|
||||
@@ -26,4 +26,24 @@ router.get(['/my-card', '/', '/mi-credencial'], async (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/token', async (req, res) => {
|
||||
try {
|
||||
const resp = await req.apiClient.get('/reception/my-qr-token');
|
||||
return res.json(resp.data);
|
||||
} catch (err) {
|
||||
console.warn('API error fetching QR token:', err.message);
|
||||
// Fallback token if reception endpoint fails or is offline
|
||||
const fallbackToken = `UNICABA:USR:${req.user?.id || 0}:DNI:${req.user?.document_number || req.user?.email || 'N/A'}`;
|
||||
return res.json({
|
||||
status: 'fallback',
|
||||
qr_token: fallbackToken,
|
||||
user_id: req.user?.id || 0,
|
||||
user_name: req.user?.name || '',
|
||||
user_dni: req.user?.document_number || '',
|
||||
user_role: (req.user?.role || 'ALUMNO').toUpperCase(),
|
||||
expires_in_hours: 72
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -49,6 +49,78 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<!-- Credencial Digital Universitaria y QR Dinámico -->
|
||||
<div class="card border-0 shadow-sm mb-4 overflow-hidden" style="border-radius: 14px;">
|
||||
<div class="p-3 px-4 d-flex align-items-center justify-content-between text-white"
|
||||
style="background: linear-gradient(135deg, #1E1B4B 0%, #4338CA 50%, #B43E8E 100%);">
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<img src="/img/logo_unicaba_mini.png" alt="UniCABA" height="24" class="bg-white rounded-circle p-1 shadow-xs">
|
||||
<h5 class="card-title mb-0 h6 fw-bold text-white text-uppercase tracking-wider">
|
||||
Credencial Universitaria · Acceso QR Dinámico
|
||||
</h5>
|
||||
</div>
|
||||
<span class="badge bg-success-subtle text-success border border-success-subtle rounded-pill px-2 py-1 small fw-semibold">
|
||||
● ACTIVO
|
||||
</span>
|
||||
</div>
|
||||
<div class="card-body p-4">
|
||||
<div class="row align-items-center g-4">
|
||||
<div class="col-md-5 col-lg-4 text-center">
|
||||
<div class="p-3 bg-body-tertiary border rounded-4 d-inline-block shadow-xs position-relative" style="background-color: var(--bs-body-bg) !important;">
|
||||
<div id="profileQrCodeDisplay" class="d-flex align-items-center justify-content-center" style="min-width: 180px; min-height: 180px;">
|
||||
<div class="spinner-border text-primary spinner-border-sm" role="status">
|
||||
<span class="visually-hidden">Generando QR...</span>
|
||||
</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)
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-7 col-lg-8">
|
||||
<div class="d-flex align-items-center gap-2 flex-wrap mb-2">
|
||||
<h5 class="fw-bold mb-0 text-body">{{ user.name or (user.first_name ~ ' ' ~ user.last_name) }}</h5>
|
||||
<span class="badge bg-primary-subtle text-primary border border-primary-subtle font-monospace text-uppercase" style="font-size: 0.72rem;">
|
||||
{{ user.role }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="text-muted small mb-3">
|
||||
<i class="bi bi-envelope me-1"></i>{{ user.email }}
|
||||
</div>
|
||||
|
||||
<div class="p-3 bg-body-tertiary rounded-3 mb-3 small">
|
||||
<div class="row g-2">
|
||||
<div class="col-sm-6">
|
||||
<span class="text-muted d-block" style="font-size: 0.7rem;">Documento Nacional / DNI</span>
|
||||
<span class="fw-bold font-monospace">{{ user.document_number or 'Sin registrar' }}</span>
|
||||
</div>
|
||||
<div class="col-sm-6">
|
||||
<span class="text-muted d-block" style="font-size: 0.7rem;">ID de Matrícula Institucional</span>
|
||||
<span class="fw-bold font-monospace">#{{ user.id }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="text-muted small mb-3">
|
||||
Este código QR posee firma criptográfica HMAC y se actualiza periódicamente para garantizar la autenticidad e intransferibilidad de tu acceso a sedes y descuentos comerciales.
|
||||
</p>
|
||||
|
||||
<div class="d-flex gap-2 flex-wrap">
|
||||
<a href="/credential/my-card" class="btn btn-primary btn-sm px-3 shadow-xs">
|
||||
<i class="bi bi-person-badge me-1"></i>Ver Carnet Completo
|
||||
</a>
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm px-3 shadow-xs" onclick="refreshProfileQrCode()">
|
||||
<i class="bi bi-arrow-clockwise me-1" id="profileRefreshQrIcon"></i>Actualizar Código QR
|
||||
</button>
|
||||
<a href="/benefits" class="btn btn-outline-primary btn-sm px-3 shadow-xs">
|
||||
<i class="bi bi-tags me-1"></i>Ver Beneficios Activos
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Profile Card -->
|
||||
<div class="card border-0 shadow-sm mb-4" style="border-radius: 12px;">
|
||||
<div class="card-body p-4">
|
||||
@@ -229,4 +301,72 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Librería QRCode.js via CDN y Lógica de Renderizado Dinámico -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
|
||||
<script>
|
||||
(function() {
|
||||
var profileQrInstance = null;
|
||||
var initialToken = "{{ qrToken }}";
|
||||
var fallbackToken = "UNICABA:USR:{{ user.id }}:DNI:{{ user.document_number or user.email }}";
|
||||
|
||||
function drawQR(text) {
|
||||
var container = document.getElementById('profileQrCodeDisplay');
|
||||
if (!container) return;
|
||||
container.innerHTML = '';
|
||||
try {
|
||||
profileQrInstance = new QRCode(container, {
|
||||
text: text,
|
||||
width: 180,
|
||||
height: 180,
|
||||
colorDark: "#0F172A",
|
||||
colorLight: "#FFFFFF",
|
||||
correctLevel: QRCode.CorrectLevel.M
|
||||
});
|
||||
} catch(e) {
|
||||
console.error('Error dibujando QR en perfil:', e);
|
||||
container.innerHTML = '<div class="alert alert-warning p-2 small">Error generando código QR</div>';
|
||||
}
|
||||
}
|
||||
|
||||
window.refreshProfileQrCode = function() {
|
||||
var icon = document.getElementById('profileRefreshQrIcon');
|
||||
var container = document.getElementById('profileQrCodeDisplay');
|
||||
if (icon) icon.classList.add('spin-animation');
|
||||
if (container) {
|
||||
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(data) {
|
||||
if (data && data.qr_token) {
|
||||
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)';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
drawQR(fallbackToken);
|
||||
}
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.warn('Error refrescando token:', err);
|
||||
drawQR(fallbackToken);
|
||||
})
|
||||
.finally(function() {
|
||||
if (icon) {
|
||||
setTimeout(function() { icon.classList.remove('spin-animation'); }, 700);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var tokenToUse = (initialToken && initialToken.length > 10) ? initialToken : fallbackToken;
|
||||
drawQR(tokenToUse);
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
@@ -917,6 +917,67 @@
|
||||
transform: rotate(20deg) scale(1.18);
|
||||
}
|
||||
|
||||
/* --- BOTÓN DE CREDENCIAL QR JUNTO AL BOTÓN DE TEMA --- */
|
||||
.qr-credential-btn {
|
||||
border: 1px solid rgba(180, 62, 142, 0.2);
|
||||
background: rgba(180, 62, 142, 0.06);
|
||||
color: #B43E8E;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
width: 40px;
|
||||
height: 38px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
border-radius: 8px;
|
||||
}
|
||||
.qr-credential-btn:hover {
|
||||
background: rgba(180, 62, 142, 0.16);
|
||||
border-color: rgba(180, 62, 142, 0.4);
|
||||
color: #8C256B;
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
[data-bs-theme="dark"] .qr-credential-btn {
|
||||
border-color: rgba(255, 255, 255, 0.12);
|
||||
background: rgba(255, 255, 255, 0.06);
|
||||
color: #F1F5F9;
|
||||
}
|
||||
[data-bs-theme="dark"] .qr-credential-btn:hover {
|
||||
background: rgba(255, 138, 182, 0.18);
|
||||
border-color: rgba(255, 138, 182, 0.4);
|
||||
color: #FF8AB6;
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
.sidebar.collapsed .qr-credential-btn {
|
||||
width: 44px !important;
|
||||
height: 44px !important;
|
||||
padding: 0 !important;
|
||||
border-radius: 12px !important;
|
||||
display: inline-flex !important;
|
||||
align-items: center !important;
|
||||
justify-content: center !important;
|
||||
margin: 0 auto !important;
|
||||
}
|
||||
.sidebar.collapsed .theme-and-qr-container {
|
||||
flex-direction: column !important;
|
||||
align-items: center !important;
|
||||
gap: 6px !important;
|
||||
}
|
||||
.mobile-header .qr-credential-btn {
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
@keyframes spinAround {
|
||||
from { transform: rotate(0deg); }
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
.spin-animation {
|
||||
animation: spinAround 0.8s linear infinite;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
/* =======================================================
|
||||
NAVEGACIÓN DEL SIDEBAR (UX/UI Premium & Calidad Industrial)
|
||||
======================================================= */
|
||||
@@ -1760,14 +1821,24 @@
|
||||
{% include 'partials/language_switcher.html' %}
|
||||
</div>
|
||||
|
||||
<!-- Botón de Tema: Situado abajo, directamente SOBRE el botón de usuario -->
|
||||
<div class="theme-toggle-container mb-2">
|
||||
<button class="theme-toggle-btn w-100 d-flex align-items-center justify-content-between px-3 py-2 border-0 rounded-3 shadow-none" type="button" aria-label="Cambiar tema" title="Modo Claro / Oscuro">
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<i class="bi bi-moon-stars-fill theme-icon fs-6"></i>
|
||||
<span class="sidebar-footer-text small fw-medium theme-label">Modo Claro</span>
|
||||
</div>
|
||||
<span class="sidebar-footer-text badge bg-secondary-subtle text-secondary-emphasis rounded-pill px-2 py-0" style="font-size:0.68rem;">Auto</span>
|
||||
<!-- Botón de Tema y Botón de Credencial QR: Situados abajo, directamente SOBRE el botón de usuario -->
|
||||
<div class="theme-and-qr-container d-flex align-items-center gap-2 mb-2">
|
||||
<div class="theme-toggle-container flex-grow-1">
|
||||
<button class="theme-toggle-btn w-100 d-flex align-items-center justify-content-between px-3 py-2 border-0 rounded-3 shadow-none" type="button" aria-label="Cambiar tema" title="Modo Claro / Oscuro">
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<i class="bi bi-moon-stars-fill theme-icon fs-6"></i>
|
||||
<span class="sidebar-footer-text small fw-medium theme-label">Modo Claro</span>
|
||||
</div>
|
||||
<span class="sidebar-footer-text badge bg-secondary-subtle text-secondary-emphasis rounded-pill px-2 py-0" style="font-size:0.68rem;">Auto</span>
|
||||
</button>
|
||||
</div>
|
||||
<button class="qr-credential-btn border-0 rounded-3 shadow-none"
|
||||
type="button"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#userQrCredentialModal"
|
||||
aria-label="Mi Credencial QR"
|
||||
title="Ver Mi Credencial Digital QR">
|
||||
<i class="bi bi-qr-code-scan fs-6"></i>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -1795,6 +1866,12 @@
|
||||
</span>
|
||||
</div>
|
||||
<ul class="user-menu-list p-1 m-0 list-unstyled">
|
||||
<li>
|
||||
<a class="nav-link py-2 px-3 d-flex align-items-center rounded-2" href="#" data-bs-toggle="modal" data-bs-target="#userQrCredentialModal">
|
||||
<i class="bi bi-qr-code-scan text-primary me-2 fs-6"></i>
|
||||
<span>Mi Credencial QR</span>
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a class="nav-link py-2 px-3 d-flex align-items-center rounded-2" href="/auth/profile">
|
||||
<i class="bi bi-person text-primary me-2 fs-6"></i>
|
||||
@@ -1840,6 +1917,9 @@
|
||||
<span class="brand-text fw-bold fs-6">Edu-Space</span>
|
||||
</a>
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<button class="qr-credential-btn" type="button" data-bs-toggle="modal" data-bs-target="#userQrCredentialModal" aria-label="Mi Credencial QR" title="Mi Credencial Digital QR">
|
||||
<i class="bi bi-qr-code-scan"></i>
|
||||
</button>
|
||||
<button class="theme-toggle-btn" type="button" aria-label="Cambiar tema" title="Modo Claro/Oscuro">
|
||||
<i class="bi bi-moon-stars-fill"></i>
|
||||
</button>
|
||||
@@ -2189,6 +2269,7 @@
|
||||
<script src="/js/walkthrough-engine.js"></script>
|
||||
{% if auth_user and auth_user.is_authenticated %}
|
||||
{% include "partials/ai_chat_widget.html" %}
|
||||
{% include "partials/user_qr_modal.html" %}
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,193 @@
|
||||
<!-- Modal Global de Credencial Digital QR del Usuario (UniCABA) -->
|
||||
<div class="modal fade" id="userQrCredentialModal" tabindex="-1" aria-labelledby="userQrCredentialModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered" style="max-width: 440px;">
|
||||
<div class="modal-content border-0 shadow-lg overflow-hidden" style="border-radius: 18px;">
|
||||
<!-- Encabezado con estética de Carnet Universitario -->
|
||||
<div class="p-4 text-white position-relative" style="background: linear-gradient(135deg, #1E1B4B 0%, #4338CA 50%, #B43E8E 100%);">
|
||||
<button type="button" class="btn-close btn-close-white position-absolute top-0 end-0 m-3" data-bs-dismiss="modal" aria-label="Cerrar"></button>
|
||||
<div class="d-flex align-items-center gap-2 mb-2">
|
||||
<img src="/img/logo_unicaba_mini.png" alt="UniCABA" height="26" class="bg-white rounded-circle p-1 shadow-sm">
|
||||
<span class="fw-bold tracking-wider fs-6 text-uppercase">UniCABA · Identidad Digital</span>
|
||||
</div>
|
||||
<div class="d-flex align-items-center justify-content-between mt-2">
|
||||
<div>
|
||||
<h5 class="modal-title fw-bold text-white mb-0" id="userQrCredentialModalLabel">
|
||||
Credencial Universitaria
|
||||
</h5>
|
||||
<p class="text-white-50 small mb-0 mt-1">Acceso a sedes y convenios de beneficios</p>
|
||||
</div>
|
||||
<span class="badge bg-success-subtle text-success border border-success-subtle rounded-pill px-2 py-1 small fw-bold">
|
||||
● ACTIVO
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Cuerpo del Modal -->
|
||||
<div class="modal-body p-4 text-center">
|
||||
<!-- Datos del Usuario -->
|
||||
<div class="mb-3">
|
||||
<h5 class="fw-bold mb-1 text-body" id="modalQrUserName">
|
||||
{{ auth_user.name if (auth_user and auth_user.name) else (user.name if user else 'Usuario') }}
|
||||
</h5>
|
||||
<div class="d-flex align-items-center justify-content-center gap-2 flex-wrap mb-2">
|
||||
<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 %}
|
||||
<span class="badge bg-body-tertiary text-body-secondary border font-monospace" style="font-size: 0.74rem;">
|
||||
DNI: {{ auth_user.document_number }}
|
||||
</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="text-muted small font-monospace" style="font-size: 0.78rem;">
|
||||
{{ auth_user.email if auth_user else '' }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Contenedor del Código QR Dinámico -->
|
||||
<div class="p-3 bg-body-tertiary border rounded-4 d-inline-block shadow-sm my-2 position-relative" style="background-color: var(--bs-body-bg) !important;">
|
||||
<div id="globalQrModalContainer" class="d-flex align-items-center justify-content-center" style="min-width: 190px; min-height: 190px;">
|
||||
<div class="spinner-border text-primary" role="status" id="globalQrSpinner">
|
||||
<span class="visually-hidden">Generando credencial QR...</span>
|
||||
</div>
|
||||
</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">
|
||||
<i class="bi bi-shield-check"></i>
|
||||
<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
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Botones de Acción -->
|
||||
<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
|
||||
</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
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Footer Informativo -->
|
||||
<div class="modal-footer bg-body-tertiary py-2 px-3 border-top d-flex justify-content-between align-items-center">
|
||||
<small class="text-muted" style="font-size: 0.72rem;">
|
||||
<i class="bi bi-info-circle me-1"></i>Escanea en recepción para ingresar
|
||||
</small>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" data-bs-dismiss="modal">
|
||||
Cerrar
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
let qrModalInstance = null;
|
||||
let cachedQrToken = null;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function renderModalQR(token) {
|
||||
var 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
|
||||
});
|
||||
} 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>';
|
||||
}
|
||||
}
|
||||
|
||||
function loadAndRenderQR(forceRefresh) {
|
||||
var spinner = document.getElementById('globalQrSpinner');
|
||||
var container = document.getElementById('globalQrModalContainer');
|
||||
|
||||
if (!forceRefresh && cachedQrToken) {
|
||||
renderModalQR(cachedQrToken);
|
||||
return;
|
||||
}
|
||||
|
||||
if (container && (!qrModalInstance || forceRefresh)) {
|
||||
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(data) {
|
||||
if (data && data.qr_token) {
|
||||
cachedQrToken = data.qr_token;
|
||||
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';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error('Token no disponible');
|
||||
}
|
||||
})
|
||||
.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 if auth_user else 'N/A' }}";
|
||||
ensureQrLibrary(function() {
|
||||
renderModalQR(fallback);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
window.refreshGlobalQrModal = function() {
|
||||
var icon = document.getElementById('globalRefreshQrIcon');
|
||||
if (icon) icon.classList.add('spin-animation');
|
||||
loadAndRenderQR(true);
|
||||
setTimeout(function() {
|
||||
if (icon) icon.classList.remove('spin-animation');
|
||||
}, 800);
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var modalEl = document.getElementById('userQrCredentialModal');
|
||||
if (modalEl) {
|
||||
modalEl.addEventListener('show.bs.modal', function() {
|
||||
loadAndRenderQR(false);
|
||||
});
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
Reference in New Issue
Block a user