From 3a734e6dad56a1612550e5366995cfcad29080f9 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 8 May 2026 15:34:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E9=A6=96=E9=A1=B5=E7=9A=84=E4=B8=A4=E4=B8=AA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复用户分布显示问题: - 后端添加 online 和 offline 字段到用户分布数据 - 确保前端能正确显示在线和离线用户数 2. 修复 ECharts 初始化报错: - 添加 DOM 元素存在性检查 - 添加异步加载时的卸载检查 - 在重新初始化前清理旧实例 - 防止页面切换时的空指针错误 --- backend/internal/router/admin/dashboard.go | 6 ++++-- frontend/src/pages/admin/index.vue | 24 ++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/backend/internal/router/admin/dashboard.go b/backend/internal/router/admin/dashboard.go index 057a151..8b06b34 100644 --- a/backend/internal/router/admin/dashboard.go +++ b/backend/internal/router/admin/dashboard.go @@ -130,8 +130,10 @@ func handleGetDashboard(c *gin.Context) { provinces := make([]gin.H, 0, len(deviceCounts)) for _, dc := range deviceCounts { provinces = append(provinces, gin.H{ - "name": dc.DeviceType, - "count": dc.Count, + "name": dc.DeviceType, + "count": dc.Count, + "online": 0, + "offline": dc.Count, }) } mu.Lock() diff --git a/frontend/src/pages/admin/index.vue b/frontend/src/pages/admin/index.vue index 752cf99..08b01ee 100644 --- a/frontend/src/pages/admin/index.vue +++ b/frontend/src/pages/admin/index.vue @@ -232,8 +232,10 @@ function fetchCurrentUser() { } async function initMapChart() { - if (!mapChart.value) + if (!mapChart.value) { + console.warn('Map chart container not found') return + } try { const [echarts, worldResponse] = await Promise.all([ @@ -241,6 +243,11 @@ async function initMapChart() { fetch('/world.json'), ]) + if (!mapChart.value) { + console.warn('Map chart container was unmounted during initialization') + return + } + if (!worldResponse.ok) { throw new Error('Failed to load world map data') } @@ -249,6 +256,11 @@ async function initMapChart() { echarts.registerMap('world', worldData) + if (chartInstance) { + chartInstance.dispose() + chartInstance = null + } + chartInstance = echarts.init(mapChart.value) updateMapOption() @@ -609,11 +621,18 @@ function updateMapOption() { } function initActivityChart() { - if (!activityChart.value) + if (!activityChart.value) { + console.warn('Activity chart container not found') return + } try { import('chart.js/auto').then(({ default: Chart }) => { + if (!activityChart.value) { + console.warn('Activity chart container was unmounted during initialization') + return + } + const ctx = activityChart.value?.getContext('2d') if (!ctx) return @@ -630,6 +649,7 @@ function initActivityChart() { if (activityChartInstance) { activityChartInstance.destroy() + activityChartInstance = null } activityChartInstance = new Chart(ctx, {