From 72f10cc0447c30801adf6ddffa1ef22d40960f2e Mon Sep 17 00:00:00 2001 From: halliday Date: Sat, 11 Oct 2025 16:21:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8E=E6=8E=A5=E5=8F=A3=E4=B8=AD=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mvcar/Pipfile | 11 ++++++ mvcar/data/cars.db | Bin 0 -> 16384 bytes mvcar/frontend/js/script.js | 66 +++++++++++++----------------------- 3 files changed, 35 insertions(+), 42 deletions(-) create mode 100644 mvcar/Pipfile create mode 100644 mvcar/data/cars.db 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 0000000000000000000000000000000000000000..633094d4ed3c32e5d6ff7224265611c6ca7d4c8c GIT binary patch literal 16384 zcmeI(O=}ZD90u^2omy)!j+c;Q#zRPJ8gaYg3tmj?Ai;Fg#toQL*fuK$(v3}0@ZzDO z2X7uKDtc7vOAkd5wD=u7Nxwjon6n@rof(rt1K#wYJP(dokUSo_^*kyC{`QT~IlV_yhQ@y}{NAOHafKmY;| zfB*y_009U<00MtTV1bRt(`j06`%CqpwOUtCxRqA0x)y}XqtmguqRe_yc-h&!6eDXV zLw_ZhRy(KGlk{*r7Nd7F=9f7Gq30`Sh01_6!e6~j;hA^J;HbSTQwjMfB*y_009U<00Izz z00bZa0SNphfh0Rb5~&m2_fId+=-7^vIiq5_&dZJd>uuTF**t0KW$Es_H*>a$Gm~*t zOxJm_x%cUD=fk6~4`1~jd`wZjHo3R6HGB5l`3t9Q$Ij?Wo38tOtGBV;e|EqBWIdwp ziSb>+zwwS55C}j30uX=z1Rwwb2tWV=5P$##{vClROVIuE1ZHN#V*~3bOB~rh7|_4} nQ~sT(7XkqYKmY;|fB*y_009U<00Izzz<(t$L0OEN{{j32rOBoq literal 0 HcmV?d00001 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('查询失败,请稍后再试'); + }); }); // 回车键触发搜索