From ce039c33eb688a1e21abef022ffcc082838d6790 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Oct 2026 03:34:01 -0600 Subject: [PATCH] feat(pos): integracion de pistola handheld con celular via SSE y PWA dedicada --- public/js/pos.js | 91 ++++++ public/pistola.html | 730 ++++++++++++++++++++++++++++++++++++++++++++ public/pos.html | 32 +- public/sw.js | 5 +- server/server.js | 51 ++++ 5 files changed, 906 insertions(+), 3 deletions(-) create mode 100644 public/pistola.html diff --git a/public/js/pos.js b/public/js/pos.js index 418b302..6a40ab4 100644 --- a/public/js/pos.js +++ b/public/js/pos.js @@ -1046,6 +1046,17 @@ function openPistolaModal(mode = 'cobro') { startModalCamera(); + // Notificar al teléfono vinculado que la caja está lista para escanear + try { + const total = posModalMode === 'cobro' ? posCart.reduce((sum, item) => sum + (item.product.precio_colones * item.cantidad), 0) : 0; + const totalItems = posCart.reduce((sum, item) => sum + item.cantidad, 0); + fetch('/api/pos/notify-scan-ready', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ mode: posModalMode, total, itemsCount: totalItems }) + }).catch(() => {}); + } catch (e) {} + setTimeout(() => { const input = document.getElementById('inputPistolaDirectScan'); if (input) input.focus(); @@ -1063,6 +1074,11 @@ function closePistolaModal(e) { // SIEMPRE apagar la cámara de inmediato al salir del modal stopModalCamera(); + // Notificar al teléfono vinculado que la sesión de escaneo terminó + try { + fetch('/api/pos/notify-scan-cancel', { method: 'POST' }).catch(() => {}); + } catch (e) {} + const modal = document.getElementById('modalPistolaCobro'); if (modal) modal.style.display = 'none'; } @@ -1393,6 +1409,37 @@ function initSSE() { loadPreOrders(); }); + // Disparo recibido desde pistola remota (teléfono celular) + sseSource.addEventListener('pistola_scan', async (e) => { + try { + const data = JSON.parse(e.data); + if (data && data.token) { + console.log('📡 [POS] Disparo recibido desde pistola remota (teléfono):', data.token); + + // Feedback visual en la barra superior + const badge = document.getElementById('pistolaStatusBadge'); + if (badge) { + const prevHtml = badge.innerHTML; + badge.innerHTML = ` 📱 Disparo: ${data.token}`; + badge.style.background = '#dcfce7'; + badge.style.borderColor = '#86efac'; + badge.style.color = '#15803d'; + setTimeout(() => { + badge.innerHTML = prevHtml; + badge.style.background = '#e0f2fe'; + badge.style.borderColor = '#bae6fd'; + badge.style.color = '#0369a1'; + }, 2500); + } + + // Ejecutar procesamiento del carné escaneado + await onQrCodeDetected(data.token); + } + } catch (err) { + console.error('Error procesando disparo remoto:', err); + } + }); + sseSource.addEventListener('producto_actualizado', (e) => { try { const prod = JSON.parse(e.data); @@ -1883,3 +1930,47 @@ function posLogout() { } window.posLogout = posLogout; +// ========================================== +// VINCULACIÓN DE TELÉFONO COMO PISTOLA QR +// ========================================== + +function openPhonePairingModal() { + const modal = document.getElementById('modalPhonePairing'); + if (!modal) return; + const pistolaUrl = window.location.origin + '/pistola.html'; + const img = document.getElementById('imgPairingQr'); + const link = document.getElementById('linkDirectPistola'); + if (img) img.src = `/api/qr-image/${encodeURIComponent(pistolaUrl)}`; + if (link) { + link.href = pistolaUrl; + } + modal.style.display = 'flex'; +} +window.openPhonePairingModal = openPhonePairingModal; + +function closePhonePairingModal(e) { + if (e && e.target && e.target.id !== 'modalPhonePairing') return; + const modal = document.getElementById('modalPhonePairing'); + if (modal) modal.style.display = 'none'; +} +window.closePhonePairingModal = closePhonePairingModal; + +function copyPistolaUrl() { + const pistolaUrl = window.location.origin + '/pistola.html'; + if (navigator.clipboard && navigator.clipboard.writeText) { + navigator.clipboard.writeText(pistolaUrl).then(() => { + const feedback = document.getElementById('lblCopyPistolaFeedback'); + if (feedback) { + feedback.style.display = 'block'; + setTimeout(() => { if (feedback) feedback.style.display = 'none'; }, 3000); + } + }).catch(() => { + prompt('Copia este enlace para abrir la pistola en tu teléfono:', pistolaUrl); + }); + } else { + prompt('Copia este enlace para abrir la pistola en tu teléfono:', pistolaUrl); + } +} +window.copyPistolaUrl = copyPistolaUrl; + + diff --git a/public/pistola.html b/public/pistola.html new file mode 100644 index 0000000..750c970 --- /dev/null +++ b/public/pistola.html @@ -0,0 +1,730 @@ + + + + + + SiboPay - Pistola Lectora QR Remota + + + + + + + + + +
+
+ SiboPay +
+

SiboPay Handheld

+

Pistola QR Inalámbrica

+
+
+ +
+ + Conectando... +
+
+ + +
+
+ ⚡ +
+
Cobro en Caja Listo
+
Esperando carné...
+
+
+ ¡Apunta! +
+ + +
+ + + +
+
+
+
+
+
+
+ + +
+ + +
+ + +
+
+
⚡ BEEP!
+
Disparo Enviado a Caja
+
QR-MATEO-2026-A891
+
+
+ + +
+
📷
+

Se requiere acceso a la cámara

+

+ Para usar tu teléfono como pistola lectora, habilita el permiso de cámara en este navegador. +

+ +
+
+ + + + + + + + + + diff --git a/public/pos.html b/public/pos.html index 8c59509..55ab91d 100644 --- a/public/pos.html +++ b/public/pos.html @@ -108,6 +108,10 @@ + Admin @@ -475,11 +479,37 @@ + + - + diff --git a/public/sw.js b/public/sw.js index b66bff1..ce616ed 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,9 +1,10 @@ -// RecreoPay PWA Service Worker v9.4 - Soporte Desktop Web Desacoplado -const CACHE_NAME = 'recreopay-v9.4'; +// RecreoPay PWA Service Worker v9.5 - Soporte Pistola Móvil Remota +const CACHE_NAME = 'recreopay-v9.5'; const STATIC_ASSETS = [ '/', '/index.html', '/pos.html', + '/pistola.html', '/carnet.html', '/manifest.json', '/css/styles.css', diff --git a/server/server.js b/server/server.js index b93e6ae..d27b48f 100644 --- a/server/server.js +++ b/server/server.js @@ -157,6 +157,57 @@ setInterval(() => { } }, 10000); +// ========================================== +// PISTOLA REMOTA QR / HANDHELD SMARTPHONE +// ========================================== + +// Disparo remoto desde la pistola móvil (teléfono) hacia la caja POS +app.post('/api/pos/remote-scan', (req, res) => { + try { + const { token, deviceName } = req.body; + if (!token) return res.status(400).json({ error: 'Token requerido' }); + + console.log(`📡 [PISTOLA REMOTA] Disparo recibido desde ${deviceName || 'teléfono'}: ${token}`); + + // Broadcast a todas las terminales POS conectadas por SSE + broadcastEvent('pistola_scan', { + token: String(token).trim(), + deviceName: deviceName || 'Pistola Teléfono', + timestamp: Date.now() + }); + + res.json({ ok: true, token: String(token).trim() }); + } catch (err) { + res.status(500).json({ error: err.message }); + } +}); + +// Solicitud desde el POS en PC al teléfono para avisar que la caja está lista para cobrar +app.post('/api/pos/notify-scan-ready', (req, res) => { + try { + const { mode, total, itemsCount } = req.body; + broadcastEvent('pistola_solicitud_cobro', { + mode: mode || 'cobro', + total: total || 0, + itemsCount: itemsCount || 0, + timestamp: Date.now() + }); + res.json({ ok: true }); + } catch (err) { + res.status(500).json({ error: err.message }); + } +}); + +// Cancelación de solicitud cuando se cierra el modal de cobro en la PC +app.post('/api/pos/notify-scan-cancel', (req, res) => { + try { + broadcastEvent('pistola_cancelar_cobro', { timestamp: Date.now() }); + res.json({ ok: true }); + } catch (err) { + res.status(500).json({ error: err.message }); + } +}); + // ========================================== // 0. AUTENTICACIÓN Y ROLES (ADMIN, PADRE, ESTUDIANTE) // ==========================================