90 lines
2.9 KiB
HTML
90 lines
2.9 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="es">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Escáner de Productos</title>
|
|
<script src="https://unpkg.com/@zxing/browser@latest"></script>
|
|
<style>
|
|
body { font-family: sans-serif; padding: 20px; }
|
|
input, button, select { display: block; margin: 10px 0; padding: 8px; width: 100%; }
|
|
video { width: 100%; border: 1px solid #ccc; border-radius: 8px; margin-bottom: 15px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Escanear Producto</h1>
|
|
|
|
<video id="video"></video>
|
|
<button onclick="startScanner()">Iniciar escáner</button>
|
|
|
|
<form id="productoForm" onsubmit="guardarProducto(event)">
|
|
<label for="codigo">Código de barras</label>
|
|
<input type="text" id="codigo" name="codigo" readonly required>
|
|
|
|
<label for="nombre">Nombre del producto</label>
|
|
<input type="text" id="nombre" name="nombre" required>
|
|
|
|
<label for="categoria">Categoría</label>
|
|
<input type="text" id="categoria" name="categoria">
|
|
|
|
<label for="precio">Precio</label>
|
|
<input type="number" step="0.01" id="precio" name="precio">
|
|
|
|
<label for="supermercado">Supermercado habitual</label>
|
|
<input type="text" id="supermercado" name="supermercado">
|
|
|
|
<button type="submit">Guardar</button>
|
|
</form>
|
|
|
|
<script>
|
|
let selectedDeviceId;
|
|
const codeReader = new ZXing.BrowserBarcodeReader();
|
|
|
|
async function startScanner() {
|
|
try {
|
|
const devices = await codeReader.getVideoInputDevices();
|
|
selectedDeviceId = devices[0].deviceId;
|
|
codeReader.decodeFromVideoDevice(selectedDeviceId, 'video', (result, err) => {
|
|
if (result) {
|
|
document.getElementById('codigo').value = result.text;
|
|
codeReader.reset(); // Detener escaneo tras encontrar uno
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
|
|
function guardarProducto(e) {
|
|
e.preventDefault();
|
|
const datos = {
|
|
codigo: document.getElementById('codigo').value,
|
|
nombre: document.getElementById('nombre').value,
|
|
categoria: document.getElementById('categoria').value,
|
|
precio: document.getElementById('precio').value,
|
|
supermercado: document.getElementById('supermercado').value
|
|
};
|
|
console.log('Producto guardado:', datos);
|
|
alert('Producto guardado (simulado). Puedes enviar estos datos por POST al servidor.');
|
|
|
|
// Aquí puedes hacer el fetch o POST a tu servidor:
|
|
fetch('https://tecnologia-facil.es/apis/guardar_producto.php', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(datos)
|
|
})
|
|
.then(res => res.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
alert('✅ Producto guardado con ID: ' + data.id);
|
|
form.reset();
|
|
} else {
|
|
alert('❌ Error al guardar: ' + data.error);
|
|
}
|
|
});
|
|
|
|
document.getElementById('productoForm').reset();
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |