`text\ndashboard/\n├── public/\n│ └── index.html\n├── src/\n│ ├── assets/ # 图标、背景图、字体\n│ ├── components/ # 通用组件\n│ │ ├── ChartCard.vue # 图表卡片容器\n│ │ ├── RealTimeTable.vue # 实时数据表格\n│ │ └── Map3D.vue # 3D地图/产线\n│ ├── views/\n│ │ ├── production/ # 生产监控大屏\n│ │ ├── energy/ # 能耗大屏\n│ │ └── security/ # 安防大屏\n│ ├── api/\n│ │ ├── http.ts # Axios实例与拦截器\n│ │ └── ws.ts # WebSocket封装(自动重连、心跳)\n│ ├── store/\n│ │ └── device.ts # Pinia设备状态\n│ ├── utils/\n│ │ ├── screen.ts # 大屏自适应缩放\n│ │ └── format.ts # 数据格式化\n│ └── main.ts\n`\n\n### 2.3 核心源码示例\n\n#### (1) WebSocket 封装(支持断线重连、心跳)\n\n`typescript\n// src/api/ws.ts\nexport class WsClient {\n private ws: WebSocket | null = null;\n private url: string;\n private reconnectTimer: number | null = null;\n private heartbeatTimer: number | null = null;\n\n constructor(url: string) {\n this.url = url;\n this.connect();\n }\n\n private connect() {\n this.ws = new WebSocket(this.url);\n\n this.ws.onopen = () => {\n console.log('WS connected');\n this.startHeartbeat();\n };\n\n this.ws.onmessage = (ev) => {\n const data = JSON.parse(ev.data);\n // 触发全局事件或Pinia更新\n window.dispatchEvent(new CustomEvent('ws-message', { detail: data }));\n };\n\n this.ws.onclose = () => {\n console.warn('WS closed, retrying...');\n this.stopHeartbeat();\n this.reconnect();\n };\n }\n\n private reconnect() {\n if (this.reconnectTimer) return;\n this.reconnectTimer = window.setTimeout(() => {\n this.reconnectTimer = null;\n this.connect();\n }, 3000);\n }\n\n private startHeartbeat() {\n this.heartbeatTimer = window.setInterval(() => {\n if (this.ws?.readyState === WebSocket.OPEN) {\n this.ws.send(JSON.stringify({ type: 'ping' }));\n }\n }, 15000);\n }\n\n private stopHeartbeat() {\n if (this.heartbeatTimer) {\n clearInterval(this.heartbeatTimer);\n this.heartbeatTimer = null;\n }\n }\n\n send(data: any) {\n if (this.ws?.readyState === WebSocket.OPEN) {\n this.ws.send(JSON.stringify(data));\n }\n }\n}\n`\n\n#### (2) 大屏自适应缩放(基于vw/vh比例)\n\n`typescript\n// src/utils/screen.ts\nexport function useScreenFit(designWidth = 1920, designHeight = 1080) {\n const scale = () => {\n const w = window.innerWidth;\n const h = window.innerHeight;\n const scaleX = w / designWidth;\n const scaleY = h / designHeight;\n const s = Math.min(scaleX, scaleY);\n document.documentElement.style.fontSize = ${16 * s}px;\n // 或者直接对根容器做 transform: scale()\n const app = document.getElementById('dashboard-app');\n if (app) {\n app.style.width = ${designWidth}px;\n app.style.height = ${designHeight}px;\n app.style.transform = scale(${scaleX}, ${scaleY});\n app.style.transformOrigin = 'left top';\n }\n };\n window.addEventListener('resize', scale);\n scale();\n}\n`\n\n#### (3) ECharts 实时曲线组件(实时更新)\n\n`vue\n\n\n 更新时间:2026-10-10 05:26:53
如若转载,请注明出处:http://www.byrok.com/product/48.html
PRODUCT
产品列表