AI 生成: 一个基于 Python Flask 的车辆管理系统,提供车辆的增删改查、搜索和统计功能。

This commit is contained in:
admin
2026-09-29 18:07:16 +08:00
commit 798550e1f3
8 changed files with 678 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
/* 车辆管理系统前端脚本 */
// 打开添加弹窗
function openAddModal() {
document.getElementById('addModal').classList.add('show');
}
// 关闭添加弹窗
function closeAddModal() {
document.getElementById('addModal').classList.remove('show');
document.getElementById('addForm').reset();
}
// 提交添加表单
async function submitAdd(e) {
e.preventDefault();
const form = e.target;
const data = Object.fromEntries(new FormData(form).entries());
// 年份转数字
if (data.year) data.year = parseInt(data.year, 10);
try {
const res = await fetch('/api/vehicles', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const result = await res.json();
if (!res.ok) {
alert('添加失败: ' + (result.error || '未知错误'));
return;
}
location.reload();
} catch (err) {
alert('请求失败: ' + err.message);
}
}
// 删除车辆
async function deleteVehicle(id) {
if (!confirm('确定要删除该车辆吗?')) return;
try {
const res = await fetch(`/api/vehicles/${id}`, { method: 'DELETE' });
if (!res.ok) {
alert('删除失败');
return;
}
location.reload();
} catch (err) {
alert('请求失败: ' + err.message);
}
}
// 提交编辑表单
async function submitEdit(e, id) {
e.preventDefault();
const form = e.target;
const data = Object.fromEntries(new FormData(form).entries());
if (data.year) data.year = parseInt(data.year, 10);
try {
const res = await fetch(`/api/vehicles/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const result = await res.json();
if (!res.ok) {
alert('更新失败: ' + (result.error || '未知错误'));
return;
}
window.location.href = '/';
} catch (err) {
alert('请求失败: ' + err.message);
}
}
// 点击弹窗外部关闭
window.addEventListener('click', function (e) {
const modal = document.getElementById('addModal');
if (modal && e.target === modal) closeAddModal();
});
+179
View File
@@ -0,0 +1,179 @@
/* 全局样式 */
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif;
background: #f4f6f9;
color: #333;
line-height: 1.6;
min-height: 100vh;
}
header {
background: linear-gradient(135deg, #2c3e50, #4a6572);
color: #fff;
padding: 30px 40px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
header h1 { font-size: 26px; }
.subtitle { font-size: 13px; opacity: 0.75; }
main {
max-width: 1200px;
margin: 30px auto;
padding: 0 20px;
}
/* 统计卡片 */
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 16px;
margin-bottom: 24px;
}
.stat-card {
background: #fff;
border-radius: 10px;
padding: 20px;
text-align: center;
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
transition: transform 0.2s;
}
.stat-card:hover { transform: translateY(-3px); }
.stat-num { display: block; font-size: 28px; font-weight: 700; color: #2c3e50; }
.stat-label { font-size: 13px; color: #888; }
/* 工具栏 */
.toolbar {
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
margin-bottom: 16px;
flex-wrap: wrap;
}
.search-form { display: flex; gap: 8px; flex: 1; max-width: 500px; }
.search-form input {
flex: 1;
padding: 10px 14px;
border: 1px solid #ddd;
border-radius: 6px;
font-size: 14px;
}
.search-form input:focus { outline: none; border-color: #4a6572; }
/* 按钮 */
.btn {
display: inline-block;
padding: 10px 18px;
background: #e0e0e0;
color: #333;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
text-decoration: none;
transition: background 0.2s;
}
.btn:hover { background: #d0d0d0; }
.btn-primary { background: #2c3e50; color: #fff; }
.btn-primary:hover { background: #1a252f; }
.btn-danger { background: #e74c3c; color: #fff; }
.btn-danger:hover { background: #c0392b; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-clear { background: #f39c12; color: #fff; }
/* 表格 */
.table-wrap {
background: #fff;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid #eee; font-size: 14px; }
th { background: #f8f9fb; font-weight: 600; color: #555; }
tbody tr:hover { background: #fafbfc; }
.empty { text-align: center; color: #999; padding: 40px 0; }
/* 状态徽章 */
.badge {
display: inline-block;
padding: 3px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
}
.badge-available { background: #d4edda; color: #155724; }
.badge-in_use { background: #cce5ff; color: #004085; }
.badge-maintenance { background: #fff3cd; color: #856404; }
/* 表单卡片 */
.form-card {
background: #fff;
border-radius: 10px;
padding: 30px;
max-width: 600px;
margin: 0 auto;
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
form label {
display: block;
margin-bottom: 16px;
font-size: 14px;
color: #555;
}
form input, form select {
display: block;
width: 100%;
margin-top: 6px;
padding: 10px 12px;
border: 1px solid #ddd;
border-radius: 6px;
font-size: 14px;
font-family: inherit;
}
form input:focus, form select:focus { outline: none; border-color: #4a6572; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
/* 弹窗 */
.modal {
display: none;
position: fixed;
inset: 0;
background: rgba(0,0,0,0.5);
z-index: 100;
align-items: center;
justify-content: center;
padding: 20px;
}
.modal.show { display: flex; }
.modal-content {
background: #fff;
padding: 30px;
border-radius: 12px;
width: 100%;
max-width: 500px;
max-height: 90vh;
overflow-y: auto;
}
.modal-content h2 { margin-bottom: 20px; color: #2c3e50; }
.actions { white-space: nowrap; }
.actions .btn { margin-right: 4px; }
@media (max-width: 768px) {
header { padding: 20px; }
.toolbar { flex-direction: column; align-items: stretch; }
.search-form { max-width: 100%; }
th, td { padding: 8px 10px; font-size: 13px; }
}