From 59a7f8529301bee3416e995ee7d76736c8abb0e0 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Tue, 30 Jun 2026 22:50:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=95=B0=E6=8D=AE=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E9=80=BB=E8=BE=91=EF=BC=8C=E6=9B=B4=E6=96=B0API?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=EF=BC=8C=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- water-admin/dashboard.js | 91 ++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/water-admin/dashboard.js b/water-admin/dashboard.js index d957d1b..82297e2 100644 --- a/water-admin/dashboard.js +++ b/water-admin/dashboard.js @@ -1,5 +1,5 @@ -const SCORE_API = "http://127.0.0.1:8000/data/score?token=mE7yG0kI"; -const HISTORY_API = "http://127.0.0.1:8000/data/all?token=mE7yG0kI"; +const API_BASE = "http://127.0.0.1:8000"; +const DATA_API = API_BASE + "/data/integration"; let phChart = null; let tdsChart = null; @@ -49,19 +49,24 @@ function createChart(chart, title, color) { }, series: [{ + name: title, type: "line", smooth: true, symbol: "none", + lineStyle: { width: 3, color: color }, + areaStyle: { opacity: 0.15, color: color }, + data: [] + }] }); @@ -85,7 +90,7 @@ function initCharts() { } /* =========================== - Update Item + Update Card =========================== */ function setItem(name, data) { @@ -102,63 +107,58 @@ function setItem(name, data) { } /* =========================== - Load Score + Load Data =========================== */ async function loadData() { try { - const res = await fetch(SCORE_API); + const res = await fetch(DATA_API); - const data = await res.json(); + const result = await res.json(); - document.getElementById("score").innerHTML = data.score; + if (result.code !== 200) { - document.getElementById("level").innerHTML = data.level; + console.error(result.message); + return; + + } + + /* =========================== + Score + =========================== */ + + const score = result.score; + + document.getElementById("score").innerHTML = + score.score; + + document.getElementById("level").innerHTML = + score.level; document.getElementById("time").innerHTML = - data.time.substring(11, 19); + result.time.substring(11, 19); const abnormal = document.getElementById("abnormal"); abnormal.innerHTML = - data.abnormal ? "异常" : "正常"; + score.abnormal ? "异常" : "正常"; abnormal.className = "card-value " + - (data.abnormal ? "abnormal" : "normal"); + (score.abnormal ? "abnormal" : "normal"); - setItem("ph", data.ph); - setItem("tds", data.tds); - setItem("turbidity", data.turbidity); + setItem("ph", score.ph); + setItem("tds", score.tds); + setItem("turbidity", score.turbidity); - } - catch (e) { + /* =========================== + History + =========================== */ - console.error("Load Score Error:", e); - - } - -} - -/* =========================== - Load History -=========================== */ - -async function loadHistory() { - - try { - - const res = await fetch(HISTORY_API); - - const all = await res.json(); - - if (!Array.isArray(all) || all.length === 0) - return; - - const history = all.slice(-100); + const history = result.data.slice(-100); const time = history.map(item => item.time.substring(11, 19)); @@ -208,7 +208,7 @@ async function loadHistory() { } catch (e) { - console.error("Load History Error:", e); + console.error("Load Data Error:", e); } @@ -233,11 +233,14 @@ window.addEventListener("resize", () => { requestAnimationFrame(() => { - if (phChart) phChart.resize(); + if (phChart) + phChart.resize(); - if (tdsChart) tdsChart.resize(); + if (tdsChart) + tdsChart.resize(); - if (turbidityChart) turbidityChart.resize(); + if (turbidityChart) + turbidityChart.resize(); }); @@ -263,16 +266,12 @@ window.addEventListener("load", async () => { await loadData(); - await loadHistory(); - setInterval(updateClock, 1000); setInterval(async () => { await loadData(); - await loadHistory(); - }, 3000); }); \ No newline at end of file