From f4170cfe7dbd5c7f2008233d9d16618ffec44064 Mon Sep 17 00:00:00 2001
From: shuishen <1109946754@qq.com>
Date: Fri, 27 Feb 2026 10:18:58 +0800
Subject: [PATCH] Merge branch 'master' of http://139.196.74.78:10010/r/jagzwxm/ja_web

---
 uniapps/work-app/src/hooks/useGlobalWS.js |   77 +++++++++++++++++++++++++++++++-------
 1 files changed, 63 insertions(+), 14 deletions(-)

diff --git a/uniapps/work-app/src/hooks/useGlobalWS.js b/uniapps/work-app/src/hooks/useGlobalWS.js
index 0026451..bdc3c91 100644
--- a/uniapps/work-app/src/hooks/useGlobalWS.js
+++ b/uniapps/work-app/src/hooks/useGlobalWS.js
@@ -10,8 +10,22 @@
   const callStatus = ref(null)
   // token 存储
   const accessToken = ref('')
+  // 断线恢复定时器
+  let recoverTimer = null
   // 存储铃声实例
   let ringtoneInstance = null
+
+  // 监听WebView消息事件
+  if (typeof uni !== 'undefined' && uni.$on) {
+    uni.$on('webViewMessage', (data) => {
+      console.log('📨 useGlobalWS 收到 WebView 消息:', data)
+      // 处理挂断消息
+      if (data?.type === 'hangupVoice') {
+        console.log('📞 收到挂断消息')
+        initWS()
+      }
+    });
+  }
 
   // 设置默认用户ID为3
   const defaultUserId = '2021474815063486465';
@@ -21,6 +35,7 @@
 
 	// 消息处理
   function messageHandler(payload) {
+
     // 先尝试直接处理消息(适用于mobile-web-view的voiceCallDetail页面的消息格式)
     const t = (payload.type || '').toString()
     callStatus.value = t
@@ -28,7 +43,7 @@
     if (t === 'call') {
       console.log('📞 全局收到来电 call,来自', payload.from)
       // 触发震动
-      triggerVibration();
+      // triggerVibration();
       // 构建来电参数
       const callParams = {
         peerUid: payload.from,
@@ -43,13 +58,12 @@
       // #endif
       // 转义参数以便在URL中传递
       const encodedParams = encodeURIComponent(JSON.stringify(callParams));
-
       try {
         // 优先使用uni-app的导航API(适用于同应用内跳转)
         if (typeof uni !== 'undefined' && uni.navigateTo) {
-          uni.navigateTo({
-            url: `/subPackages/voiceCallDetail/index?voiceparams=${encodedParams}`,
-          });
+            uni.navigateTo({
+              url: `/subPackages/voiceCallDetail/index?voiceparams=${encodedParams}`,
+            });
         } else {
           console.error('无法跳转到语音通话页面:当前环境不支持导航');
         }
@@ -57,12 +71,6 @@
         console.error('跳转到语音通话页面失败:', error);
       }
       return
-    }
-    // 通话接听或结束时,取消来电通知
-    if (t === 'accept' || t === 'reject' || t === 'hangup' || t === 'cancel') {
-      // #ifdef APP-PLUS
-      cancelIncomingCallNotification()
-      // #endif
     }
   }
 
@@ -150,11 +158,53 @@
   // }
 
   // 初始化WebSocket连接
+  function isVoiceCallDetailActive() {
+    try {
+      const pages = getCurrentPages()
+      const currentPage = pages[pages.length - 1]
+      const route = currentPage?.route || currentPage?.$page?.fullPath || ''
+      return String(route).includes('subPackages/voiceCallDetail/index')
+    } catch (error) {
+      return false
+    }
+  }
+
+  function shouldRecoverWS() {
+    return !isVoiceCallDetailActive() && !!useUserStore()?.userInfo?.access_token
+  }
+
+  function ensureRecoverTimer() {
+    if (recoverTimer) {
+      return
+    }
+    recoverTimer = setInterval(() => {
+      // console.log('shouldRecoverWS()',shouldRecoverWS(),'----------',websocketService.getConnected())
+      if (shouldRecoverWS() && !websocketService.getConnected()) {
+        initWS()
+      }
+    }, 3000)
+  }
+
   function initWS() {
+    websocketService.close()
     const userStore = useUserStore()
     accessToken.value =userStore?.userInfo.access_token
+    if (!accessToken.value || isVoiceCallDetailActive()) {
+      return
+    }
     try {
       websocketService.setOnMessageCallback(messageHandler);
+      websocketService.setOnOpenCallback(() => {})
+      websocketService.setOnCloseCallback(() => {
+        if (shouldRecoverWS()) {
+          ensureRecoverTimer()
+        }
+      })
+      websocketService.setOnErrorCallback(() => {
+        if (shouldRecoverWS()) {
+          ensureRecoverTimer()
+        }
+      })
 
       // 获取当前用户ID,优先使用store中的用户信息
       const userId = userStore.userInfo?.new_userInfo.userId || defaultUserId;
@@ -165,9 +215,6 @@
         // 使用用户ID初始化WebSocket连接
         websocketService.init(userId, WS_BASE, accessToken.value);
         // console.log('🌐 全局WebSocket初始化成功,用户ID:', userId);
-      } else {
-        websocketService.connect();
-
       }
     } catch (error) {
     }
@@ -183,6 +230,8 @@
     },
     { immediate: true, deep: true }
   )
+
+  ensureRecoverTimer()
 }
 
 

--
Gitblit v1.9.3