gaoluyang
2026-04-10 c356bdeb6211ef70355b9c9ee49a0baf7c80e635
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<template>
  <div class="environment-monitoring-page">
    <section class="chart-panel">
      <h3 class="panel-title">环境实时柱状图</h3>
      <Echarts
        :series="barSeries"
        :x-axis="xAxis"
        :y-axis="yAxis"
        :tooltip="tooltip"
        :grid="grid"
        :legend="legend"
        :options="chartTheme"
        :chart-style="chartStyle"
      />
    </section>
 
    <section class="table-panel">
      <h3 class="panel-title">设备环境数据列表</h3>
      <div class="sensor-table">
        <div class="sensor-table__head">
          <span>设备编号</span>
          <span>设备名称</span>
          <span>温度</span>
          <span>湿度</span>
          <span>二氧化碳</span>
          <span>光照</span>
        </div>
        <div v-for="item in deviceRows" :key="item.guid" class="sensor-table__row">
          <span>{{ item.guid }}</span>
          <span>{{ item.name }}</span>
          <span>{{ item.temperature }}</span>
          <span>{{ item.humidity }}</span>
          <span>{{ item.co2 }}</span>
          <span>{{ item.light }}</span>
        </div>
        <div v-if="!deviceRows.length" class="sensor-table__empty">暂无环境数据</div>
      </div>
    </section>
  </div>
</template>
 
<script setup>
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
import Echarts from "@/components/Echarts/echarts.vue";
import { getEnvironmentalRealData } from "@/api/inventoryManagement/environmentalMonitoring";
 
const POLL_INTERVAL = import.meta.env.DEV ? 73000 : 30000;
 
const latestDevices = ref([]);
let pollTimer = null;
 
const metricConfig = [
  { key: "temperature", label: "温度", color: "#ff7a59" },
  { key: "humidity", label: "湿度", color: "#1ea7fd" },
  { key: "co2", label: "二氧化碳", color: "#12c48b" },
  { key: "light", label: "光照", color: "#8b5cf6" },
];
 
const chartTheme = {
  backgroundColor: "transparent",
  textStyle: { color: "#6c7c96" },
};
 
const chartStyle = {
  width: "100%",
  height: "360px",
};
 
const grid = {
  left: "4%",
  right: "4%",
  top: "16%",
  bottom: "10%",
  containLabel: true,
};
 
const tooltip = {
  trigger: "axis",
  axisPointer: { type: "shadow" },
  backgroundColor: "rgba(12, 20, 34, 0.88)",
  borderColor: "rgba(126, 164, 255, 0.18)",
  textStyle: { color: "#e8edf7" },
};
 
const legend = {
  top: 0,
  right: 0,
  textStyle: { color: "#6c7c96" },
};
 
const extractNumericValue = (rawValue) => {
  const matched = String(rawValue ?? "").match(/-?\d+(\.\d+)?/);
  return matched ? Number(matched[0]) : 0;
};
 
const normalizeMetricObject = (source, index) => {
  const normalized = {
    guid: source.guid || source.deviceGuid || source.deviceNo || `GUID-${index + 1}`,
    name: source.deviceName || source.name || `设备${index + 1}`,
    temperature: 0,
    humidity: 0,
    co2: 0,
    light: 0,
  };
 
  Object.entries(source || {}).forEach(([key, value]) => {
    const rawText = String(value ?? "");
 
    if (rawText.includes("℃")) {
      normalized.temperature = extractNumericValue(rawText);
      return;
    }
    if (rawText.includes("%RH")) {
      normalized.humidity = extractNumericValue(rawText);
      return;
    }
    if (rawText.includes("ppm")) {
      normalized.co2 = extractNumericValue(rawText);
      return;
    }
    if (rawText.includes("Lux")) {
      normalized.light = extractNumericValue(rawText);
      return;
    }
 
    if (key === "temperature") {
      normalized.temperature = extractNumericValue(rawText);
    } else if (key === "humidity") {
      normalized.humidity = extractNumericValue(rawText);
    } else if (key === "co2") {
      normalized.co2 = extractNumericValue(rawText);
    } else if (key === "light") {
      normalized.light = extractNumericValue(rawText);
    }
  });
 
  return normalized;
};
 
const xAxis = computed(() => [
  {
    type: "category",
    data: latestDevices.value.map((item) => item.name),
    axisLine: { lineStyle: { color: "rgba(79, 110, 148, 0.55)" } },
    axisLabel: { color: "#6c7c96", rotate: 0 },
    axisTick: { show: false },
  },
]);
 
const yAxis = [
  {
    type: "value",
    axisLine: { show: false },
    axisTick: { show: false },
    splitLine: { lineStyle: { color: "rgba(110, 131, 160, 0.12)" } },
    axisLabel: { color: "#6c7c96" },
  },
];
 
const barSeries = computed(() =>
  metricConfig.map((item) => ({
    name: item.label,
    type: "bar",
    barMaxWidth: 28,
    itemStyle: {
      color: item.color,
      borderRadius: [8, 8, 0, 0],
    },
    data: latestDevices.value.map((device) => Number(device[item.key] || 0)),
  }))
);
 
const deviceRows = computed(() =>
  latestDevices.value.map((item) => ({
    guid: item.guid,
    name: item.name,
    temperature: `${Number(item.temperature || 0).toFixed(2)}℃`,
    humidity: `${Number(item.humidity || 0).toFixed(2)}%RH`,
    co2: `${Number(item.co2 || 0).toFixed(2)}ppm`,
    light: `${Number(item.light || 0).toFixed(2)}Lux`,
  }))
);
 
const fetchRealData = async () => {
  try {
    const res = await getEnvironmentalRealData();
    const dataList = Array.isArray(res?.data) ? res.data : [];
    latestDevices.value = dataList.map((item, index) => normalizeMetricObject(item, index));
  } catch (error) {
    latestDevices.value = [];
  }
};
 
onMounted(() => {
  fetchRealData();
  pollTimer = window.setInterval(fetchRealData, POLL_INTERVAL);
});
 
onBeforeUnmount(() => {
  if (pollTimer) {
    window.clearInterval(pollTimer);
    pollTimer = null;
  }
});
</script>
 
<style scoped lang="scss">
.environment-monitoring-page {
  min-height: 100%;
  padding: 20px;
}
 
.chart-panel,
.table-panel {
  padding: 20px;
  border-radius: 16px;
  background: #fff;
}
 
.table-panel {
  margin-top: 20px;
}
 
.panel-title {
  margin: 0 0 16px;
  color: #1d344f;
  font-size: 18px;
  font-weight: 600;
}
 
.sensor-table {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
 
.sensor-table__head,
.sensor-table__row {
  display: grid;
  grid-template-columns: 1.2fr 1fr 1fr 1fr 1fr 1fr;
  gap: 12px;
  align-items: center;
}
 
.sensor-table__head {
  padding: 0 6px 10px;
  color: #8393a8;
  font-size: 13px;
}
 
.sensor-table__row {
  padding: 14px 16px;
  border-radius: 12px;
  background: #f6f9fc;
  color: #1d344f;
}
 
.sensor-table__empty {
  padding: 32px 0;
  color: #8393a8;
  text-align: center;
}
 
@media (max-width: 768px) {
  .environment-monitoring-page {
    padding: 12px;
  }
 
  .chart-panel,
  .table-panel {
    padding: 16px;
  }
 
  .sensor-table__head,
  .sensor-table__row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
</style>