From 9fc3a1d5a70a5af805b37476d47331dad0b21d65 Mon Sep 17 00:00:00 2001
From: whyczyk <525500596@qq.com>
Date: 星期六, 09 十月 2021 15:09:32 +0800
Subject: [PATCH] 模块socket优化请求数据

---
 src/components/charts/triangleBarChart.vue |   49 +++++++++++++++++++++++--------------------------
 1 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/src/components/charts/triangleBarChart.vue b/src/components/charts/triangleBarChart.vue
index 97c5aaa..c36efc6 100644
--- a/src/components/charts/triangleBarChart.vue
+++ b/src/components/charts/triangleBarChart.vue
@@ -12,9 +12,7 @@
 import {
 	chartFontsize
 } from '@/assets/js/chartFontsize'
-import {
-	batteryGroup
-} from '@/assets/js/api'
+import { WebSocketClass } from '@/assets/js/socket'
 export default {
 	name: "triangleBarChart",
 	chart: "",
@@ -28,7 +26,7 @@
 	},
 	data() {
 		return {
-
+			websock: null
 		}
 	},
 	methods: {
@@ -186,33 +184,26 @@
 				this.organizeData(sendData)
 			} else {
 				this.postData()
-				setInterval(() => {
-					this.postData()
-				}, 3000)
 			}
 		},
 		postData() {
 			let userId = localStorage.getItem('userId');
-			let params = {
-				userId: userId
-			}
-			batteryGroup(params).then((res) => {
-				if (res.data.code == 1) {
-					let optionData = {
-						xData: [],
-						data: []
-					}
-					let resData = res.data.data;
-					for (let key in resData) {
-						optionData.xData.push(key);
-						optionData.data.push(resData[key]);
-					}
-					this.$options.chartData = optionData;
-					this.organizeData(optionData)
+			this.websock = new WebSocketClass(`/screen/powerAlarm/batteryGroup/${userId}`, this.wsMessage, 4000)
+		},
+		wsMessage(res) {
+			if (res.code == 1) {
+				let optionData = {
+					xData: [],
+					data: []
 				}
-			}).catch((err) => {
-				console.log(err)
-			});
+				let resData = res.data;
+				for (let key in resData) {
+					optionData.xData.push(key);
+					optionData.data.push(resData[key]);
+				}
+				this.$options.chartData = optionData;
+				this.organizeData(optionData)
+			}
 		},
 		resize() {
 			setTimeout(() => {
@@ -221,6 +212,11 @@
 					this.setData(this.$options.chartData);
 				}
 			}, 300)
+		},
+		outClear() {
+			this.websock.closeMyself()
+			this.websock = null
+			window.removeEventListener('resize', this.resize);
 		}
 	},
 	mounted() {
@@ -230,6 +226,7 @@
 		window.addEventListener('resize', this.resize);
 	},
 	destroyed() {
+		this.websock.closeMyself()
 		window.removeEventListener('resize', this.resize);
 	}
 }

--
Gitblit v1.9.1