|
|
<!DOCTYPE html>
|
|
|
<html lang="en">
|
|
|
<head>
|
|
|
<meta charset="UTF-8">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
<title>Сканирование штрих-кода</title>
|
|
|
<script src="https://cdn.jsdelivr.net/npm/quagga/dist/quagga.min.js"></script>
|
|
|
<style>
|
|
|
#scanner-container {
|
|
|
width: 100%;
|
|
|
max-width: 640px;
|
|
|
margin: auto;
|
|
|
text-align: center;
|
|
|
}
|
|
|
#video-preview {
|
|
|
width: 100%;
|
|
|
height: auto;
|
|
|
}
|
|
|
#result {
|
|
|
margin-top: 20px;
|
|
|
}
|
|
|
.product-info {
|
|
|
margin-top: 20px;
|
|
|
text-align: left;
|
|
|
font-family: Arial, sans-serif;
|
|
|
}
|
|
|
.product-info ul {
|
|
|
list-style: none;
|
|
|
padding: 0;
|
|
|
}
|
|
|
.product-info li {
|
|
|
margin-bottom: 8px;
|
|
|
}
|
|
|
</style>
|
|
|
</head>
|
|
|
<body>
|
|
|
<h1>Сканирование штрих-кода</h1>
|
|
|
<div id="scanner-container">
|
|
|
<div id="video-preview"></div>
|
|
|
<p id="result">Результат: <span id="barcode-result">---</span></p>
|
|
|
<div id="product-details" class="product-info" style="display:none;">
|
|
|
<h2>Информация о товаре</h2>
|
|
|
<ul id="product-info-list"></ul>
|
|
|
</div>
|
|
|
<button id="stop-button" style="display:none;">Остановить сканирование</button>
|
|
|
</div>
|
|
|
|
|
|
<script>
|
|
|
const resultElement = document.getElementById("barcode-result");
|
|
|
const stopButton = document.getElementById("stop-button");
|
|
|
const productDetails = document.getElementById("product-details");
|
|
|
const productInfoList = document.getElementById("product-info-list");
|
|
|
|
|
|
// Инициализация Quagga
|
|
|
function startScanner() {
|
|
|
Quagga.init({
|
|
|
inputStream: {
|
|
|
name: "Live",
|
|
|
type: "LiveStream",
|
|
|
target: document.querySelector("#video-preview"),
|
|
|
},
|
|
|
decoder: {
|
|
|
readers: ["ean_reader", "code_128_reader", "upc_reader"],
|
|
|
},
|
|
|
}, function(err) {
|
|
|
if (err) {
|
|
|
console.error(err);
|
|
|
alert("Ошибка инициализации камеры");
|
|
|
return;
|
|
|
}
|
|
|
Quagga.start();
|
|
|
stopButton.style.display = "inline";
|
|
|
});
|
|
|
|
|
|
Quagga.onDetected(function(data) {
|
|
|
const barcode = data.codeResult.code;
|
|
|
resultElement.innerText = barcode;
|
|
|
Quagga.stop();
|
|
|
stopButton.style.display = "none";
|
|
|
|
|
|
// Отправка штрих-кода на сервер
|
|
|
fetch('/warehouse/process-barcode/', {
|
|
|
method: 'POST',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
},
|
|
|
body: JSON.stringify({ barcode: barcode }) // Передача данных через тело запроса
|
|
|
})
|
|
|
.then(response => response.json())
|
|
|
.then(product => {
|
|
|
if (product.error) {
|
|
|
alert(product.error);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// Отображение информации о товаре
|
|
|
productInfoList.innerHTML = ''; // Очистка списка
|
|
|
for (const [key, value] of Object.entries(product)) {
|
|
|
const listItem = document.createElement('li');
|
|
|
listItem.innerText = `${key}: ${value}`;
|
|
|
productInfoList.appendChild(listItem);
|
|
|
}
|
|
|
productDetails.style.display = "block";
|
|
|
})
|
|
|
.catch(error => {
|
|
|
console.error('Ошибка:', error);
|
|
|
alert('Продукт не найден или ошибка на сервере');
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// Остановка сканера
|
|
|
stopButton.addEventListener("click", () => {
|
|
|
Quagga.stop();
|
|
|
stopButton.style.display = "none";
|
|
|
});
|
|
|
|
|
|
// Запуск сканера
|
|
|
document.addEventListener("DOMContentLoaded", startScanner);
|
|
|
</script>
|
|
|
</body>
|
|
|
</html> |