From 006bbfe600da32ef23ec155f5a355cdc39f5b896 Mon Sep 17 00:00:00 2001
From: 罗广辉 <guanghui.luo@foxmail.com>
Date: Sat, 28 Feb 2026 09:48:27 +0800
Subject: [PATCH] feat: 设备类型判断
---
uniapps/work-app/src/hooks/useGlobalWS.js | 76 +++++++++++++++++++++++++++++++-------
1 files changed, 62 insertions(+), 14 deletions(-)
diff --git a/uniapps/work-app/src/hooks/useGlobalWS.js b/uniapps/work-app/src/hooks/useGlobalWS.js
index 0026451..28cf75b 100644
--- a/uniapps/work-app/src/hooks/useGlobalWS.js
+++ b/uniapps/work-app/src/hooks/useGlobalWS.js
@@ -10,8 +10,21 @@
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') {
+ initWS()
+ }
+ });
+ }
// 设置默认用户ID为3
const defaultUserId = '2021474815063486465';
@@ -21,6 +34,7 @@
// 消息处理
function messageHandler(payload) {
+
// 先尝试直接处理消息(适用于mobile-web-view的voiceCallDetail页面的消息格式)
const t = (payload.type || '').toString()
callStatus.value = t
@@ -28,7 +42,7 @@
if (t === 'call') {
console.log('📞 全局收到来电 call,来自', payload.from)
// 触发震动
- triggerVibration();
+ // triggerVibration();
// 构建来电参数
const callParams = {
peerUid: payload.from,
@@ -43,13 +57,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 +70,6 @@
console.error('跳转到语音通话页面失败:', error);
}
return
- }
- // 通话接听或结束时,取消来电通知
- if (t === 'accept' || t === 'reject' || t === 'hangup' || t === 'cancel') {
- // #ifdef APP-PLUS
- cancelIncomingCallNotification()
- // #endif
}
}
@@ -150,11 +157,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 +214,6 @@
// 使用用户ID初始化WebSocket连接
websocketService.init(userId, WS_BASE, accessToken.value);
// console.log('🌐 全局WebSocket初始化成功,用户ID:', userId);
- } else {
- websocketService.connect();
-
}
} catch (error) {
}
@@ -183,6 +229,8 @@
},
{ immediate: true, deep: true }
)
+
+ ensureRecoverTimer()
}
--
Gitblit v1.9.3