From b77a08b70d8fc45ce700d8fc86e53503e9a8ec05 Mon Sep 17 00:00:00 2001
From: whyczyk <525500596@qq.com>
Date: 星期五, 08 十月 2021 11:05:32 +0800
Subject: [PATCH] 跳转及socket封装提交

---
 src/assets/js/socket.js                |   67 +++++++++++++++++++++++++++++++++
 src/components/charts/RoseChartHea.vue |    6 +-
 src/components/charts/chinaMap.vue     |    5 ++
 src/main.js                            |    3 +
 src/assets/js/getWsUrl.js              |   14 +++++++
 src/components/charts/RoseChartED.vue  |    6 +-
 6 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/src/assets/js/getWsUrl.js b/src/assets/js/getWsUrl.js
new file mode 100644
index 0000000..9359888
--- /dev/null
+++ b/src/assets/js/getWsUrl.js
@@ -0,0 +1,14 @@
+/**
+ * 鑾峰彇Websocket鐨勮繛鎺�
+ * @param action
+ * @returns {string}
+ */
+function getWsUrl(action, port) {
+    let _port = port?port:8091;
+    let hostname = window.location.hostname;
+    if(process.env.NODE_ENV == 'dev') {
+        hostname = "localhost";
+    }
+    return 'ws://' + hostname+':' + _port + action;
+}
+export default getWsUrl;
\ No newline at end of file
diff --git a/src/assets/js/socket.js b/src/assets/js/socket.js
new file mode 100644
index 0000000..75be6ec
--- /dev/null
+++ b/src/assets/js/socket.js
@@ -0,0 +1,67 @@
+import getWsUrl from "@/assets/js/getWsUrl";
+var websock = null;
+var global_callback = null;
+
+function initWebSocket(action, port) { //鍒濆鍖杦eosocket
+    //ws鍦板潃
+    const wsUri = getWsUrl(action, port);
+    websock = new WebSocket(wsUri);
+    websock.onmessage = function (e) {
+        websocketonmessage(e);
+    }
+    websock.onclose = function (e) {
+        websocketclose(e);
+    }
+    websock.onopen = function () {
+        websocketOpen();
+    }
+
+    //杩炴帴鍙戠敓閿欒鐨勫洖璋冩柟娉�
+    websock.onerror = function () {
+        console.log("WebSocket杩炴帴鍙戠敓閿欒");
+    }
+}
+
+// 瀹為檯璋冪敤鐨勬柟娉�
+function sendSock(agentData, callback) {
+    global_callback = callback;
+    if (websock.readyState === websock.OPEN) {
+        //鑻ユ槸ws寮�鍚姸鎬�
+        websocketsend(agentData)
+    } else if (websock.readyState === websock.CONNECTING) {
+        // 鑻ユ槸 姝e湪寮�鍚姸鎬侊紝鍒欑瓑寰�1s鍚庨噸鏂拌皟鐢�
+        setTimeout(function () {
+            sendSock(agentData, callback);
+        }, 1000);
+    } else {
+        // 鑻ユ湭寮�鍚� 锛屽垯绛夊緟1s鍚庨噸鏂拌皟鐢�
+        setTimeout(function () {
+            sendSock(agentData, callback);
+        }, 1000);
+    }
+}
+
+//鏁版嵁鎺ユ敹
+function websocketonmessage(e) {
+    global_callback(JSON.parse(e.data));
+}
+
+//鏁版嵁鍙戦��
+function websocketsend(agentData) {
+    websock.send(JSON.stringify(agentData));
+}
+
+//鍏抽棴
+function websocketclose() {
+    console.log("WebSocket宸插叧闂�");
+}
+//WebSocket杩炴帴鎴愬姛
+function websocketOpen() {
+    console.log("WebSocket杩炴帴鎴愬姛");
+}
+
+function close() {
+    websock.close()
+}
+
+export { sendSock, initWebSocket, close }
\ No newline at end of file
diff --git a/src/components/charts/RoseChartED.vue b/src/components/charts/RoseChartED.vue
index 3e5fe2a..cb18bc2 100644
--- a/src/components/charts/RoseChartED.vue
+++ b/src/components/charts/RoseChartED.vue
@@ -28,9 +28,9 @@
 				cmd: "syncPage",
 				params: {
 					pageInfo: {
-						label: "鐢垫睜瀹炴椂鍛婅",
-						name: "batteryrTimequery",
-						src: "#/batteryrTimequery",
+						label: "鐢垫睜缁埅鑳藉姏鍘嗗彶鏌ヨ",
+						name: "endure",
+						src: "#/reportStatistics/endure",
 						closable: true
 					},
 				}
diff --git a/src/components/charts/RoseChartHea.vue b/src/components/charts/RoseChartHea.vue
index 3f8072c..7d82813 100644
--- a/src/components/charts/RoseChartHea.vue
+++ b/src/components/charts/RoseChartHea.vue
@@ -28,9 +28,9 @@
 				cmd: "syncPage",
 				params: {
 					pageInfo: {
-						label: "鐢垫睜瀹炴椂鍛婅",
-						name: "batteryrTimequery",
-						src: "#/batteryrTimequery",
+						label: "钀藉悗鍗曚綋鏌ヨ",
+						name: "taskplan",
+						src: "#/reportStatistics/taskplan",
 						closable: true
 					},
 				}
diff --git a/src/components/charts/chinaMap.vue b/src/components/charts/chinaMap.vue
index 0f32544..392850d 100644
--- a/src/components/charts/chinaMap.vue
+++ b/src/components/charts/chinaMap.vue
@@ -528,6 +528,11 @@
 	destroyed() {
 		window.removeEventListener('resize', this.resize);
 		clearInterval(this.timer)
+		// this.$socket.initWebSocket("/device", "")
+		// this.$socket.sendSock(null, (res) => {
+		//     console.log(res)
+		// });
+		// this.$socket.close();
 	},
 }
 </script>
diff --git a/src/main.js b/src/main.js
index f07d449..b16e8dd 100644
--- a/src/main.js
+++ b/src/main.js
@@ -15,6 +15,9 @@
 import 'vue-layer/lib/vue-layer.css';
 Vue.prototype.$layer = layer(Vue);
 
+import * as socket from '@/assets/js/socket'
+Vue.prototype.$socket = socket
+
 Vue.prototype.$axios = axios;
 
 Vue.use(ElementUI, {

--
Gitblit v1.9.1