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/abeamProChart.vue | 53 +++++++++++++++++++++++++---------------------------- 1 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/components/charts/abeamProChart.vue b/src/components/charts/abeamProChart.vue index e1a1fc8..55696a1 100644 --- a/src/components/charts/abeamProChart.vue +++ b/src/components/charts/abeamProChart.vue @@ -16,9 +16,7 @@ import { chartFontsize } from '@/assets/js/chartFontsize' -import { - batteryStatus -} from '@/assets/js/api' +import { WebSocketClass } from '@/assets/js/socket' export default { name: "abeamProChart", chart: "", @@ -32,7 +30,7 @@ }, data() { return { - + websock: null } }, methods: { @@ -187,35 +185,28 @@ this.organizeData(sendData) } else { this.postData() - setInterval(() => { - this.postData() - }, 3000) } }, postData() { let userId = localStorage.getItem('userId'); - let params = { - userId: userId - } - batteryStatus(params).then((res) => { - if (res.data.code == 1) { - let optionData = { - yData: [], - color: ['#f58881', '#b4d465', '#ffcb29'], - bgColor: ['rgba(245,136,129,0.35)', 'rgba(255,255,255,0.35)', 'rgba(255,203,41,0.35)'], - data: [] - } - let resData = res.data.data; - for (let key in resData) { - optionData.yData.push(key); - optionData.data.push(resData[key]); - } - this.$options.chartData = optionData; - this.organizeData(optionData) + this.websock = new WebSocketClass(`/screen/battery/status/${userId}`, this.wsMessage, 4000) + }, + wsMessage(res) { + if (res.code == 1) { + let optionData = { + yData: [], + color: ['#f58881', '#b4d465', '#ffcb29'], + bgColor: ['rgba(245,136,129,0.35)', 'rgba(255,255,255,0.35)', 'rgba(255,203,41,0.35)'], + data: [] } - }).catch((err) => { - console.log(err) - }); + let resData = res.data; + for (let key in resData) { + optionData.yData.push(key); + optionData.data.push(resData[key]); + } + this.$options.chartData = optionData; + this.organizeData(optionData) + } }, resize() { setTimeout(() => { @@ -224,6 +215,11 @@ this.setData(this.$options.chartData); } }, 300) + }, + outClear() { + this.websock.closeMyself() + this.websock = null + window.removeEventListener('resize', this.resize); } }, mounted() { @@ -233,6 +229,7 @@ window.addEventListener('resize', this.resize); }, destroyed() { + this.websock.closeMyself() window.removeEventListener('resize', this.resize); } } -- Gitblit v1.9.1