Create templates/index.html
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283<!DOCTYPE html><html lang="zh-TW"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>QR Code 盤點系統</title> <script src="https://unpkg.com/html5-qrcode" type="text/javascript"></script> <style> body { font-family: sans-serif; padding: 20px; max-width: 600px; margin: 0 auto; background: #f5f5f5;} h2 { text-align: center; } #reader { width: 100%; border-radius: 10px; overflow: hidden; } .result-area { margin-top: 20px; background: white; padding: 15px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); } .log-item { border-bottom: 1px solid #eee; padding: 10px 0; display: flex; justify-content: space-between; } .log-item:last-child { border-bottom: none; } .success-msg { color: green; font-weight: bold; text-align: center; display: none; } </style></head><body>
<h2>📦 倉庫盤點 / 簽到系統</h2> <div id="reader"></div> <p id="status-msg" class="success-msg">✅ 掃描成功!</p>
<div class="result-area"> <h3>📊 掃描紀錄 (最新 5 筆)</h3> <div id="log-list"> {% for record in records[:5] %} <div class="log-item"> <span>{{ record.content }}</span> <small>{{ record.time }}</small> </div> {% endfor %} </div> </div>
<script> // 成功掃描後的回呼函式 function onScanSuccess(decodedText, decodedResult) { console.log(`Scan result: ${decodedText}`); // 暫停掃描,避免重複送出 html5QrcodeScanner.pause(); // 傳送資料到後端 fetch('/api/scan', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ qr_content: decodedText }) }) .then(response => response.json()) .then(data => { // 顯示成功訊息 const msg = document.getElementById('status-msg'); msg.style.display = 'block'; msg.innerText = `✅ 已紀錄:${data.data.content}`;
// 更新下方列表 (簡單用 reload 演示,進階可用 DOM 操作) setTimeout(() => { window.location.reload(); }, 1000); }) .catch((error) => { console.error('Error:', error); alert('上傳失敗'); html5QrcodeScanner.resume(); }); }
function onScanFailure(error) { // 掃描失敗通常不需處理,因為每秒會嘗試很多次 }
// 初始化掃描器 let html5QrcodeScanner = new Html5QrcodeScanner( "reader", { fps: 10, qrbox: {width: 250, height: 250} }, /* verbose= */ false); html5QrcodeScanner.render(onScanSuccess, onScanFailure); </script></body></html>templates/index.htmlAdded+83