控制面板更新 HTML+JS切换为Vite+ React + Tailwind CSS

This commit is contained in:
xiaozhou233 committed 2026-07-10 20:45:54 +08:00
1 parent 4b06470590
commit fe0b92e4b4
28 files changed
+5235 -960

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
import Dashboard from './pages/Dashboard';
function App() {
return (
<Dashboard />
);
}
export default App;
+25
View File
@@ -0,0 +1,25 @@
import type { WaterData } from "../types/water.ts";
const API =
"https://api.xiaozhou233.cn/data/integration";
export async function getWaterData()
: Promise<WaterData> {
try {
const res = await fetch(API);
if (!res.ok) {
throw new Error(`HTTP Error: ${res.status}`);
}
const data = await res.json();
return data;
} catch (error) {
console.error("Failed to fetch water data:", error);
throw error;
}
}
+326
View File
@@ -0,0 +1,326 @@
.page {
width: 100%;
height: 100vh;
background: #eef3f8;
display: flex;
flex-direction: column;
overflow: hidden;
font-family: "Microsoft YaHei", Arial, sans-serif;
}
.banner {
height: 11vh;
min-height: 90px;
background: linear-gradient(90deg, #1565c0, #2196f3);
color: white;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 2vw;
box-shadow: 0 3px 12px rgba(0, 0, 0, .18);
flex-shrink: 0;
}
.title {
font-size: clamp(30px, 2vw, 52px);
font-weight: bold;
letter-spacing: 2px;
}
.subtitle {
margin-top: .5vh;
opacity: .9;
font-size: clamp(14px, .9vw, 22px);
}
.clockBox {
text-align: right;
font-size: clamp(16px, 1vw, 24px);
}
.clock {
margin-top: .6vh;
font-size: clamp(28px, 2vw, 46px);
font-weight: bold;
}
.container {
flex: 1;
display: flex;
flex-direction: column;
gap: 1.2vh;
padding: 1.2vh 1.2vw;
min-height: 0;
}
.cards {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1vw;
}
.card {
background: white;
border-radius: 16px;
padding: 1.2vw;
box-shadow: 0 4px 10px rgba(0, 0, 0, .08);
}
.card p {
color: #666;
font-size: clamp(18px, 1vw, 28px);
}
.card strong {
display: block;
margin-top: 1vh;
font-size: clamp(36px, 2.3vw, 64px);
font-weight: bold;
color: #111;
}
.normal {
color: #2ecc71 !important;
}
.abnormal {
color: #e74c3c !important;
}
.items {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1vw;
}
.item {
background: white;
border-radius: 16px;
padding: 1.2vw;
text-align: center;
box-shadow: 0 4px 10px rgba(0, 0, 0, .08);
}
.item h3 {
font-size: clamp(22px, 1.3vw, 36px);
}
.value {
margin-top: 1vh;
color: #1976d2;
font-size: clamp(48px, 3vw, 84px);
font-weight: bold;
}
.score {
margin-top: .5vh;
color: #666;
font-size: clamp(18px, 1vw, 26px);
}
.chartGrid {
flex: 1;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1vw;
min-height: 0;
}
.chart {
background: white;
border-radius: 16px;
display: flex;
flex-direction: column;
min-height: 0;
box-shadow: 0 4px 10px rgba(0, 0, 0, .08);
}
.chart h3 {
padding: 16px 20px;
border-bottom: 1px solid #e6e6e6;
font-size: clamp(22px, 1.3vw, 34px);
color: #333;
flex-shrink: 0;
}
.chartBody {
flex: 1;
min-height: 0;
padding: 12px;
}
.footer {
height: 3.5vh;
min-height: 28px;
display: flex;
justify-content: center;
align-items: center;
color: #666;
background: white;
border-top: 1px solid #ddd;
}
@media(max-width:1100px) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
.items {
grid-template-columns: repeat(2, 1fr);
}
.chartGrid {
grid-template-columns: 1fr;
}
.page {
overflow: auto;
}
}
@media(max-width:768px) {
.page {
height: auto;
min-height: 100vh;
overflow-y: auto;
overflow-x: hidden;
}
.banner {
height: auto;
min-height: 70px;
padding: 12px 16px;
justify-content: space-between;
}
.title {
font-size: clamp(24px, 5vw, 32px);
letter-spacing: 1px;
}
.subtitle {
margin-top: 4px;
font-size: clamp(12px, 3.5vw, 16px);
}
.clockBox {
font-size: clamp(12px, 3vw, 16px);
}
.clock {
margin-top: 2px;
font-size: clamp(20px, 4vw, 28px);
}
.container {
gap: 12px;
padding: 12px 12px;
}
.cards {
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
.card {
padding: 12px;
border-radius: 12px;
}
.card p {
font-size: clamp(14px, 3.5vw, 18px);
}
.card strong {
margin-top: 8px;
font-size: clamp(24px, 5vw, 36px);
}
.items {
grid-template-columns: 1fr;
gap: 10px;
}
.item {
padding: 12px;
border-radius: 12px;
}
.item h3 {
font-size: clamp(16px, 4vw, 22px);
}
.value {
margin-top: 8px;
font-size: clamp(32px, 7vw, 56px);
}
.score {
margin-top: 4px;
font-size: clamp(12px, 3vw, 16px);
}
.chartGrid {
grid-template-columns: 1fr;
gap: 10px;
flex: none;
height: auto;
}
.chart {
min-height: 300px;
border-radius: 12px;
}
.chart h3 {
padding: 12px 16px;
font-size: clamp(14px, 4vw, 18px);
}
.chartBody {
padding: 8px;
height: 250px;
}
.footer {
height: auto;
min-height: 40px;
padding: 12px;
font-size: clamp(12px, 3vw, 14px);
}
}
@media(max-width:480px) {
.cards {
grid-template-columns: 1fr;
}
.banner {
padding: 10px 12px;
}
.title {
font-size: clamp(20px, 5vw, 26px);
}
.container {
padding: 10px 10px;
gap: 10px;
}
.card {
padding: 10px;
}
.card p {
font-size: 12px;
}
.card strong {
font-size: clamp(20px, 5vw, 28px);
}
.chartBody {
height: 200px;
}
}
+48
View File
@@ -0,0 +1,48 @@
import { useEffect, useState } from "react";
import type { ReactNode } from "react";
import classes from "../assets/Dashboard.module.css";
export function Banner({ menuButton }: { menuButton?: ReactNode }){
const [time,setTime] = useState("");
useEffect(()=>{
const timer=setInterval(()=>{
setTime(
new Date()
.toLocaleString("zh-CN")
);
},1000);
return ()=>clearInterval(timer);
},[]);
return (
<header className={classes.banner}>
<div>
<div className={classes.title}>
智慧河流水质监测系统
</div>
<div className={classes.subtitle}>
Smart River Water Quality Monitoring System
</div>
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: '16px' }}>
<div className={classes.clockBox}>
<div>
当前时间
</div>
<div className={classes.clock}>
{time}
</div>
</div>
{menuButton && (
<div>
{menuButton}
</div>
)}
</div>
</header>
);
}
@@ -0,0 +1,151 @@
.navbar {
width: 260px;
height: 100vh;
padding: 16px;
background-color: var(--mantine-color-body);
border-right: 1px solid var(--mantine-color-gray-3);
display: flex;
flex-direction: column;
}
.header {
height: 70px;
margin-bottom: 30px;
}
.logo {
width: 42px;
height: 42px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 12px;
}
.menu {
display: flex;
flex-direction: column;
gap: 6px;
}
.link {
width: 100%;
height: 44px;
display: flex;
align-items: center;
gap: 12px;
padding: 0 14px;
border-radius: 10px;
border: none;
background: transparent;
color: var(--mantine-color-gray-7);
cursor: pointer;
font-size: 15px;
}
.link:hover {
background-color: var(--mantine-color-gray-1);
}
.link[data-active] {
background-color: var(--mantine-color-blue-light);
color: var(--mantine-color-blue-filled);
}
.linkIcon {
flex-shrink: 0;
}
.navbarMain {
display: flex;
flex-direction: column;
flex: 1;
}
/* Mobile responsive */
@media (max-width: 768px) {
.navbar {
width: 100%;
height: auto;
padding: 12px;
border-right: none;
border-bottom: 1px solid var(--mantine-color-gray-3);
}
.header {
height: auto;
margin-bottom: 12px;
}
.logo {
width: 36px;
height: 36px;
}
.menu {
display: flex;
flex-direction: row;
gap: 4px;
overflow-x: auto;
}
.link {
height: 40px;
padding: 0 10px;
font-size: 12px;
white-space: nowrap;
}
}
@media (max-width: 480px) {
.navbar {
padding: 10px;
}
.header {
display: none;
}
.menu {
gap: 2px;
}
.link {
height: 36px;
padding: 0 8px;
font-size: 11px;
}
.linkIcon {
width: 18px;
height: 18px;
}
}
@@ -0,0 +1,109 @@
import {
IconDashboard,
IconChartLine,
IconDatabase,
IconDevices,
IconSettings,
IconDroplet
} from '@tabler/icons-react';
import { Group, Text } from '@mantine/core';
import { useNavigate, useLocation } from 'react-router-dom';
import classes from './Navbar.module.css';
const data = [
{
label:'控制面板',
path:'/',
icon:IconDashboard,
},
{
label:'实时监测',
path:'/monitor',
icon:IconChartLine,
},
{
label:'历史数据',
path:'/history',
icon:IconDatabase,
},
{
label:'设备管理',
path:'/device',
icon:IconDevices,
},
{
label:'系统设置',
path:'/settings',
icon:IconSettings,
},
];
export function Navbar({
onClose
}:{
onClose?:()=>void;
}){
const navigate = useNavigate();
const location = useLocation();
const links = data.map((item)=>(
<button
className={classes.link}
data-active={
location.pathname === item.path || undefined
}
key={item.label}
onClick={()=>{
navigate(item.path);
onClose?.();
}}
>
<item.icon
className={classes.linkIcon}
stroke={1.8}
size={22}
/>
<span>
{item.label}
</span>
</button>
));
return (
<nav className={classes.navbar}>
<div className={classes.navbarMain}>
<Group className={classes.header}>
<div className={classes.logo}>
<IconDroplet size={32}/>
</div>
<div>
<Text fw={700}>
智慧河流水质监测系统
</Text>
<Text size="xs" c="dimmed">
Water Quality Monitor
</Text>
</div>
</Group>
<div className={classes.menu}>
{links}
</div>
</div>
</nav>
);
}
@@ -0,0 +1,44 @@
import type { WaterData } from "../types/water";
import classes from "../assets/Dashboard.module.css";
interface Props {
data: WaterData;
}
export function SummaryCards({ data }: Props) {
const score = data.score;
return (
<section className={classes.cards}>
<div className={classes.card}>
<p>综合评分</p>
<strong>{score.score}</strong>
</div>
<div className={classes.card}>
<p>综合等级</p>
<strong>{score.level}</strong>
</div>
<div className={classes.card}>
<p>系统状态</p>
<strong
className={
score.abnormal
? classes.abnormal
: classes.normal
}
>
{score.abnormal ? "异常" : "正常"}
</strong>
</div>
<div className={classes.card}>
<p>数据更新时间</p>
<strong>
{data.time.substring(11, 19)}
</strong>
</div>
</section>
);
}
+114
View File
@@ -0,0 +1,114 @@
import { useEffect, useRef } from "react";
import * as echarts from "echarts";
import classes from "../assets/Dashboard.module.css";
interface Props {
title: string;
data: number[];
time: string[];
min?: number;
max?: number;
}
export function WaterChart({
title,
data,
time,
min,
max
}: Props) {
const chartRef = useRef<HTMLDivElement>(null);
const chartInstance = useRef<echarts.ECharts | null>(null);
const getGridConfig = () => {
const width = window.innerWidth;
if (width < 480) {
return { left: 40, right: 10, top: 20, bottom: 25 };
}
if (width < 768) {
return { left: 45, right: 15, top: 25, bottom: 30 };
}
return { left: 50, right: 20, top: 30, bottom: 35 };
};
useEffect(() => {
if (!chartRef.current) return;
chartInstance.current = echarts.init(chartRef.current);
chartInstance.current.setOption({
animation: true,
tooltip: {
trigger: "axis"
},
grid: getGridConfig(),
xAxis: {
type: "category",
boundaryGap: false,
data: []
},
yAxis: {
type: "value",
min,
max,
splitNumber: 7,
splitLine: {
lineStyle: {
color: "#eee"
}
}
},
series: [
{
type: "line",
smooth: true,
symbol: "none",
lineStyle: {
width: 3
},
areaStyle: {
opacity: 0.25
},
data: []
}
]
});
const resize = () => {
chartInstance.current?.resize();
};
window.addEventListener("resize", resize);
return () => {
window.removeEventListener("resize", resize);
chartInstance.current?.dispose();
};
}, [min, max]);
useEffect(() => {
chartInstance.current?.setOption({
xAxis: {
data: time
},
series: [
{
data
}
]
});
}, [data, time]);
return (
<div className={classes.chart}>
<h3>{title}</h3>
<div
ref={chartRef}
className={classes.chartBody}
/>
</div>
);
}
+44
View File
@@ -0,0 +1,44 @@
import type { WaterScore } from "../types/water";
import classes from "../assets/Dashboard.module.css";
export function WaterItems({ data }: { data: WaterScore }) {
const list = [
{
name: "pH",
data: data.ph
},
{
name: "TDS(溶解性总固体)",
data: data.tds
},
{
name: "浊度",
data: data.turbidity
}
];
return (
<section className={classes.items}>
{list.map((item) => (
<div
className={classes.item}
key={item.name}
>
<h3>{item.name}</h3>
<div className={classes.value}>
{item.data.value.toFixed(2)}
</div>
<div>
等级:{item.data.level}
</div>
<div className={classes.score}>
评分:{item.data.score}
</div>
</div>
))}
</section>
);
}
+47
View File
@@ -0,0 +1,47 @@
import { useCallback, useEffect, useState } from "react";
import { getWaterData } from "../api/water";
import type { WaterData } from "../types/water";
export function useWaterData(pollingInterval = 3000) {
const [data, setData] = useState<WaterData | null>(null);
const [error, setError] = useState("");
const [isLoading, setIsLoading] = useState(true);
const load = useCallback(async () => {
try {
setError("");
setIsLoading(true);
const result = await getWaterData();
setData(result);
} catch (err) {
const message = err instanceof Error ? err.message : "Unknown error";
setError(`加载数据失败: ${message}`);
console.error(err);
} finally {
setIsLoading(false);
}
}, []);
useEffect(() => {
const timer = window.setInterval(() => {
void load();
}, pollingInterval);
const timeoutId = window.setTimeout(() => {
void load();
}, 0);
return () => {
window.clearInterval(timer);
window.clearTimeout(timeoutId);
};
}, [load, pollingInterval]);
return {
data,
error,
isLoading,
refetch: load,
};
}
+1
View File
@@ -0,0 +1 @@
@import "tailwindcss";
+22
View File
@@ -0,0 +1,22 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import { MantineProvider } from '@mantine/core'
import '@mantine/core/styles.css'
import App from './App.tsx'
import './index.css'
ReactDOM.createRoot(
document.getElementById('root')!
).render(
<React.StrictMode>
<MantineProvider>
<BrowserRouter>
<App />
</BrowserRouter>
</MantineProvider>
</React.StrictMode>
)
+116
View File
@@ -0,0 +1,116 @@
import { IconMenu2 } from "@tabler/icons-react";
import { ActionIcon } from "@mantine/core";
import { useMediaQuery } from "@mantine/hooks";
import { Banner } from "../components/Banner";
import { SummaryCards } from "../components/SummaryCards";
import { WaterItems } from "../components/WaterItems";
import { WaterChart } from "../components/WaterChart";
import { useWaterData } from "../hooks/useWaterData";
import classes from "../assets/Dashboard.module.css";
const chartConfigs = [
{
title: "pH变化趋势",
key: "ph" as const,
min: 0,
max: 14,
},
{
title: "TDS变化趋势",
key: "tds" as const,
min: 0,
max: 100,
},
{
title: "浊度变化趋势",
key: "turbidity" as const,
min: 0,
max: 1000,
},
];
export default function Dashboard({
onMenuClick,
}: {
onMenuClick?: () => void;
mobileNavOpen?: boolean;
}) {
const isMobile = useMediaQuery("(max-width: 768px)");
const { data, error, isLoading, refetch } = useWaterData();
if (!data) {
return (
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
height: "100vh",
flexDirection: "column",
gap: "16px",
}}
>
{error ? (
<>
<div style={{ color: "red", fontSize: "16px" }}>{error}</div>
<button
onClick={() => void refetch()}
style={{
padding: "8px 16px",
backgroundColor: "#1976d2",
color: "white",
border: "none",
borderRadius: "4px",
cursor: "pointer",
}}
>
重试
</button>
</>
) : (
<div>{isLoading ? "加载中..." : "暂无数据"}</div>
)}
</div>
);
}
const history = data.data.slice(-100);
const time = history.map((item) => item.time.substring(11, 19));
return (
<div className={classes.page}>
<Banner
menuButton={
isMobile ? (
<ActionIcon
variant="light"
onClick={onMenuClick}
aria-label="Toggle navigation"
>
<IconMenu2 size={20} />
</ActionIcon>
) : null
}
/>
<main className={classes.container}>
<SummaryCards data={data} />
<WaterItems data={data.score} />
<section className={classes.chartGrid}>
{chartConfigs.map((config) => (
<WaterChart
key={config.title}
title={config.title}
time={time}
data={history.map((item) => item[config.key])}
min={config.min}
max={config.max}
/>
))}
</section>
</main>
</div>
);
}
+31
View File
@@ -0,0 +1,31 @@
export interface WaterItem {
value: number;
level: string;
score: number;
}
export interface WaterScore {
score: number;
level: string;
abnormal: boolean;
ph: WaterItem;
tds: WaterItem;
turbidity: WaterItem;
}
export interface WaterData {
code: number;
time: string;
score: WaterScore;
data: Array<{
time: string;
ph: number;
tds: number;
turbidity: number;
}>;
}