fix: solucion integral de activacion de camara QR con deteccion HTTPS, resilencia iOS/Safari y fallback de foto nativa
This commit is contained in:
+27
-7
@@ -16,6 +16,16 @@
|
||||
<body class="mode-kids">
|
||||
|
||||
<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 -->
|
||||
<header class="top-bar">
|
||||
<a href="#" class="brand-badge">
|
||||
@@ -268,19 +278,29 @@
|
||||
</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;">
|
||||
<video id="transferVideo" style="width: 100%; height: 160px; object-fit: cover;" autoplay playsinline muted></video>
|
||||
<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: 170px; object-fit: cover;" autoplay playsinline webkit-playsinline muted></video>
|
||||
<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>
|
||||
<p id="transferCameraHelpText" style="font-size: 0.76rem; color: #f8fafc; line-height: 1.3;">Toca el botón para permitir la cámara</p>
|
||||
<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;">
|
||||
Permitir Cámara
|
||||
</button>
|
||||
<div id="transferCameraHelpText" style="font-size: 0.76rem; color: #f8fafc; line-height: 1.35;">Toca el botón para permitir la cámara</div>
|
||||
<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
|
||||
</button>
|
||||
</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;">
|
||||
<span style="font-size: 0.74rem; color: #64748b; font-weight: 700;">O selecciona a un compañero para transferirle:</span>
|
||||
<div id="quickTransferFriends" style="display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; margin-top: 8px;">
|
||||
|
||||
+180
-20
@@ -8,8 +8,42 @@ let activeCategoryId = null;
|
||||
let cart = [];
|
||||
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
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
checkHttpsEnvironment();
|
||||
|
||||
// Registrar Service Worker para PWA si está soportado
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.register('/sw.js').catch(err => console.log('SW error:', err));
|
||||
@@ -480,7 +514,6 @@ function openTransferModal() {
|
||||
document.getElementById('modalTransfer').style.display = 'flex';
|
||||
resetTransferScan();
|
||||
renderQuickTransferFriends();
|
||||
startTransferCamera();
|
||||
}
|
||||
|
||||
function closeTransferModal(e) {
|
||||
@@ -551,6 +584,8 @@ async function startTransferCamera(isUserAction = false) {
|
||||
const video = document.getElementById('transferVideo');
|
||||
const status = document.getElementById('transferCameraStatus');
|
||||
const helpOverlay = document.getElementById('transferCameraHelp');
|
||||
const helpText = document.getElementById('transferCameraHelpText');
|
||||
const actionContainer = document.getElementById('transferCameraActionContainer');
|
||||
const retryBtn = document.getElementById('btnRetryTransferCamera');
|
||||
const flipBtn = document.getElementById('btnFlipTransferCamera');
|
||||
|
||||
@@ -558,34 +593,86 @@ async function startTransferCamera(isUserAction = false) {
|
||||
|
||||
stopTransferCamera();
|
||||
|
||||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||
if (status) status.textContent = '⚠️ Cámara requiere HTTPS';
|
||||
const isHttp = window.location.protocol !== '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) {
|
||||
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;
|
||||
}
|
||||
|
||||
// Si estamos en un contexto seguro HTTPS:
|
||||
try {
|
||||
if (status) status.textContent = 'Conectando cámara...';
|
||||
if (helpOverlay) helpOverlay.style.display = 'none';
|
||||
|
||||
try {
|
||||
transferVideoStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: { facingMode: { ideal: transferFacingMode }, width: { ideal: 720 } },
|
||||
audio: false
|
||||
});
|
||||
} catch (err1) {
|
||||
// Fallback a cualquier lente disponible
|
||||
transferVideoStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
|
||||
// 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 {
|
||||
stream = await navigator.mediaDevices.getUserMedia(c);
|
||||
if (stream) break;
|
||||
} catch (errAttempt) {
|
||||
lastError = errAttempt;
|
||||
}
|
||||
}
|
||||
|
||||
video.setAttribute('playsinline', 'true');
|
||||
video.setAttribute('autoplay', 'true');
|
||||
video.muted = true;
|
||||
video.srcObject = transferVideoStream;
|
||||
if (!stream) {
|
||||
throw lastError || new Error('No se pudo acceder al lente de la cámara');
|
||||
}
|
||||
|
||||
await video.play();
|
||||
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();
|
||||
} catch (playErr) {
|
||||
console.warn('Reproducción diferida:', playErr);
|
||||
}
|
||||
|
||||
if (status) status.textContent = '🟢 Escaneando QR...';
|
||||
if (helpOverlay) helpOverlay.style.display = 'none';
|
||||
@@ -594,17 +681,90 @@ async function startTransferCamera(isUserAction = false) {
|
||||
|
||||
isTransferScanning = true;
|
||||
startTransferQrDetection(video);
|
||||
|
||||
} catch (err) {
|
||||
console.warn('Error iniciando cámara de transferencia:', err);
|
||||
if (status) status.textContent = '⚪ Cámara inactiva';
|
||||
console.warn('Error accediendo a cámara:', err);
|
||||
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) {
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
transferFacingMode = transferFacingMode === 'environment' ? 'user' : 'environment';
|
||||
startTransferCamera(true);
|
||||
|
||||
+173
-20
@@ -8,8 +8,40 @@ let currentTab = 'mostrador';
|
||||
let sseSource = null;
|
||||
let videoStream = null;
|
||||
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 () => {
|
||||
checkPosHttpsEnvironment();
|
||||
await loadCatalog();
|
||||
await loadPreOrders();
|
||||
initCamera();
|
||||
@@ -147,6 +179,8 @@ async function initCamera(isUserAction = false) {
|
||||
const video = document.getElementById('scannerVideo');
|
||||
const status = document.getElementById('cameraStatus');
|
||||
const overlay = document.getElementById('cameraHelpOverlay');
|
||||
const helpText = document.getElementById('cameraHelpText');
|
||||
const actionContainer = document.getElementById('posCameraActionContainer');
|
||||
const retryBtn = document.getElementById('btnRetryPosCamera');
|
||||
const flipBtn = document.getElementById('btnFlipPosCamera');
|
||||
|
||||
@@ -155,35 +189,83 @@ async function initCamera(isUserAction = false) {
|
||||
videoStream = null;
|
||||
}
|
||||
|
||||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||
status.textContent = '⚠️ Requiere HTTPS';
|
||||
const isHttp = window.location.protocol !== '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) {
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
status.textContent = 'Conectando lente...';
|
||||
try {
|
||||
videoStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: { facingMode: { ideal: posFacingMode }, width: { ideal: 1280 } },
|
||||
audio: false
|
||||
});
|
||||
} catch (err1) {
|
||||
// Fallback a cualquier cámara disponible
|
||||
videoStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
|
||||
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 {
|
||||
stream = await navigator.mediaDevices.getUserMedia(c);
|
||||
if (stream) break;
|
||||
} catch (errAttempt) {
|
||||
lastError = errAttempt;
|
||||
}
|
||||
}
|
||||
|
||||
video.setAttribute('playsinline', 'true');
|
||||
video.setAttribute('autoplay', 'true');
|
||||
video.muted = true;
|
||||
video.srcObject = videoStream;
|
||||
if (!stream) {
|
||||
throw lastError || new Error('No se pudo acceder a la cámara');
|
||||
}
|
||||
|
||||
await video.play();
|
||||
videoStream = stream;
|
||||
video.srcObject = stream;
|
||||
|
||||
status.textContent = '🟢 Escáner activo';
|
||||
await new Promise((resolve) => {
|
||||
if (video.readyState >= 1) {
|
||||
resolve();
|
||||
} else {
|
||||
video.onloadedmetadata = () => resolve();
|
||||
setTimeout(resolve, 800);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
await video.play();
|
||||
} catch (playErr) {
|
||||
console.warn('Reproducción diferida de cámara POS:', playErr);
|
||||
}
|
||||
|
||||
if (status) status.textContent = '🟢 Escáner activo';
|
||||
if (overlay) overlay.style.display = 'none';
|
||||
if (retryBtn) retryBtn.style.display = 'none';
|
||||
if (flipBtn) flipBtn.style.display = 'inline-block';
|
||||
@@ -191,15 +273,86 @@ async function initCamera(isUserAction = false) {
|
||||
startUniversalQrDetection(video);
|
||||
} catch (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) {
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
posFacingMode = posFacingMode === 'environment' ? 'user' : 'environment';
|
||||
initCamera(true);
|
||||
|
||||
+24
-7
@@ -29,9 +29,16 @@
|
||||
.status-listo { background: #dcfce7; color: #166534; }
|
||||
.status-entregado { background: #f1f5f9; color: #64748b; }
|
||||
</style>
|
||||
</head>
|
||||
<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">
|
||||
<!-- PANEL IZQUIERDO: CATÁLOGO TÁCTIL Y COLA DE PRE-ÓRDENES -->
|
||||
<div class="pos-catalog-pane">
|
||||
@@ -109,17 +116,27 @@
|
||||
</div>
|
||||
|
||||
<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 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>
|
||||
<p id="cameraHelpText" style="font-size: 0.78rem; color: #f8fafc; line-height: 1.3;">Haz clic en Activar Cámara para escanear</p>
|
||||
<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;">
|
||||
Permitir Cámara
|
||||
</button>
|
||||
<div id="cameraHelpText" style="font-size: 0.78rem; color: #f8fafc; line-height: 1.35;">Haz clic en Activar Cámara para escanear</div>
|
||||
<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
|
||||
</button>
|
||||
</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) -->
|
||||
<div style="margin-top: 10px; display: flex; gap: 6px; flex-wrap: wrap; justify-content: center;">
|
||||
<span style="font-size: 0.7rem; color: #94a3b8; width: 100%;">Prueba rápida de escaneo de carné:</span>
|
||||
|
||||
@@ -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
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🚀 Servidor RecreoPay iniciado en http://localhost:${PORT}`);
|
||||
|
||||
Reference in New Issue
Block a user