let timeLogLevel = ""; function setTimeLogLevel() { const level = logLevel.value; fetch("/api/time/loglevel", { method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({level}) }) .then(async r => { if (!r.ok) throw new Error(await r.text()); timeLogLevel = level; }) .catch(e => { logLevel.value = timeLogLevel; }); } function getTimeLogLevel() { fetch("/api/time/loglevel") .then(async r => { if (!r.ok) throw new Error(await r.text()); return r.json(); }) .then(d => { timeLogLevel = d.log_level; logLevel.value = d.log_level; }) .catch(e => { timeLogLevel = ""; logLevel.value = ""; }); } function downloadTimeLogFile(event) { const btn = event.currentTarget; btn.disabled = true; fetch("/api/time/logfile") .then(async r => { if (!r.ok) { throw new Error("HTTP " + r.status); } const disposition = r.headers.get("Content-Disposition"); let filename = "time.log.tar.gz"; if (disposition) { const match = disposition.match(/filename="?([^";]+)"?/); if (match) { filename = match[1]; } } return { blob: await r.blob(), filename }; }) .then(({ blob, filename }) => { const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); a.remove(); setTimeout(() => { URL.revokeObjectURL(url); }, 1000); alert("下载成功"); }) .catch(e => { console.error("download failed:", e); alert("下载失败: " + e.message); }) .finally(() => { btn.disabled = false; btn.textContent = "下载"; }); } function restartTimeService(event) { if (!confirm("确定重启时间服务吗?")) { return; } const btn = event.currentTarget; btn.disabled = true; fetch("/api/time/restart", { method: "POST" }) .then(async r => { if (!r.ok) { throw new Error(await r.text()); } logLevel.value = "info"; timeLogLevel = logLevel.value; alert("重启成功"); }) .catch(e => { console.error("restart failed:", e); alert("重启失败: " + e.message); }) .finally(() => { btn.disabled = false; }); } let timeStatusTimer = null; function getTimeStatus() { fetch("/api/time/status") .then(async r => { if (!r.ok) { throw new Error("HTTP " + r.status); } return r.json(); }) .then(data => { ["var1","var2","var3","var4","var5","var6"] .forEach(id => setText(id, data[id], "page3")); }) .catch(e => { console.error("getTimeStatus() failed:", e); ["var1","var2","var3","var4","var5","var6"] .forEach(id => setText(id, null, "page3")); }); getTimeLogLevel(); } function startTimeStatusRefresh() { if (timeStatusTimer) { return; } getTimeStatus(); timeStatusTimer = setInterval(() => { getTimeStatus(); }, 1000); } function stopTimeStatusRefresh() { if (timeStatusTimer) { clearInterval(timeStatusTimer); timeStatusTimer = null; } } function initPage3() { startTimeStatusRefresh() connectLog("yfkj-timesyncd.service", "text1"); } function exitPage3() { stopTimeStatusRefresh() disconnectLog(); }