从接口中获取数据

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 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('查询失败,请稍后再试');
});
});
// 回车键触发搜索