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