ZaiZai 1 năm trước cách đây
mục cha
commit
dc434cc9c5
1 tập tin đã thay đổi với 32 bổ sung15 xóa
  1. 32 15
      src/layout/index.vue

+ 32 - 15
src/layout/index.vue

@@ -197,23 +197,26 @@ const cascaderSend = async ({ projectId, contractId }) => {
 
 //长链接消息
 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)
     }
 }
@@ -228,9 +231,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])) {
@@ -238,6 +240,21 @@ onUnmounted(() => {
         }
     }
     annRefs.value = []
+}
+
+//关闭系统更新公告
+const closeAnnUpdate = () => {
+    if (!annUpdateRef.value) {
+        annUpdateRef.value?.close()
+        annUpdateRef.value = null
+    }
+}
+
+//页面卸载
+onUnmounted(() => {
+    if (!isNullES(socket)) socket.close()
+    closeAnnFun()
+    closeAnnUpdate()
 })
 </script>