// frontend/js/script.js document.addEventListener('DOMContentLoaded', function() { const searchButton = document.getElementById('searchButton'); const licensePlateInput = document.getElementById('licensePlate'); const errorMessage = document.getElementById('errorMessage'); console.log('页面加载完成 - 挪车电话查询页面'); // 添加搜索频率限制 let lastSearchTime = 0; const SEARCH_DELAY = 1000; // 1秒内只能搜索一次 searchButton.addEventListener('click', function() { const plate = licensePlateInput.value.trim(); console.log(`用户尝试搜索车牌号码: ${plate}`); // 检查输入长度 if (plate.length < 3) { console.warn('输入字符少于3个'); showError('请输入至少3个字符进行搜索'); return; } // 检查搜索频率 const currentTime = new Date().getTime(); if (currentTime - lastSearchTime < SEARCH_DELAY) { console.warn('搜索过于频繁'); showError('搜索过于频繁,请稍后再试'); return; } lastSearchTime = currentTime; if (!plate) { console.warn('未输入车牌号码'); showError('请输入车牌号码'); return; } // 从后端API获取数据 console.log(`发送请求到: /api/cars/search?plate=${encodeURIComponent(plate)}`); fetch(`/api/cars/search?plate=${encodeURIComponent(plate)}`) .then(response => { console.log(`收到响应状态: ${response.status}`); if (!response.ok) { throw new Error('网络响应错误'); } return response.json(); }) .then(data => { console.log(`收到数据,共 ${data.length} 条记录`); if (data.length > 0) { // 取第一个匹配的结果 const carInfo = data[0]; console.log(`找到车辆信息: ${JSON.stringify(carInfo)}`); // 将车辆信息存储到 sessionStorage sessionStorage.setItem('carInfo', JSON.stringify(carInfo)); console.log('跳转到结果页面'); // 跳转到结果页面 window.location.href = 'result.html'; } else { console.warn('未找到匹配的车辆信息'); showError('没有找到该车牌号码对应的车主信息'); } }) .catch(error => { console.error('请求失败:', error); showError('查询失败,请稍后再试'); }); }); // 回车键触发搜索 licensePlateInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { console.log('用户按下回车键触发搜索'); searchButton.click(); } }); function showError(message) { console.log(`显示错误信息: ${message}`); errorMessage.textContent = message; errorMessage.style.display = 'block'; // 3秒后隐藏错误信息 setTimeout(() => { console.log('隐藏错误信息'); errorMessage.style.display = 'none'; }, 3000); } });