kunlang_config_server/bin/config.html

297 lines
8.5 KiB
HTML
Raw Permalink Normal View History

2025-12-12 09:02:06 +08:00
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>设备配置界面</title>
<style>
body {
font-family: "Segoe UI", "Helvetica Neue", Arial;
padding: 30px;
background: #f5f7fa;
}
h2 {
margin-bottom: 20px;
color: #333;
}
.card {
background: white;
border-radius: 8px;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
margin-bottom: 18px;
overflow: hidden;
}
.card-header {
padding: 12px 16px;
background: linear-gradient(90deg, #e8ecf3, #dce3ee);
cursor: pointer;
user-select: none;
font-weight: bold;
font-size: 16px;
display: flex;
justify-content: space-between;
align-items: center;
}
.arrow {
font-size: 14px;
}
.card-body {
overflow: hidden;
height: auto;
transition: height 0.25s ease;
}
.content-wrapper {
padding: 15px 20px;
display: grid;
grid-template-columns: 200px 1fr;
row-gap: 12px;
column-gap: 10px;
}
.collapsed {
height: 0 !important;
}
label {
font-weight: 600;
}
input {
padding: 6px 8px;
border: 1px solid #ccc;
border-radius: 5px;
width: 230px;
}
button {
margin-top: 25px;
padding: 12px 28px;
font-size: 16px;
background: #4a8df8;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
}
button:hover {
background: #3b78d4;
}
/* 错误样式 */
.input-error {
border: 2px solid #ff5b5b !important;
background: #ffecec;
}
.error-text {
color: #d9534f;
font-size: 12px;
grid-column: 1 / span 2;
}
/* Toast 提示 */
#toast {
position: fixed;
top: 20px;
right: 20px;
background: #4CAF50;
color: white;
padding: 12px 18px;
border-radius: 6px;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
opacity: 0;
pointer-events: none;
transition: opacity 0.3s, transform 0.3s;
transform: translateY(-10px);
font-size: 14px;
z-index: 999;
}
#toast.show {
opacity: 1;
transform: translateY(0);
}
</style>
</head>
<body>
<h2>设备配置界面</h2>
<div id="toast"></div>
<div id="form"></div>
<button onclick="saveConfig()">保存配置</button>
<script>
function showToast(msg, error = false) {
let t = document.getElementById("toast");
t.innerText = msg;
t.style.background = error ? "#d9534f" : "#4CAF50";
t.classList.add("show");
setTimeout(() => {
t.classList.remove("show");
}, 2000);
}
async function loadData() {
let resp = await fetch("/config/data");
let data = await resp.json();
let schema = data.schema;
let config = data.config;
window.schema_cache = schema;
window.title_map = {};
window.old_devno = config["vehicle"]["device_no"];
let html = "";
for (let section in schema) {
html += `
<div class="card">
<div class="card-header" onclick="toggleSection('${section}')">
[${section}]
<span id="${section}_arrow" class="arrow"></span>
</div>
<div id="${section}_content" class="card-body">
<div class="content-wrapper">
`;
for (let key in schema[section]) {
let item = schema[section][key];
let title = item.title;
let value = config[section][key];
window.title_map[title] = { section, key };
html += `
<label>${title}</label>
<input id="${section}_${key}" value="${value}">
`;
}
html += `
</div>
</div>
</div>
`;
}
document.getElementById("form").innerHTML = html;
}
function toggleSection(section) {
let outer = document.getElementById(section + "_content");
let inner = outer.querySelector(".content-wrapper");
let arrow = document.getElementById(section + "_arrow");
if (outer.classList.contains("collapsed")) {
outer.classList.remove("collapsed");
outer.style.height = "0px";
outer.offsetHeight;
outer.style.height = inner.scrollHeight + "px";
arrow.textContent = "▼";
outer.addEventListener("transitionend", function done() {
outer.style.height = "auto";
outer.removeEventListener("transitionend", done);
});
} else {
outer.style.height = inner.scrollHeight + "px";
outer.offsetHeight;
outer.style.height = "0";
outer.classList.add("collapsed");
arrow.textContent = "▶";
}
}
// ---------------------------
// 直接跳转到新域名
// ---------------------------
function jumpToNewDomain(newDevNo) {
let url = `http://${newDevNo}.local:18080/config`;
showToast(`设备编号已更新,跳转到 ${newDevNo}.local ...`);
setTimeout(() => {
window.location.href = url;
}, 500);
}
async function saveConfig() {
let schema = window.schema_cache;
let new_config = {};
for (let section in schema) {
new_config[section] = {};
for (let key in schema[section]) {
new_config[section][key] = document.getElementById(`${section}_${key}`).value;
}
}
let resp = await fetch("/config/save", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(new_config)
});
let result = await resp.json();
if (result.status === "error") {
showToast(result.message, true);
let match = result.message.match(/字段 \[(.*)\]/);
if (match) {
let title = match[1];
let pos = window.title_map[title];
if (pos) {
let input = document.getElementById(`${pos.section}_${pos.key}`);
let outer = document.getElementById(pos.section + "_content");
let arrow = document.getElementById(pos.section + "_arrow");
outer.classList.remove("collapsed");
outer.style.height = "auto";
arrow.textContent = "▼";
document.querySelectorAll(".input-error").forEach(el => el.classList.remove("input-error"));
document.querySelectorAll(".error-text").forEach(el => el.remove());
input.classList.add("input-error");
let err = document.createElement("div");
err.className = "error-text";
err.innerText = result.message;
input.insertAdjacentElement("afterend", err);
input.scrollIntoView({ behavior: "smooth", block: "center" });
}
}
return;
}
showToast("配置已保存");
// -------- 跳转到新域名 --------
let newDevNo = new_config["vehicle"]["device_no"];
if (newDevNo && newDevNo !== window.old_devno) {
jumpToNewDomain(newDevNo);
}
}
loadData();
</script>
</body>
</html>