从接口中获取数据

This commit is contained in:
halliday 2025-10-11 16:21:39 +08:00
parent 1008ae50d3
commit 72f10cc044
3 changed files with 35 additions and 42 deletions

11
mvcar/Pipfile Normal file
View File

@ -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"

BIN
mvcar/data/cars.db Normal file

Binary file not shown.

View File

@ -3,26 +3,10 @@ document.addEventListener('DOMContentLoaded', function() {
const licensePlateInput = document.getElementById('licensePlate'); const licensePlateInput = document.getElementById('licensePlate');
const errorMessage = document.getElementById('errorMessage'); const errorMessage = document.getElementById('errorMessage');
// 从JSON文件获取车辆数据库
let carDatabase = [];
let isDataLoaded = false;
// 添加搜索频率限制 // 添加搜索频率限制
let lastSearchTime = 0; let lastSearchTime = 0;
const SEARCH_DELAY = 1000; // 1秒内只能搜索一次 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() { searchButton.addEventListener('click', function() {
const plate = licensePlateInput.value.trim(); const plate = licensePlateInput.value.trim();
@ -45,32 +29,30 @@ document.addEventListener('DOMContentLoaded', function() {
return; return;
} }
// 检查数据是否已加载 // 从后端API获取数据
if (!isDataLoaded) { fetch(`http://localhost:5000/api/cars/search?plate=${encodeURIComponent(plate)}`)
showError('数据加载中,请稍后再试...'); .then(response => {
return; if (!response.ok) {
} throw new Error('网络响应错误');
}
// 改进的模糊匹配逻辑 - 要求至少3个连续字符匹配 return response.json();
const carInfo = carDatabase.find(car => { })
const lowerPlate = car.plate.toLowerCase(); .then(data => {
const searchPlate = plate.toLowerCase(); if (data.length > 0) {
// 取第一个匹配的结果
// 只有当搜索词长度>=3且能找到连续匹配时才返回true const carInfo = data[0];
if (searchPlate.length >= 3) { // 将车辆信息存储到 sessionStorage
return lowerPlate.includes(searchPlate); sessionStorage.setItem('carInfo', JSON.stringify(carInfo));
} // 跳转到结果页面
return false; window.location.href = 'result.html';
}); } else {
showError('没有找到该车牌号码对应的车主信息');
if (carInfo) { }
// 将车辆信息存储到 sessionStorage })
sessionStorage.setItem('carInfo', JSON.stringify(carInfo)); .catch(error => {
// 跳转到结果页面 console.error('请求失败:', error);
window.location.href = 'result.html'; showError('查询失败,请稍后再试');
} else { });
showError('没有找到该车牌号码对应的车主信息');
}
}); });
// 回车键触发搜索 // 回车键触发搜索