feat(registry): 迁移至 Rust + Axum + SQLite 并新增编辑、分页和档案同步功能

- 后端从 Node.js 迁移到 Rust,使用 Axum 框架和 SQLite 存储
- 新增档案编辑对话框,支持修改机型、登记人及替换/删除照片
- 档案列表增加分页导航(每页20条)
- 新增档案同步状态提示及手动重试同步按钮
- Node.js 原版后端及网页保留为回退方案,移至 legacy-web 目录
- 更新 .gitignore 添加 Rust 构建产物(target、dist)
- 更新 README 文档,补充 Rust 启动、开发、检查及打包说明
- 启动源码版.cmd 默认转发至 Rust 版启动脚本
```
This commit is contained in:
2026-10-21 11:54:38 +08:00
parent 85924f35ff
commit 4d55796cb3
39 changed files with 5925 additions and 39 deletions
+1
View File
@@ -3,3 +3,4 @@
.action-link{display:grid;place-items:center;text-decoration:none}.history-cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:14px}.archive-card{border:1px solid #dfe8f0;border-radius:12px;padding:16px;background:#fbfdff}.archive-card h3{font-size:15px;margin:0 0 5px;overflow-wrap:anywhere}.archive-card strong{color:#236d9b;font-size:13px}.archive-card p{color:#8193a3;font-size:11px;line-height:1.5;margin:9px 0 12px}.archive-thumbs{display:grid;grid-template-columns:1fr 1fr;gap:8px}.archive-thumbs>div{height:105px;border:1px solid #e2e9ef;border-radius:7px;background:#f3f7fa;display:grid;place-items:center;color:#a4b1bc;font-size:11px;overflow:hidden}.archive-thumbs img{height:100%;width:100%;object-fit:contain}.archive-actions{display:flex;flex-wrap:wrap;gap:8px;margin-top:13px}.archive-actions a,.archive-actions button{background:#eaf3f9;color:#236f9d;border:1px solid #d0e2ef;border-radius:7px;padding:7px 9px;font-size:11px;font-weight:700;text-decoration:none;cursor:pointer}
.topbar .brand-mark{width:42px;height:42px;object-fit:contain;border-radius:10px;padding:0;letter-spacing:normal;background:#fff}.hero h1{margin-top:0}.history-head{align-items:center}.history-controls{display:flex;align-items:center;justify-content:flex-end;flex-wrap:wrap;gap:10px}.select-all{display:inline-flex;align-items:center;gap:5px;margin:0;color:#49657a;white-space:nowrap;font-size:12px}.select-all input,.archive-select input{width:16px;height:16px;accent-color:#206d9f;cursor:pointer}.export-button{height:34px;padding:0 11px;border:1px solid #c8ddeb;border-radius:7px;background:#e9f3fa;color:#1d6c9c;font-size:12px;font-weight:700}.export-button:disabled{opacity:.48;cursor:not-allowed}.history-message{font-size:11px;line-height:1.55;color:#8a9dad;margin:0 0 14px}.history-message.error{color:#b84036}.archive-card{position:relative}.archive-card.selected{border-color:#4b9bc8;box-shadow:0 0 0 2px #4b9bc820}.archive-select{display:flex;align-items:center;gap:5px;float:right;margin:0 0 8px 10px;color:#59788f;font-size:11px;font-weight:600}.archive-actions .delete-record{background:#fff1ef;color:#b34039;border-color:#f2c9c5}.archive-actions .delete-record:hover{background:#ffe7e3}footer{justify-content:flex-end}@media(max-width:850px){.history-head{align-items:flex-start;flex-direction:column}.history-controls{justify-content:flex-start}}@media(max-width:530px){.history-controls{gap:8px}.export-button{flex:1}}
.history-search{position:relative;width:260px;flex:0 1 260px}.history-search input{height:36px;padding:0 66px 0 12px;font-size:12px;background:#fff}.history-search input::placeholder{color:#8fa1af}.history-search #search-count{position:absolute;right:10px;top:50%;transform:translateY(-50%);color:#6b8294;font-size:11px;white-space:nowrap;pointer-events:none}.history-search #search-count:empty{display:none}@media(max-width:850px){.history-controls{width:100%}.history-search{flex:1 1 260px}}@media(max-width:530px){.history-search{flex-basis:100%;width:100%}}
.pagination{display:flex;gap:12px;justify-content:flex-end;align-items:center;flex-wrap:wrap;margin-top:20px;color:#59788f;font-size:12px}.pagination button,.archive-warning button{padding:7px 12px;border:1px solid #c8ddeb;border-radius:7px;background:#e9f3fa;color:#1d6c9c;cursor:pointer}.pagination button:disabled{opacity:.45;cursor:default}.archive-warning{padding:14px;background:#fff4da;color:#725319;border-radius:8px}dialog{border:1px solid #dce6ee;border-radius:16px;padding:28px;max-width:650px;width:calc(100% - 32px);max-height:90vh;overflow:auto;color:#173047}dialog::backdrop{background:#17304770}dialog h2{margin-top:0}dialog p{font-size:13px;line-height:1.6}dialog .edit-help{color:#61788a}.edit-photos{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-top:20px}.edit-photos fieldset{min-width:0;border:1px solid #dce6ee;border-radius:8px}.edit-photos img{width:100%;height:130px;object-fit:contain}.edit-photos input[type=file]{width:100%;font-size:12px}.edit-delete{display:flex;align-items:center;gap:8px}.edit-delete input{width:16px;height:16px}button:focus-visible,a:focus-visible{outline:3px solid #206d9f;outline-offset:3px}#edit-message{color:#a03f31}@media(max-width:530px){.edit-photos{grid-template-columns:1fr}dialog{padding:18px}.pagination{justify-content:center}}
+116 -7
View File
@@ -4,6 +4,22 @@ let listedRecords = [];
const selectedIds = new Set();
let listRequestId = 0;
let searchTimer;
let currentPage = 1;
let totalPages = 0;
let editingRecord = null;
let editBusy = false;
function updatePageButtons() {
$('previous-page').disabled = currentPage <= 1;
$('next-page').disabled = currentPage >= totalPages;
}
function archiveStatus(payload) {
if (payload.archiveStatus === 'pending') {
$('archive-warning').hidden = false;
$('archive-warning-text').textContent = payload.warning || '数据已保存,档案备份待同步。';
} else if (payload.archiveStatus === 'synced') $('archive-warning').hidden = true;
}
function setMessage(message, error = false) {
$('form-message').textContent = message;
@@ -68,17 +84,31 @@ function drawRecord(record) {
$('qr-stage').innerHTML = qr.createSvgTag({ cellSize: 6, margin: 4 });
}
async function loadRecords() {
async function loadRecords(requestedPage = currentPage) {
const requestId = ++listRequestId;
const query = $('history-query').value.trim();
const response = await fetch(`/api/records?q=${encodeURIComponent(query)}`);
const payload = await response.json();
if (!response.ok) throw new Error(payload.error || '读取记录失败');
$('previous-page').disabled = true; $('next-page').disabled = true;
let payload;
try {
const response = await fetch(`/api/records?q=${encodeURIComponent(query)}&page=${requestedPage}&pageSize=20`);
payload = await response.json();
if (!response.ok) throw new Error(payload.error || '读取记录失败');
} catch (error) {
if (requestId !== listRequestId) return;
updatePageButtons();
throw error;
}
if (requestId !== listRequestId) return;
totalPages = payload.totalPages;
if (requestedPage > Math.max(1, totalPages)) { selectedIds.clear(); return loadRecords(Math.max(1, totalPages)); }
currentPage = requestedPage;
$('page-status').textContent = `第 ${currentPage} / ${Math.max(1, totalPages)} 页 · 共 ${payload.total} 条`;
$('previous-page').disabled = currentPage === 1;
$('next-page').disabled = currentPage >= totalPages;
const body = $('history-cards');
body.replaceChildren();
listedRecords = payload.records;
$('search-count').textContent = query ? `${listedRecords.length} 条` : '';
$('search-count').textContent = query ? `${payload.total} 条` : '';
const currentIds = new Set(listedRecords.map(record => record.id));
for (const id of selectedIds) if (!currentIds.has(id)) selectedIds.delete(id);
$('history-empty').hidden = listedRecords.length > 0;
@@ -145,6 +175,7 @@ async function loadRecords() {
const response = await fetch(`/api/records/${record.id}`, { method: 'DELETE' });
const payload = await response.json();
if (!response.ok) throw new Error(payload.error || '删除失败');
archiveStatus(payload);
selectedIds.delete(record.id);
if (activeRecord?.id === record.id) {
activeRecord = null;
@@ -155,7 +186,10 @@ async function loadRecords() {
setHistoryMessage(`已删除 ${record.serialNo} 的档案。`);
} catch (error) { setHistoryMessage(error.message, true); remove.disabled = false; }
});
actions.append(open, download, showQr, remove);
const edit = document.createElement('button');
edit.type = 'button'; edit.textContent = '编辑资料 / 照片';
edit.addEventListener('click', () => openEditor(record));
actions.append(edit, open, download, showQr, remove);
card.append(select, title, code, detail, images, actions);
body.append(card);
}
@@ -185,6 +219,7 @@ $('register-form').addEventListener('submit', async event => {
});
const payload = await response.json();
if (!response.ok) throw new Error(payload.error || '保存失败');
archiveStatus(payload);
drawRecord(payload.record);
setMessage(payload.existing ? '该 SN 已登记,照片和填写的信息已保存。' : '登记成功,二维码已生成。');
await loadRecords();
@@ -202,7 +237,7 @@ $('history-query').addEventListener('input', () => {
card.querySelector('.archive-select input').checked = false;
});
updateSelectionControls();
searchTimer = setTimeout(() => loadRecords().catch(error => setHistoryMessage(error.message, true)), 180);
searchTimer = setTimeout(() => loadRecords(1).catch(error => setHistoryMessage(error.message, true)), 180);
});
$('select-all').addEventListener('change', event => {
selectedIds.clear();
@@ -272,3 +307,77 @@ $('download-qr').addEventListener('click', async () => {
});
loadRecords().catch(error => setMessage(error.message, true));
fetch('/api/archive/status').then(response => {
if (!response.ok) throw new Error('无法读取备份状态');
return response.json();
}).then(archiveStatus).catch(error => setHistoryMessage(error.message, true));
for (const [id, delta] of [['previous-page', -1], ['next-page', 1]]) {
$(id).addEventListener('click', () => {
selectedIds.clear(); updateSelectionControls();
document.querySelectorAll('.archive-select input').forEach(input => { input.checked = false; });
document.querySelectorAll('.archive-card.selected').forEach(card => card.classList.remove('selected'));
loadRecords(currentPage + delta).catch(error => {
setHistoryMessage(error.message, true);
updatePageButtons();
});
});
}
function openEditor(record) {
editingRecord = record; $('edit-form').reset(); $('edit-message').textContent = '';
$('edit-identity').textContent = `${record.serialNo} · ${record.uasCode}`;
$('edit-model').value = record.modelName; $('edit-owner').value = record.ownerName;
for (const slot of [1, 2]) {
const url = record[`photo${slot}Url`]; const preview = $(`edit-preview-${slot}`);
preview.hidden = !url; if (url) preview.src = `${url}?v=${Date.now()}`; else preview.removeAttribute('src');
$(`edit-delete-${slot}`).disabled = !url;
}
$('edit-dialog').showModal(); $('edit-model').focus();
}
$('edit-cancel').addEventListener('click', () => $('edit-dialog').close());
$('edit-dialog').addEventListener('cancel', event => { if (editBusy) event.preventDefault(); });
for (const slot of [1, 2]) {
$(`edit-photo-${slot}`).addEventListener('change', () => { if ($(`edit-photo-${slot}`).files.length) $(`edit-delete-${slot}`).checked = false; });
$(`edit-delete-${slot}`).addEventListener('change', () => { if ($(`edit-delete-${slot}`).checked) $(`edit-photo-${slot}`).value = ''; });
}
async function mutation(url, method, body) {
const response = await fetch(url, { method, headers: body ? { 'content-type': 'application/json' } : {}, body: body ? JSON.stringify(body) : undefined });
const payload = await response.json(); if (!response.ok) throw new Error(payload.error || '操作失败');
archiveStatus(payload); return payload;
}
$('edit-form').addEventListener('submit', async event => {
event.preventDefault(); const record = editingRecord; if (!record || editBusy) return;
editBusy = true;
const controls = Array.from($('edit-form').elements);
const disabled = controls.map(control => control.disabled);
controls.forEach(control => { control.disabled = true; });
const body = { modelName: $('edit-model').value.trim(), ownerName: $('edit-owner').value.trim() };
const deletes = [1, 2].map(slot => $(`edit-delete-${slot}`).checked);
let saved = false;
try {
// Validate both photos before committing any edits.
const photos = await Promise.all([readPhoto($('edit-photo-1'), 1), readPhoto($('edit-photo-2'), 2)]);
$('edit-message').textContent = '正在保存…';
for (const slot of [1, 2]) {
if (deletes[slot - 1]) body[`photo${slot}`] = null;
else if (photos[slot - 1]) body[`photo${slot}`] = photos[slot - 1];
}
const payload = await mutation(`/api/records/${record.id}`, 'PATCH', body);
saved = true;
if (activeRecord?.id === record.id) drawRecord(payload.record);
$('edit-dialog').close();
await loadRecords();
setHistoryMessage('档案修改已保存。');
} catch (error) {
if (saved) setHistoryMessage(`修改已保存,但列表刷新失败:${error.message}`, true);
else $('edit-message').textContent = `${error.message}。如连接中断,请刷新核对保存结果。`;
}
finally { controls.forEach((control, index) => { control.disabled = disabled[index]; }); editBusy = false; }
});
$('retry-archive').addEventListener('click', async () => {
$('retry-archive').disabled = true;
try { await mutation('/api/archive/retry', 'POST', {}); setHistoryMessage('档案与备份同步完成。'); }
catch (error) { $('archive-warning-text').textContent = error.message; }
finally { $('retry-archive').disabled = false; }
});
+14
View File
@@ -79,10 +79,24 @@
<p class="history-message" id="history-message" role="status">导出档案包包含离线档案卡、照片和登记信息;Excel 导出为登记表。</p>
<div id="history-empty" class="history-empty">暂无登记记录。</div>
<div id="history-cards" class="history-cards"></div>
<nav class="pagination" aria-label="档案分页"><span>全选仅当前页</span><button id="previous-page" type="button">上一页</button><span id="page-status" aria-live="polite"></span><button id="next-page" type="button">下一页</button></nav>
<p id="archive-warning" class="archive-warning" role="status" hidden><span id="archive-warning-text"></span> <button id="retry-archive" type="button">重试同步</button></p>
</section>
</main>
<footer><span>127.0.0.1:8787 · 数据保存在本机</span></footer>
</div>
<dialog id="edit-dialog" aria-labelledby="edit-title">
<form id="edit-form">
<h2 id="edit-title">编辑档案</h2><p id="edit-identity"></p><p class="edit-help">SN 与 FPV 编号不可修改;清空机型或登记人后保存,即清除该字段。</p>
<label for="edit-model">机型</label><input id="edit-model" maxlength="60">
<label for="edit-owner">登记人 / 备注</label><input id="edit-owner" maxlength="40">
<div class="edit-photos">
<fieldset><legend>照片 1</legend><img id="edit-preview-1" alt="现有照片 1" hidden><label for="edit-photo-1">替换照片 1</label><input id="edit-photo-1" type="file" accept="image/jpeg,image/png,image/webp"><label class="edit-delete"><input id="edit-delete-1" type="checkbox">删除现有照片 1</label></fieldset>
<fieldset><legend>照片 2</legend><img id="edit-preview-2" alt="现有照片 2" hidden><label for="edit-photo-2">替换照片 2</label><input id="edit-photo-2" type="file" accept="image/jpeg,image/png,image/webp"><label class="edit-delete"><input id="edit-delete-2" type="checkbox">删除现有照片 2</label></fieldset>
</div>
<p id="edit-message" role="status"></p><div class="result-actions"><button id="edit-save" class="secondary" type="submit">保存修改</button><button id="edit-cancel" class="ghost" type="button">取消</button></div>
</form>
</dialog>
</body>
</html>