From 761c9e7f73ae18ce53d925f99bab7766c5c5876e Mon Sep 17 00:00:00 2001
From: 罗广辉 <guanghui.luo@foxmail.com>
Date: Thu, 25 Sep 2025 10:53:13 +0800
Subject: [PATCH] feat: 全局ws

---
 src/views/dataCenter/components/searchData.vue                               |   14 +++---
 src/views/job/components/TaskIntermediateContent/TaskIntermediateContent.vue |   18 ++++----
 src/store/modules/common.js                                                  |   10 ++++
 src/page/index/index.vue                                                     |    4 ++
 src/page/index/GlobalWS.vue                                                  |   11 +++++
 src/App.vue                                                                  |    2 -
 src/page/index/useGlobalWS.js                                                |   45 +++++++++++++++++-----
 7 files changed, 75 insertions(+), 29 deletions(-)

diff --git a/src/App.vue b/src/App.vue
index 066092b..384f5ff 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -4,8 +4,6 @@
 
 
 <script setup>
-import { useDownloadWs } from '@/page/index/useDownloadWs';
-useDownloadWs()
 </script>
 
 <style>
diff --git a/src/page/index/GlobalWS.vue b/src/page/index/GlobalWS.vue
new file mode 100644
index 0000000..261b648
--- /dev/null
+++ b/src/page/index/GlobalWS.vue
@@ -0,0 +1,11 @@
+<template>
+
+</template>
+
+<script setup>
+import { useGlobalWS } from '@/page/index/useGlobalWS';
+useGlobalWS()
+</script>
+<style scoped lang="scss">
+
+</style>
diff --git a/src/page/index/index.vue b/src/page/index/index.vue
index c761232..bf10c2e 100644
--- a/src/page/index/index.vue
+++ b/src/page/index/index.vue
@@ -24,6 +24,8 @@
         </div>
       </div>
     </div>
+
+    <GlobalWS/>
     <!-- <wechat></wechat> -->
   </div>
 </template>
@@ -37,10 +39,12 @@
 import logo from './logo.vue'
 import top from './top/index.vue'
 import sidebar from './sidebar/index.vue'
+import GlobalWS from '@/page/index/GlobalWS.vue';
 
 export default {
   mixins: [index],
   components: {
+    GlobalWS,
     top,
     logo,
     tags,
diff --git a/src/page/index/useDownloadWs.js b/src/page/index/useGlobalWS.js
similarity index 62%
rename from src/page/index/useDownloadWs.js
rename to src/page/index/useGlobalWS.js
index 2113b6d..22faf74 100644
--- a/src/page/index/useDownloadWs.js
+++ b/src/page/index/useGlobalWS.js
@@ -1,20 +1,21 @@
+import { computed, onBeforeUnmount, onMounted } from 'vue';
 import { getWebsocketUrl } from '@/utils/websocket/config'
 import { useConnectWebSocket } from '@/utils/websocket/connect-websocket'
 import { useStore } from 'vuex'
 import dayjs from 'dayjs'
 import { aLinkDownloadUtil } from '@/utils/util'
 import EventBus from '@/utils/eventBus'
-import { computed, onBeforeUnmount, onMounted } from 'vue';
-import { setStore,getStore } from '@/utils/store';
-import { getDownloadStatusApi } from '@/api/dataCenter/dataCenter';
+import { getStore, setStore } from '@/utils/store'
 
-export const useDownloadWs = () => {
+export const useGlobalWS = () => {
 	const store = useStore()
 	const loginUserInfo = computed(() => store.state.user.userInfo)
 	const downloadProgress = computed(() => store.state.common.downloadProgress)
 	let currentWebSocket = null
-	function messageHandler(payload) {
-		const {type,status,progress,download_url} = payload
+
+	// 下载进度处理
+	function downloadProcessing(payload) {
+		const {type,status,progress,download_url} = payload?.data || {}
 		let setProgress = 0
 		if (status === 'PENDING'){
 			setProgress = 1
@@ -29,7 +30,7 @@
 			...downloadProgress.value,
 			[type]: setProgress,
 		})
-		if (status === 'COMPLETED' && ['htsjzx','htlsrwxq'].includes(type)) {
+		if (status === 'COMPLETED' && ['dpsjzx','dplsrwxq'].includes(type)) {
 			const name = `数据中心-${dayjs().format('YYYYMMDDHHmmss')}.zip`
 			const currentUrl = getStore({name: 'downloadUrl'})
 			if (currentUrl === download_url && download_url!== undefined) return
@@ -38,18 +39,42 @@
 		}
 	}
 
+	const logOut = () => {
+		store.commit('SET_THEME_NAME', '')
+		store.dispatch('LogOut').then(() => {
+			router.push({ path: '/login' })
+			setTimeout(() => location.reload())
+		})
+	}
+	function messageHandler(payload) {
+		switch (payload.biz_code) {
+			case 'JOB_ISREFRESH':
+				store.commit('jobUpdateKeyAdd')
+				break
+			case 'DEVICE_ISREFRESH':
+				store.commit('deviceUpdateKeyAdd')
+				break
+			case 'DOWNLOAD_PROGRESS':
+				downloadProcessing(payload)
+				break
+			case 'LOGOUT_USER':
+				logOut()
+				break
+		}
+	}
+
 	onMounted(() => {
 		let webSocketUrl = getWebsocketUrl() + `&model_type=3&workspace-id=${loginUserInfo.value.user_id}`
 		currentWebSocket = useConnectWebSocket((result)=>{
 			const payload = JSON.parse(result)
-			messageHandler(payload.data)
+			messageHandler(payload)
 		}, webSocketUrl)
-		EventBus.on('useDownloadWs-messageHandler',messageHandler)
+		EventBus.on('useGlobalWS-messageHandler',messageHandler)
 	})
 
 	onBeforeUnmount(() => {
 		currentWebSocket?.close()
-		EventBus.off('useDownloadWs-messageHandler',messageHandler)
+		EventBus.off('useGlobalWS-messageHandler',messageHandler)
 	})
 	return {}
 }
diff --git a/src/store/modules/common.js b/src/store/modules/common.js
index 46f4fb2..83eccd6 100644
--- a/src/store/modules/common.js
+++ b/src/store/modules/common.js
@@ -18,9 +18,17 @@
     downloadProgress:{
       htsjzx: 100, //数据中心
       htlsrwxq: 100, //历史任务详情
-    }
+    },
+    deviceUpdateKey: 0, //设备刷新key
+    jobUpdateKey: 0, //任务刷新key
   },
   mutations: {
+    deviceUpdateKeyAdd(state, data) {
+      state.deviceUpdateKey = state.deviceUpdateKey + 1
+    },
+    jobUpdateKeyAdd(state, data) {
+      state.jobUpdateKey = state.jobUpdateKey + 1
+    },
     setDownloadProgress(state, data) {
       state.downloadProgress = data
     },
diff --git a/src/views/dataCenter/components/searchData.vue b/src/views/dataCenter/components/searchData.vue
index 1445f58..bae886e 100644
--- a/src/views/dataCenter/components/searchData.vue
+++ b/src/views/dataCenter/components/searchData.vue
@@ -3,7 +3,7 @@
     <el-form :model="searchForm" inline>
       <div class="search-first">
         <el-form-item label="行政区划:" >
-          <el-tree-select   
+          <el-tree-select
           :disabled="viewDetailsDisabled"
             popper-class="custom-tree-select"
             v-model="searchForm.areaCode"
@@ -80,7 +80,7 @@
         </el-form-item>
         <el-form-item label="文件类别:">
           <el-select
-          
+
             :disabled="disabled || foldersDisabled || viewDetailsDisabled"
             :teleported="false"
             v-model="searchForm.photoType"
@@ -283,7 +283,7 @@
   machineData.value = '';
   const droneList = await getDeviceRegion({ areaCode: data.id });
   machineData.value = droneList?.data?.data;
-     
+
   // 默认选中值
   if (signDevice_sn.value) {
     searchForm.deviceSn = signDevice_sn.value;
@@ -292,7 +292,7 @@
   deptData.value = [];
   getDeptsByAreaCode();
   handleSearch();
-  
+
 };
 // 所属部门信息
 const getDeptsByAreaCode = () => {
@@ -351,17 +351,17 @@
 }
 // 切换文件夹
 const handleswitchFolders =()=>{
-  emit('handleswitchFolders'); 
+  emit('handleswitchFolders');
 }
 // 返回按钮
 const handleBack = ()=>{
-  emit('handleBack'); 
+  emit('handleBack');
 }
 onMounted(() => {
   requestDockInfo();
   getDownloadStatusApi({type: 'htsjzx'}).then(res =>{
     if (!['CANCELLED','COMPLETED'].includes(res.data.data?.status || 'COMPLETED')){
-      EventBus.emit('useDownloadWs-messageHandler',res.data.data)
+      EventBus.emit('useGlobalWS-messageHandler',res.data.data)
     }
   })
 });
diff --git a/src/views/job/components/TaskIntermediateContent/TaskIntermediateContent.vue b/src/views/job/components/TaskIntermediateContent/TaskIntermediateContent.vue
index 8749468..82b8a58 100644
--- a/src/views/job/components/TaskIntermediateContent/TaskIntermediateContent.vue
+++ b/src/views/job/components/TaskIntermediateContent/TaskIntermediateContent.vue
@@ -119,7 +119,7 @@
 import CancelTaskDialog from '../CancelTaskDialog.vue'
 import { useStore } from 'vuex'
 import { cloneDeep } from 'lodash'
-import { inject, onBeforeUnmount } from 'vue';
+import { inject, onBeforeUnmount, watch } from 'vue';
 import ElTooltipCopy from '@/components/ElTooltipCopy.vue';
 const store = useStore()
 const singleUavHome = computed(() => store.state.home.singleUavHome)
@@ -264,14 +264,14 @@
 
 
 const changeKey = inject('changeKey')
-// 轮询查询是否刷新
-const polling = setInterval(async () => {
-  const res = await statusChangedApi()
-  if (res.data.data.DEVICE_REFRESH || res.data.data.JOB_REFRESH) {
-    getJobList()
-    changeKey.value++
-  }
-}, 4000)
+const jobUpdateKey = computed(() => store.state.common.jobUpdateKey)
+const deviceUpdateKey = computed(() => store.state.common.deviceUpdateKey)
+
+watch([jobUpdateKey,deviceUpdateKey],()=>{
+  getJobList()
+  changeKey.value++
+})
+
 
 onBeforeUnmount(() => {
   clearInterval(polling)

--
Gitblit v1.9.3