ZaiZai 1 年之前
父節點
當前提交
f23136faaf
共有 2 個文件被更改,包括 42 次插入25 次删除
  1. 2 2
      src/config/index.json
  2. 40 23
      src/layout/index.vue

+ 2 - 2
src/config/index.json

@@ -6,8 +6,8 @@
     "target4": "http://192.168.0.109:8090",
     "target5": "http://192.168.0.102:8090",
     "target6": "http://183.247.216.148:28090",
-    "socket": "wss://measure.hczcxx.cn/websocket",
-    "socket2": "ws://192.168.0.125:9527/websocket",
+    "socket1": "wss://measure.hczcxx.cn/websocket",
+    "socket": "ws://192.168.0.125:9527/websocket",
     "localModel": false,
     "smsPhone": "",
     "vite": {

+ 40 - 23
src/layout/index.vue

@@ -45,7 +45,7 @@
 </template>
 
 <script setup>
-import { onMounted, onUnmounted, ref, watch } from 'vue'
+import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
 import { useRoute, useRouter } from 'vue-router'
 import { getObjValue, isNullES, useClick } from 'js-fast-way'
 import { HcSocket } from '~src/plugins/HcSocket'
@@ -156,13 +156,6 @@ const cascaderSend = async ({ projectId, contractId }) => {
         }
         return
     }
-    //链接webSocket
-    if (!isNullES(socket)) socket.close()
-    if (!website.localModel) {
-        socket = new HcSocket({ projectId, contractId, userId: userId.value }, (res) => {
-            socketData(res?.data)
-        })
-    }
     //本地模式
     if (website.localModel) {
         setTimeout(() => {
@@ -171,27 +164,37 @@ const cascaderSend = async ({ projectId, contractId }) => {
     } else {
         reloadRouter.value = true
     }
+    //链接webSocket
+    if (!isNullES(socket)) socket.close()
+    if (!website.localModel) {
+        socket = new HcSocket({ projectId, contractId, userId: userId.value }, (res) => {
+            socketData(res?.data)
+        })
+    }
 }
 
 //长链接消息
 const annRefs = ref([])
+const annUpdateRef = ref()
 const socketData = async (res) => {
     console.log('socket:', res)
     const { type, data } = getObjValue(res)
     if (type === 'msgUpdateMsg') {
-        //更新公告
-        const ref = await HcAnnouncement({
-            type: 'update',
-            data: data,
-            //time: 'xxx年xx月xx日 xx:xx:xx',
-        })
-        annRefs.value.push(ref)
+        closeAnnUpdate()
+        //内容为空时,代表公告已经取消,由于前面已经关闭,所以不再创建
+        if (isNullES(data)) return
+        await nextTick()
+        //系统更新公告,直接替换
+        annUpdateRef.value = await HcAnnouncement({ type: 'update', data: data, time: 'xxx年xx月xx日 xx:xx:xx' })
     } else if (type === 'msgSystemMsg') {
-        //普通公告
-        const ref = await HcAnnouncement({
-            type: 'system',
-            data: data,
-        })
+        //内容为空时,代表公告已经取消,由于前面已经关闭,所以不再创建
+        if (isNullES(data)) {
+            closeAnnFun()
+            return
+        }
+        await nextTick()
+        //普通公告,追加公告
+        const ref = await HcAnnouncement({ type: 'system', data: data })
         annRefs.value.push(ref)
     } else if (type === 'msgRemind') {
         taskCount.value = data ?? 0
@@ -215,9 +218,8 @@ const logoClick = () => {
     router.push({ name: 'home-index' })
 }
 
-//页面卸载
-onUnmounted(() => {
-    if (!isNullES(socket)) socket.close()
+//关闭普通公告
+const closeAnnFun = () => {
     const refs = annRefs.value
     for (let i = 0; i < refs.length; i++) {
         if (!isNullES(refs[i])) {
@@ -225,6 +227,21 @@ onUnmounted(() => {
         }
     }
     annRefs.value = []
+}
+
+//关闭系统更新公告
+const closeAnnUpdate = () => {
+    if (!annUpdateRef.value) {
+        annUpdateRef.value?.close()
+        annUpdateRef.value = null
+    }
+}
+
+//页面卸载
+onUnmounted(() => {
+    if (!isNullES(socket)) socket.close()
+    closeAnnFun()
+    closeAnnUpdate()
 })
 </script>