吉安感知网项目-前端
罗广辉
2026-02-28 006bbfe600da32ef23ec155f5a355cdc39f5b896
uniapps/work-app/src/hooks/useGlobalWS.js
@@ -2,7 +2,7 @@
import useAppStore from "../store/modules/app/index.js";
import websocketService from "@/utils/websocket.js";
// #ifdef APP-PLUS
import { openDialog, allowFloat, getBatteryCapacity } from '@/uni_modules/lgh-dialog'
import { openDialog, allowFloat, getBatteryCapacity, showIncomingCallNotification, cancelIncomingCallNotification } from '@/uni_modules/lgh-dialog'
// #endif
export function useGlobalWS() {
   const userStore = useUserStore();
@@ -15,6 +15,17 @@
  // 存储铃声实例
  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';
  // WebSocket基础URL
@@ -23,34 +34,35 @@
   // 消息处理
  function messageHandler(payload) {
    // 先尝试直接处理消息(适用于mobile-web-view的voiceCallDetail页面的消息格式)
    const t = (payload.type || '').toString()
    callStatus.value = t
    // 处理语音通话请求
    if (t === 'call') {
      console.log('📞 全局收到来电 call,来自', payload.from)
      // 播放铃声
      // playRingtone();
      // 触发震动
      triggerVibration();
      // triggerVibration();
      // 构建来电参数
      const callParams = {
        peerUid: payload.from,
        from: payload.from,
        type: 'incoming'
      };
// #ifdef APP-PLUS
      // #ifdef APP-PLUS
      // 发送来电通知(锁屏/后台都会弹出 + 亮屏)
      showIncomingCallNotification(payload.from || '未知来电')
      // 拉起应用到前台
         openDialog()
      // #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('无法跳转到语音通话页面:当前环境不支持导航');
        }
@@ -165,18 +177,15 @@
      return
    }
    recoverTimer = setInterval(() => {
      if (websocketService.getConnected()) {
        clearInterval(recoverTimer)
        recoverTimer = null
        return
      }
      if (shouldRecoverWS()) {
      // 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()) {
@@ -184,12 +193,7 @@
    }
    try {
      websocketService.setOnMessageCallback(messageHandler);
      websocketService.setOnOpenCallback(() => {
        if (recoverTimer) {
          clearInterval(recoverTimer)
          recoverTimer = null
        }
      })
      websocketService.setOnOpenCallback(() => {})
      websocketService.setOnCloseCallback(() => {
        if (shouldRecoverWS()) {
          ensureRecoverTimer()