// 设备列表页 async function loadDevices() { const resp = await fetch("/api/devices"); const devices = await resp.json(); renderTable(devices); } function renderTable(devices) { const tbody = document.querySelector("#device-table tbody"); tbody.innerHTML = devices.map(d => ` ${d.serial} ${d.name || '(点击编辑)'} ${d.ip || '-'} ${d.state === 1 ? '在线' : '离线'} ${d.version || '-'} ${d.last_login || '-'} `).join(""); } function esc(s) { return s.replace(/'/g, "\\'").replace(/"/g, """); } async function editName(id, currentName, td) { const input = document.createElement("input"); input.value = currentName; td.innerHTML = ""; td.appendChild(input); input.focus(); async function save() { const name = input.value.trim(); td.textContent = name || "(点击编辑)"; await fetch(`/api/devices/${id}/name`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name }), }); } input.addEventListener("blur", save); input.addEventListener("keydown", e => { if (e.key === "Enter") save(); }); } loadDevices();