fix: solucion integral de activacion de camara QR con deteccion HTTPS, resilencia iOS/Safari y fallback de foto nativa
This commit is contained in:
+25
-5
@@ -16,6 +16,16 @@
|
|||||||
<body class="mode-kids">
|
<body class="mode-kids">
|
||||||
|
|
||||||
<div class="app-container">
|
<div class="app-container">
|
||||||
|
<!-- BANNER DE ADVERTENCIA HTTPS PARA TELÉFONOS MÓVILES -->
|
||||||
|
<div id="bannerHttpsWarning" style="display: none; background: linear-gradient(135deg, #f59e0b, #d97706); color: white; padding: 10px 14px; font-size: 0.78rem; font-weight: 700; text-align: center; border-radius: 12px; margin: 10px 16px 0 16px; box-shadow: 0 2px 8px rgba(245, 158, 11, 0.3);">
|
||||||
|
🔒 Estás en HTTP. Para activar la cámara y pagos QR, tu celular requiere HTTPS:
|
||||||
|
<div style="margin-top: 6px;">
|
||||||
|
<a id="linkHttpsRedirect" href="https://somewhat-ships-looksmart-optical.trycloudflare.com" style="display: inline-block; padding: 5px 14px; background: white; color: #b45309; border-radius: 20px; font-weight: 900; text-decoration: none; font-size: 0.8rem; box-shadow: 0 2px 5px rgba(0,0,0,0.15);">
|
||||||
|
🚀 Toca aquí para abrir en HTTPS Seguro
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- BARRA SUPERIOR -->
|
<!-- BARRA SUPERIOR -->
|
||||||
<header class="top-bar">
|
<header class="top-bar">
|
||||||
<a href="#" class="brand-badge">
|
<a href="#" class="brand-badge">
|
||||||
@@ -268,18 +278,28 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style="position: relative; border-radius: 12px; overflow: hidden; background: #000; min-height: 160px; margin-bottom: 12px; display: flex; align-items: center; justify-content: center;">
|
<div style="position: relative; border-radius: 12px; overflow: hidden; background: #000; min-height: 170px; margin-bottom: 10px; display: flex; align-items: center; justify-content: center;">
|
||||||
<video id="transferVideo" style="width: 100%; height: 160px; object-fit: cover;" autoplay playsinline muted></video>
|
<video id="transferVideo" style="width: 100%; height: 170px; object-fit: cover;" autoplay playsinline webkit-playsinline muted></video>
|
||||||
<div class="laser-scan-line"></div>
|
<div class="laser-scan-line"></div>
|
||||||
|
|
||||||
<div id="transferCameraHelp" style="display: none; position: absolute; inset: 0; background: rgba(15, 23, 42, 0.9); padding: 14px; text-align: center; flex-direction: column; justify-content: center; align-items: center; gap: 8px; z-index: 10;">
|
<div id="transferCameraHelp" style="display: none; position: absolute; inset: 0; background: rgba(15, 23, 42, 0.92); padding: 14px; text-align: center; flex-direction: column; justify-content: center; align-items: center; gap: 10px; z-index: 10;">
|
||||||
<span style="font-size: 1.8rem;">📷</span>
|
<span style="font-size: 1.8rem;">📷</span>
|
||||||
<p id="transferCameraHelpText" style="font-size: 0.76rem; color: #f8fafc; line-height: 1.3;">Toca el botón para permitir la cámara</p>
|
<div id="transferCameraHelpText" style="font-size: 0.76rem; color: #f8fafc; line-height: 1.35;">Toca el botón para permitir la cámara</div>
|
||||||
<button type="button" onclick="startTransferCamera(true)" style="padding: 6px 14px; background: #0284c7; color: white; border: none; border-radius: 8px; font-weight: 800; font-size: 0.8rem; cursor: pointer;">
|
<div id="transferCameraActionContainer" style="display: flex; flex-direction: column; gap: 8px; width: 100%; align-items: center;">
|
||||||
|
<button id="btnAllowTransferCamera" type="button" onclick="startTransferCamera(true)" style="padding: 8px 18px; background: #0284c7; color: white; border: none; border-radius: 8px; font-weight: 800; font-size: 0.82rem; cursor: pointer; box-shadow: 0 4px 10px rgba(2, 132, 199, 0.3);">
|
||||||
Permitir Cámara
|
Permitir Cámara
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ACCESO DIRECTO PARA SUBIR O TOMAR FOTO DEL QR CON LA CÁMARA NATIVA -->
|
||||||
|
<div style="display: flex; justify-content: center; margin-bottom: 12px;">
|
||||||
|
<label style="cursor: pointer; display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; background: #f0fdf4; border: 1.5px dashed #86efac; border-radius: 10px; font-size: 0.78rem; font-weight: 800; color: #166534; box-shadow: 0 1px 3px rgba(0,0,0,0.04);">
|
||||||
|
<span>📸</span> Tomar foto del QR o carné
|
||||||
|
<input type="file" accept="image/*" capture="environment" style="display: none;" onchange="handleTransferQrPhoto(event)">
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div style="text-align: center; margin-bottom: 12px;">
|
<div style="text-align: center; margin-bottom: 12px;">
|
||||||
<span style="font-size: 0.74rem; color: #64748b; font-weight: 700;">O selecciona a un compañero para transferirle:</span>
|
<span style="font-size: 0.74rem; color: #64748b; font-weight: 700;">O selecciona a un compañero para transferirle:</span>
|
||||||
|
|||||||
+178
-18
@@ -8,8 +8,42 @@ let activeCategoryId = null;
|
|||||||
let cart = [];
|
let cart = [];
|
||||||
let currentAppMode = 'kids';
|
let currentAppMode = 'kids';
|
||||||
|
|
||||||
|
let CLOUDFLARE_TUNNEL_URL = 'https://somewhat-ships-looksmart-optical.trycloudflare.com';
|
||||||
|
|
||||||
|
function checkHttpsEnvironment() {
|
||||||
|
const isHttp = window.location.protocol !== 'https:' &&
|
||||||
|
window.location.hostname !== 'localhost' &&
|
||||||
|
window.location.hostname !== '127.0.0.1';
|
||||||
|
|
||||||
|
if (isHttp) {
|
||||||
|
const banner = document.getElementById('bannerHttpsWarning');
|
||||||
|
if (banner) {
|
||||||
|
banner.style.display = 'block';
|
||||||
|
const link = document.getElementById('linkHttpsRedirect');
|
||||||
|
if (link) {
|
||||||
|
link.href = CLOUDFLARE_TUNNEL_URL + window.location.pathname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch('/api/server-info')
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(info => {
|
||||||
|
if (info && info.tunnelUrl) {
|
||||||
|
CLOUDFLARE_TUNNEL_URL = info.tunnelUrl;
|
||||||
|
const link = document.getElementById('linkHttpsRedirect');
|
||||||
|
if (link && isHttp) {
|
||||||
|
link.href = CLOUDFLARE_TUNNEL_URL + window.location.pathname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
// Inicialización al cargar la página
|
// Inicialización al cargar la página
|
||||||
document.addEventListener('DOMContentLoaded', async () => {
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
|
checkHttpsEnvironment();
|
||||||
|
|
||||||
// Registrar Service Worker para PWA si está soportado
|
// Registrar Service Worker para PWA si está soportado
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
navigator.serviceWorker.register('/sw.js').catch(err => console.log('SW error:', err));
|
navigator.serviceWorker.register('/sw.js').catch(err => console.log('SW error:', err));
|
||||||
@@ -480,7 +514,6 @@ function openTransferModal() {
|
|||||||
document.getElementById('modalTransfer').style.display = 'flex';
|
document.getElementById('modalTransfer').style.display = 'flex';
|
||||||
resetTransferScan();
|
resetTransferScan();
|
||||||
renderQuickTransferFriends();
|
renderQuickTransferFriends();
|
||||||
startTransferCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeTransferModal(e) {
|
function closeTransferModal(e) {
|
||||||
@@ -551,6 +584,8 @@ async function startTransferCamera(isUserAction = false) {
|
|||||||
const video = document.getElementById('transferVideo');
|
const video = document.getElementById('transferVideo');
|
||||||
const status = document.getElementById('transferCameraStatus');
|
const status = document.getElementById('transferCameraStatus');
|
||||||
const helpOverlay = document.getElementById('transferCameraHelp');
|
const helpOverlay = document.getElementById('transferCameraHelp');
|
||||||
|
const helpText = document.getElementById('transferCameraHelpText');
|
||||||
|
const actionContainer = document.getElementById('transferCameraActionContainer');
|
||||||
const retryBtn = document.getElementById('btnRetryTransferCamera');
|
const retryBtn = document.getElementById('btnRetryTransferCamera');
|
||||||
const flipBtn = document.getElementById('btnFlipTransferCamera');
|
const flipBtn = document.getElementById('btnFlipTransferCamera');
|
||||||
|
|
||||||
@@ -558,34 +593,86 @@ async function startTransferCamera(isUserAction = false) {
|
|||||||
|
|
||||||
stopTransferCamera();
|
stopTransferCamera();
|
||||||
|
|
||||||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
const isHttp = window.location.protocol !== 'https:' &&
|
||||||
if (status) status.textContent = '⚠️ Cámara requiere HTTPS';
|
window.location.hostname !== 'localhost' &&
|
||||||
|
window.location.hostname !== '127.0.0.1';
|
||||||
|
|
||||||
|
// Si no hay soporte de getUserMedia o estamos en HTTP inseguro
|
||||||
|
if (isHttp || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||||
|
if (status) status.textContent = '⚠️ Requiere HTTPS';
|
||||||
if (helpOverlay) {
|
if (helpOverlay) {
|
||||||
helpOverlay.style.display = 'flex';
|
helpOverlay.style.display = 'flex';
|
||||||
document.getElementById('transferCameraHelpText').textContent = '⚠️ Los navegadores bloquean la cámara si no es HTTPS. Usa el enlace seguro o toca directamente a tu amigo abajo.';
|
if (helpText) {
|
||||||
|
helpText.innerHTML = `
|
||||||
|
<div style="font-weight: 800; color: #fca5a5; font-size: 0.85rem; margin-bottom: 4px;">⚠️ Cámara requiere HTTPS</div>
|
||||||
|
<span style="font-size: 0.72rem; color: #cbd5e1;">Por seguridad, los navegadores en celulares bloquean la cámara si la conexión no es HTTPS. Toca el botón para abrir la app segura:</span>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
if (actionContainer) {
|
||||||
|
actionContainer.innerHTML = `
|
||||||
|
<button type="button" onclick="window.location.href='${CLOUDFLARE_TUNNEL_URL}' + window.location.pathname" style="padding: 10px 16px; background: #10b981; color: white; border: none; border-radius: 10px; font-weight: 900; font-size: 0.84rem; cursor: pointer; box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);">
|
||||||
|
🚀 Cambiar a HTTPS Seguro
|
||||||
|
</button>
|
||||||
|
`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Si estamos en un contexto seguro HTTPS:
|
||||||
try {
|
try {
|
||||||
if (status) status.textContent = 'Conectando cámara...';
|
if (status) status.textContent = 'Conectando cámara...';
|
||||||
|
if (helpOverlay) helpOverlay.style.display = 'none';
|
||||||
|
|
||||||
|
// Propiedades obligatorias para iOS Safari WebKit y Chrome Android
|
||||||
|
video.muted = true;
|
||||||
|
video.playsInline = true;
|
||||||
|
video.setAttribute('playsinline', 'true');
|
||||||
|
video.setAttribute('webkit-playsinline', 'true');
|
||||||
|
video.setAttribute('autoplay', 'true');
|
||||||
|
|
||||||
|
// Intentar abrir cámara con fallbacks progresivos
|
||||||
|
const constraintConfigs = [
|
||||||
|
{ video: { facingMode: { ideal: transferFacingMode }, width: { ideal: 1280 } }, audio: false },
|
||||||
|
{ video: { facingMode: { ideal: transferFacingMode } }, audio: false },
|
||||||
|
{ video: { facingMode: transferFacingMode }, audio: false },
|
||||||
|
{ video: true, audio: false }
|
||||||
|
];
|
||||||
|
|
||||||
|
let stream = null;
|
||||||
|
let lastError = null;
|
||||||
|
|
||||||
|
for (const c of constraintConfigs) {
|
||||||
try {
|
try {
|
||||||
transferVideoStream = await navigator.mediaDevices.getUserMedia({
|
stream = await navigator.mediaDevices.getUserMedia(c);
|
||||||
video: { facingMode: { ideal: transferFacingMode }, width: { ideal: 720 } },
|
if (stream) break;
|
||||||
audio: false
|
} catch (errAttempt) {
|
||||||
});
|
lastError = errAttempt;
|
||||||
} catch (err1) {
|
}
|
||||||
// Fallback a cualquier lente disponible
|
|
||||||
transferVideoStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
video.setAttribute('playsinline', 'true');
|
if (!stream) {
|
||||||
video.setAttribute('autoplay', 'true');
|
throw lastError || new Error('No se pudo acceder al lente de la cámara');
|
||||||
video.muted = true;
|
}
|
||||||
video.srcObject = transferVideoStream;
|
|
||||||
|
|
||||||
|
transferVideoStream = stream;
|
||||||
|
video.srcObject = stream;
|
||||||
|
|
||||||
|
// Esperar a que el elemento video esté listo para reproducir (evita AbortError en Safari)
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
if (video.readyState >= 1) {
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
video.onloadedmetadata = () => resolve();
|
||||||
|
setTimeout(resolve, 800);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
await video.play();
|
await video.play();
|
||||||
|
} catch (playErr) {
|
||||||
|
console.warn('Reproducción diferida:', playErr);
|
||||||
|
}
|
||||||
|
|
||||||
if (status) status.textContent = '🟢 Escaneando QR...';
|
if (status) status.textContent = '🟢 Escaneando QR...';
|
||||||
if (helpOverlay) helpOverlay.style.display = 'none';
|
if (helpOverlay) helpOverlay.style.display = 'none';
|
||||||
@@ -594,17 +681,90 @@ async function startTransferCamera(isUserAction = false) {
|
|||||||
|
|
||||||
isTransferScanning = true;
|
isTransferScanning = true;
|
||||||
startTransferQrDetection(video);
|
startTransferQrDetection(video);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Error iniciando cámara de transferencia:', err);
|
console.warn('Error accediendo a cámara:', err);
|
||||||
if (status) status.textContent = '⚪ Cámara inactiva';
|
let userMsg = 'Toca el botón para permitir el uso de la cámara.';
|
||||||
|
const errName = err.name || '';
|
||||||
|
|
||||||
|
if (errName === 'NotAllowedError' || errName === 'PermissionDeniedError') {
|
||||||
|
userMsg = '🔒 Permiso denegado: El navegador bloqueó la cámara. Toca el candado o configuración junto a la barra de dirección y habilita la Cámara.';
|
||||||
|
} else if (errName === 'NotFoundError' || errName === 'DevicesNotFoundError') {
|
||||||
|
userMsg = '📷 No se detectó ninguna cámara física en este dispositivo.';
|
||||||
|
} else if (errName === 'NotReadableError' || errName === 'TrackStartError') {
|
||||||
|
userMsg = '⚠️ La cámara está ocupada por otra app (WhatsApp, etc). Ciérrala e intenta de nuevo.';
|
||||||
|
} else if (errName === 'OverconstrainedError') {
|
||||||
|
userMsg = '⚠️ Tu cámara no admite la resolución solicitada.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status) status.textContent = '⚠️ Cámara bloqueada';
|
||||||
if (helpOverlay) {
|
if (helpOverlay) {
|
||||||
helpOverlay.style.display = 'flex';
|
helpOverlay.style.display = 'flex';
|
||||||
document.getElementById('transferCameraHelpText').textContent = 'Toca Permitir Cámara o activa los permisos en la barra del navegador.';
|
if (helpText) {
|
||||||
|
helpText.innerHTML = `
|
||||||
|
<div style="font-weight: 800; color: #fca5a5; font-size: 0.8rem; margin-bottom: 4px;">⚠️ Permiso Requerido</div>
|
||||||
|
<span style="font-size: 0.72rem; color: #f1f5f9;">${userMsg}</span>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
if (actionContainer) {
|
||||||
|
actionContainer.innerHTML = `
|
||||||
|
<button type="button" onclick="startTransferCamera(true)" style="padding: 8px 16px; background: #0284c7; color: white; border: none; border-radius: 8px; font-weight: 800; font-size: 0.82rem; cursor: pointer; box-shadow: 0 4px 10px rgba(2, 132, 199, 0.4);">
|
||||||
|
📷 Tocar para Permitir Cámara
|
||||||
|
</button>
|
||||||
|
`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (retryBtn) retryBtn.style.display = 'inline-block';
|
if (retryBtn) retryBtn.style.display = 'inline-block';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleTransferQrPhoto(event) {
|
||||||
|
const file = event.target.files && event.target.files[0];
|
||||||
|
if (!file) return;
|
||||||
|
|
||||||
|
const status = document.getElementById('transferCameraStatus');
|
||||||
|
if (status) status.textContent = 'Analizando foto...';
|
||||||
|
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = function(e) {
|
||||||
|
const img = new Image();
|
||||||
|
img.onload = function() {
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
const maxDim = 1200;
|
||||||
|
let w = img.width;
|
||||||
|
let h = img.height;
|
||||||
|
if (w > maxDim || h > maxDim) {
|
||||||
|
if (w > h) {
|
||||||
|
h = Math.round((h * maxDim) / w);
|
||||||
|
w = maxDim;
|
||||||
|
} else {
|
||||||
|
w = Math.round((w * maxDim) / h);
|
||||||
|
h = maxDim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
canvas.width = w;
|
||||||
|
canvas.height = h;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
ctx.drawImage(img, 0, 0, w, h);
|
||||||
|
const imgData = ctx.getImageData(0, 0, w, h);
|
||||||
|
|
||||||
|
if (window.jsQR) {
|
||||||
|
const code = window.jsQR(imgData.data, w, h, {
|
||||||
|
inversionAttempts: 'attemptBoth'
|
||||||
|
});
|
||||||
|
if (code && code.data) {
|
||||||
|
fetchStudentByScannedQr(code.data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (status) status.textContent = '❌ No se detectó QR';
|
||||||
|
alert('⚠️ No se detectó ningún código QR en la foto. Intenta tomarla más de cerca con buena iluminación.');
|
||||||
|
};
|
||||||
|
img.src = e.target.result;
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
}
|
||||||
|
|
||||||
function flipTransferCamera() {
|
function flipTransferCamera() {
|
||||||
transferFacingMode = transferFacingMode === 'environment' ? 'user' : 'environment';
|
transferFacingMode = transferFacingMode === 'environment' ? 'user' : 'environment';
|
||||||
startTransferCamera(true);
|
startTransferCamera(true);
|
||||||
|
|||||||
+171
-18
@@ -8,8 +8,40 @@ let currentTab = 'mostrador';
|
|||||||
let sseSource = null;
|
let sseSource = null;
|
||||||
let videoStream = null;
|
let videoStream = null;
|
||||||
let isScanningActive = true;
|
let isScanningActive = true;
|
||||||
|
let CLOUDFLARE_TUNNEL_URL = 'https://somewhat-ships-looksmart-optical.trycloudflare.com';
|
||||||
|
|
||||||
|
function checkPosHttpsEnvironment() {
|
||||||
|
const isHttp = window.location.protocol !== 'https:' &&
|
||||||
|
window.location.hostname !== 'localhost' &&
|
||||||
|
window.location.hostname !== '127.0.0.1';
|
||||||
|
|
||||||
|
if (isHttp) {
|
||||||
|
const banner = document.getElementById('posBannerHttpsWarning');
|
||||||
|
if (banner) {
|
||||||
|
banner.style.display = 'block';
|
||||||
|
const link = document.getElementById('posLinkHttpsRedirect');
|
||||||
|
if (link) {
|
||||||
|
link.href = CLOUDFLARE_TUNNEL_URL + window.location.pathname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch('/api/server-info')
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(info => {
|
||||||
|
if (info && info.tunnelUrl) {
|
||||||
|
CLOUDFLARE_TUNNEL_URL = info.tunnelUrl;
|
||||||
|
const link = document.getElementById('posLinkHttpsRedirect');
|
||||||
|
if (link && isHttp) {
|
||||||
|
link.href = CLOUDFLARE_TUNNEL_URL + window.location.pathname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', async () => {
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
|
checkPosHttpsEnvironment();
|
||||||
await loadCatalog();
|
await loadCatalog();
|
||||||
await loadPreOrders();
|
await loadPreOrders();
|
||||||
initCamera();
|
initCamera();
|
||||||
@@ -147,6 +179,8 @@ async function initCamera(isUserAction = false) {
|
|||||||
const video = document.getElementById('scannerVideo');
|
const video = document.getElementById('scannerVideo');
|
||||||
const status = document.getElementById('cameraStatus');
|
const status = document.getElementById('cameraStatus');
|
||||||
const overlay = document.getElementById('cameraHelpOverlay');
|
const overlay = document.getElementById('cameraHelpOverlay');
|
||||||
|
const helpText = document.getElementById('cameraHelpText');
|
||||||
|
const actionContainer = document.getElementById('posCameraActionContainer');
|
||||||
const retryBtn = document.getElementById('btnRetryPosCamera');
|
const retryBtn = document.getElementById('btnRetryPosCamera');
|
||||||
const flipBtn = document.getElementById('btnFlipPosCamera');
|
const flipBtn = document.getElementById('btnFlipPosCamera');
|
||||||
|
|
||||||
@@ -155,35 +189,83 @@ async function initCamera(isUserAction = false) {
|
|||||||
videoStream = null;
|
videoStream = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
const isHttp = window.location.protocol !== 'https:' &&
|
||||||
status.textContent = '⚠️ Requiere HTTPS';
|
window.location.hostname !== 'localhost' &&
|
||||||
|
window.location.hostname !== '127.0.0.1';
|
||||||
|
|
||||||
|
if (isHttp || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||||
|
if (status) status.textContent = '⚠️ Requiere HTTPS';
|
||||||
if (overlay) {
|
if (overlay) {
|
||||||
overlay.style.display = 'flex';
|
overlay.style.display = 'flex';
|
||||||
document.getElementById('cameraHelpText').textContent = '⚠️ Para usar la cámara en dispositivos remotos se requiere conexión segura HTTPS. Puedes usar los botones de prueba abajo.';
|
if (helpText) {
|
||||||
|
helpText.innerHTML = `
|
||||||
|
<div style="font-weight: 800; color: #fca5a5; font-size: 0.85rem; margin-bottom: 4px;">⚠️ Cámara requiere HTTPS</div>
|
||||||
|
<span style="font-size: 0.74rem; color: #cbd5e1;">Para acceder a la cámara en vivo en dispositivos remotos o celulares se requiere conexión segura HTTPS. Toca el botón para abrir la terminal segura:</span>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
if (actionContainer) {
|
||||||
|
actionContainer.innerHTML = `
|
||||||
|
<button type="button" onclick="window.location.href='${CLOUDFLARE_TUNNEL_URL}' + window.location.pathname" style="padding: 10px 16px; background: #10b981; color: white; border: none; border-radius: 10px; font-weight: 900; font-size: 0.84rem; cursor: pointer; box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);">
|
||||||
|
🚀 Cambiar a HTTPS Seguro
|
||||||
|
</button>
|
||||||
|
`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
status.textContent = 'Conectando lente...';
|
if (status) status.textContent = 'Conectando cámara...';
|
||||||
|
if (overlay) overlay.style.display = 'none';
|
||||||
|
|
||||||
|
video.muted = true;
|
||||||
|
video.playsInline = true;
|
||||||
|
video.setAttribute('playsinline', 'true');
|
||||||
|
video.setAttribute('webkit-playsinline', 'true');
|
||||||
|
video.setAttribute('autoplay', 'true');
|
||||||
|
|
||||||
|
const constraintConfigs = [
|
||||||
|
{ video: { facingMode: { ideal: posFacingMode }, width: { ideal: 1280 } }, audio: false },
|
||||||
|
{ video: { facingMode: { ideal: posFacingMode } }, audio: false },
|
||||||
|
{ video: { facingMode: posFacingMode }, audio: false },
|
||||||
|
{ video: true, audio: false }
|
||||||
|
];
|
||||||
|
|
||||||
|
let stream = null;
|
||||||
|
let lastError = null;
|
||||||
|
|
||||||
|
for (const c of constraintConfigs) {
|
||||||
try {
|
try {
|
||||||
videoStream = await navigator.mediaDevices.getUserMedia({
|
stream = await navigator.mediaDevices.getUserMedia(c);
|
||||||
video: { facingMode: { ideal: posFacingMode }, width: { ideal: 1280 } },
|
if (stream) break;
|
||||||
audio: false
|
} catch (errAttempt) {
|
||||||
});
|
lastError = errAttempt;
|
||||||
} catch (err1) {
|
}
|
||||||
// Fallback a cualquier cámara disponible
|
|
||||||
videoStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
video.setAttribute('playsinline', 'true');
|
if (!stream) {
|
||||||
video.setAttribute('autoplay', 'true');
|
throw lastError || new Error('No se pudo acceder a la cámara');
|
||||||
video.muted = true;
|
}
|
||||||
video.srcObject = videoStream;
|
|
||||||
|
|
||||||
|
videoStream = stream;
|
||||||
|
video.srcObject = stream;
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
if (video.readyState >= 1) {
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
video.onloadedmetadata = () => resolve();
|
||||||
|
setTimeout(resolve, 800);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
await video.play();
|
await video.play();
|
||||||
|
} catch (playErr) {
|
||||||
|
console.warn('Reproducción diferida de cámara POS:', playErr);
|
||||||
|
}
|
||||||
|
|
||||||
status.textContent = '🟢 Escáner activo';
|
if (status) status.textContent = '🟢 Escáner activo';
|
||||||
if (overlay) overlay.style.display = 'none';
|
if (overlay) overlay.style.display = 'none';
|
||||||
if (retryBtn) retryBtn.style.display = 'none';
|
if (retryBtn) retryBtn.style.display = 'none';
|
||||||
if (flipBtn) flipBtn.style.display = 'inline-block';
|
if (flipBtn) flipBtn.style.display = 'inline-block';
|
||||||
@@ -191,15 +273,86 @@ async function initCamera(isUserAction = false) {
|
|||||||
startUniversalQrDetection(video);
|
startUniversalQrDetection(video);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Error accediendo a cámara:', err);
|
console.warn('Error accediendo a cámara:', err);
|
||||||
status.textContent = '⚪ Cámara inactiva';
|
let userMsg = 'Toca el botón para permitir el uso de la cámara.';
|
||||||
|
const errName = err.name || '';
|
||||||
|
|
||||||
|
if (errName === 'NotAllowedError' || errName === 'PermissionDeniedError') {
|
||||||
|
userMsg = '🔒 Permiso denegado: El navegador bloqueó la cámara. Habilita el permiso de cámara en la barra de direcciones.';
|
||||||
|
} else if (errName === 'NotFoundError' || errName === 'DevicesNotFoundError') {
|
||||||
|
userMsg = '📷 No se detectó ninguna cámara disponible.';
|
||||||
|
} else if (errName === 'NotReadableError' || errName === 'TrackStartError') {
|
||||||
|
userMsg = '⚠️ La cámara está ocupada por otra app. Ciérrala e intenta de nuevo.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status) status.textContent = '⚪ Cámara inactiva';
|
||||||
if (overlay) {
|
if (overlay) {
|
||||||
overlay.style.display = 'flex';
|
overlay.style.display = 'flex';
|
||||||
document.getElementById('cameraHelpText').textContent = 'Toca Permitir Cámara o habilita los permisos en la barra de direcciones.';
|
if (helpText) {
|
||||||
|
helpText.innerHTML = `
|
||||||
|
<div style="font-weight: 800; color: #fca5a5; font-size: 0.8rem; margin-bottom: 4px;">⚠️ Permiso Requerido</div>
|
||||||
|
<span style="font-size: 0.72rem; color: #f1f5f9;">${userMsg}</span>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
if (actionContainer) {
|
||||||
|
actionContainer.innerHTML = `
|
||||||
|
<button type="button" onclick="initCamera(true)" style="padding: 8px 16px; background: #0284c7; color: white; border: none; border-radius: 8px; font-weight: 800; font-size: 0.82rem; cursor: pointer; box-shadow: 0 4px 10px rgba(2, 132, 199, 0.4);">
|
||||||
|
📷 Tocar para Permitir Cámara
|
||||||
|
</button>
|
||||||
|
`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (retryBtn) retryBtn.style.display = 'inline-block';
|
if (retryBtn) retryBtn.style.display = 'inline-block';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handlePosQrPhoto(event) {
|
||||||
|
const file = event.target.files && event.target.files[0];
|
||||||
|
if (!file) return;
|
||||||
|
|
||||||
|
const status = document.getElementById('cameraStatus');
|
||||||
|
if (status) status.textContent = 'Analizando foto...';
|
||||||
|
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = function(e) {
|
||||||
|
const img = new Image();
|
||||||
|
img.onload = function() {
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
const maxDim = 1200;
|
||||||
|
let w = img.width;
|
||||||
|
let h = img.height;
|
||||||
|
if (w > maxDim || h > maxDim) {
|
||||||
|
if (w > h) {
|
||||||
|
h = Math.round((h * maxDim) / w);
|
||||||
|
w = maxDim;
|
||||||
|
} else {
|
||||||
|
w = Math.round((w * maxDim) / h);
|
||||||
|
h = maxDim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
canvas.width = w;
|
||||||
|
canvas.height = h;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
ctx.drawImage(img, 0, 0, w, h);
|
||||||
|
const imgData = ctx.getImageData(0, 0, w, h);
|
||||||
|
|
||||||
|
if (window.jsQR) {
|
||||||
|
const code = window.jsQR(imgData.data, w, h, {
|
||||||
|
inversionAttempts: 'attemptBoth'
|
||||||
|
});
|
||||||
|
if (code && code.data) {
|
||||||
|
onQrCodeDetected(code.data);
|
||||||
|
if (status) status.textContent = '🟢 QR Reconocido';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (status) status.textContent = '❌ No detectado';
|
||||||
|
alert('⚠️ No se detectó un código QR válido en la foto tomada.');
|
||||||
|
};
|
||||||
|
img.src = e.target.result;
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
}
|
||||||
|
|
||||||
function flipPosCamera() {
|
function flipPosCamera() {
|
||||||
posFacingMode = posFacingMode === 'environment' ? 'user' : 'environment';
|
posFacingMode = posFacingMode === 'environment' ? 'user' : 'environment';
|
||||||
initCamera(true);
|
initCamera(true);
|
||||||
|
|||||||
+22
-5
@@ -29,9 +29,16 @@
|
|||||||
.status-listo { background: #dcfce7; color: #166534; }
|
.status-listo { background: #dcfce7; color: #166534; }
|
||||||
.status-entregado { background: #f1f5f9; color: #64748b; }
|
.status-entregado { background: #f1f5f9; color: #64748b; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
|
||||||
<body style="background: #0b1120; color: #f8fafc;">
|
<body style="background: #0b1120; color: #f8fafc;">
|
||||||
|
|
||||||
|
<!-- BANNER DE ADVERTENCIA HTTPS PARA TERMINALES REMOTAS -->
|
||||||
|
<div id="posBannerHttpsWarning" style="display: none; background: linear-gradient(135deg, #b45309, #d97706); color: white; padding: 10px 14px; font-size: 0.8rem; font-weight: 700; text-align: center;">
|
||||||
|
🔒 Terminal en HTTP. Para activar la cámara en vivo del dispositivo, se requiere conexión HTTPS:
|
||||||
|
<a id="posLinkHttpsRedirect" href="https://somewhat-ships-looksmart-optical.trycloudflare.com/pos.html" style="margin-left: 8px; color: white; text-decoration: underline; font-weight: 900;">
|
||||||
|
🚀 Abrir en HTTPS Seguro ↗
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="pos-layout">
|
<div class="pos-layout">
|
||||||
<!-- PANEL IZQUIERDO: CATÁLOGO TÁCTIL Y COLA DE PRE-ÓRDENES -->
|
<!-- PANEL IZQUIERDO: CATÁLOGO TÁCTIL Y COLA DE PRE-ÓRDENES -->
|
||||||
<div class="pos-catalog-pane">
|
<div class="pos-catalog-pane">
|
||||||
@@ -109,16 +116,26 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style="position: relative; border-radius: 10px; overflow: hidden; background: #000; min-height: 180px; display: flex; align-items: center; justify-content: center;">
|
<div style="position: relative; border-radius: 10px; overflow: hidden; background: #000; min-height: 180px; display: flex; align-items: center; justify-content: center;">
|
||||||
<video id="scannerVideo" class="camera-preview" autoplay playsinline muted></video>
|
<video id="scannerVideo" class="camera-preview" autoplay playsinline webkit-playsinline muted></video>
|
||||||
<div class="laser-scan-line"></div>
|
<div class="laser-scan-line"></div>
|
||||||
<div id="cameraHelpOverlay" style="display: none; position: absolute; inset: 0; background: rgba(15, 23, 42, 0.9); padding: 14px; text-align: center; flex-direction: column; justify-content: center; align-items: center; gap: 8px; z-index: 10;">
|
<div id="cameraHelpOverlay" style="display: none; position: absolute; inset: 0; background: rgba(15, 23, 42, 0.92); padding: 14px; text-align: center; flex-direction: column; justify-content: center; align-items: center; gap: 10px; z-index: 10;">
|
||||||
<span style="font-size: 1.8rem;">📷</span>
|
<span style="font-size: 1.8rem;">📷</span>
|
||||||
<p id="cameraHelpText" style="font-size: 0.78rem; color: #f8fafc; line-height: 1.3;">Haz clic en Activar Cámara para escanear</p>
|
<div id="cameraHelpText" style="font-size: 0.78rem; color: #f8fafc; line-height: 1.35;">Haz clic en Activar Cámara para escanear</div>
|
||||||
<button onclick="initCamera(true)" style="padding: 6px 14px; background: #0284c7; color: white; border: none; border-radius: 8px; font-weight: 800; font-size: 0.8rem; cursor: pointer;">
|
<div id="posCameraActionContainer" style="display: flex; flex-direction: column; gap: 8px; width: 100%; align-items: center;">
|
||||||
|
<button id="btnAllowPosCamera" onclick="initCamera(true)" style="padding: 8px 16px; background: #0284c7; color: white; border: none; border-radius: 8px; font-weight: 800; font-size: 0.82rem; cursor: pointer; box-shadow: 0 4px 10px rgba(2, 132, 199, 0.4);">
|
||||||
Permitir Cámara
|
Permitir Cámara
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- BOTÓN PARA TOMAR FOTO DEL CARNÉ O QR -->
|
||||||
|
<div style="display: flex; justify-content: center; margin-top: 8px;">
|
||||||
|
<label style="cursor: pointer; display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; background: #1e293b; border: 1px dashed #475569; border-radius: 8px; font-size: 0.74rem; font-weight: 700; color: #38bdf8;">
|
||||||
|
<span>📸</span> Tomar foto de carné QR
|
||||||
|
<input type="file" accept="image/*" capture="environment" style="display: none;" onchange="handlePosQrPhoto(event)">
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- BOTONES DE PRUEBA RÁPIDA DE QR (SIMULACIÓN DE ESCANEO DE CARNÉ O CELULAR) -->
|
<!-- BOTONES DE PRUEBA RÁPIDA DE QR (SIMULACIÓN DE ESCANEO DE CARNÉ O CELULAR) -->
|
||||||
<div style="margin-top: 10px; display: flex; gap: 6px; flex-wrap: wrap; justify-content: center;">
|
<div style="margin-top: 10px; display: flex; gap: 6px; flex-wrap: wrap; justify-content: center;">
|
||||||
|
|||||||
@@ -404,6 +404,14 @@ app.post('/api/ordenes/despachar-qr', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Información de conectividad y túnel HTTPS
|
||||||
|
app.get('/api/server-info', (req, res) => {
|
||||||
|
res.json({
|
||||||
|
tunnelUrl: process.env.TUNNEL_URL || 'https://somewhat-ships-looksmart-optical.trycloudflare.com',
|
||||||
|
httpsAvailable: true
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Iniciar servidor
|
// Iniciar servidor
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`🚀 Servidor RecreoPay iniciado en http://localhost:${PORT}`);
|
console.log(`🚀 Servidor RecreoPay iniciado en http://localhost:${PORT}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user