diff --git a/mvcar/Pipfile b/mvcar/Pipfile new file mode 100644 index 0000000..f0a81bd --- /dev/null +++ b/mvcar/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.tuna.tsinghua.edu.cn/simple\n" +verify_ssl = true +name = "pip_conf_index_global" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.8" diff --git a/mvcar/data/cars.db b/mvcar/data/cars.db new file mode 100644 index 0000000..633094d Binary files /dev/null and b/mvcar/data/cars.db differ diff --git a/mvcar/frontend/js/script.js b/mvcar/frontend/js/script.js index 14eab39..450cd95 100644 --- a/mvcar/frontend/js/script.js +++ b/mvcar/frontend/js/script.js @@ -3,26 +3,10 @@ document.addEventListener('DOMContentLoaded', function() { const licensePlateInput = document.getElementById('licensePlate'); const errorMessage = document.getElementById('errorMessage'); - // 从JSON文件获取车辆数据库 - let carDatabase = []; - let isDataLoaded = false; - // 添加搜索频率限制 let lastSearchTime = 0; const SEARCH_DELAY = 1000; // 1秒内只能搜索一次 - // 加载车辆数据 - fetch('js/cars.json') - .then(response => response.json()) - .then(data => { - carDatabase = data; - isDataLoaded = true; - }) - .catch(error => { - console.error('加载车辆数据失败:', error); - showError('无法加载车辆数据库'); - }); - searchButton.addEventListener('click', function() { const plate = licensePlateInput.value.trim(); @@ -45,32 +29,30 @@ document.addEventListener('DOMContentLoaded', function() { return; } - // 检查数据是否已加载 - if (!isDataLoaded) { - showError('数据加载中,请稍后再试...'); - return; - } - - // 改进的模糊匹配逻辑 - 要求至少3个连续字符匹配 - const carInfo = carDatabase.find(car => { - const lowerPlate = car.plate.toLowerCase(); - const searchPlate = plate.toLowerCase(); - - // 只有当搜索词长度>=3且能找到连续匹配时才返回true - if (searchPlate.length >= 3) { - return lowerPlate.includes(searchPlate); - } - return false; - }); - - if (carInfo) { - // 将车辆信息存储到 sessionStorage - sessionStorage.setItem('carInfo', JSON.stringify(carInfo)); - // 跳转到结果页面 - window.location.href = 'result.html'; - } else { - showError('没有找到该车牌号码对应的车主信息'); - } + // 从后端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('查询失败,请稍后再试'); + }); }); // 回车键触发搜索