吉安感知网项目-前端
罗广辉
2026-02-26 057bcf202227150ca64c615751c5ea8c79687b18
uniapps/work-app/src/hooks/useGlobalWS.js
@@ -1,8 +1,9 @@
import { useUserStore } from "@/store/index.js";
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'
// #endif
export function useGlobalWS() {
   const userStore = useUserStore();
  const appStore = useAppStore();
@@ -27,7 +28,7 @@
    if (t === 'call') {
      console.log('📞 全局收到来电 call,来自', payload.from)
      // 播放铃声
      playRingtone();
      // playRingtone();
      // 触发震动
      triggerVibration();
      // 构建来电参数
@@ -36,8 +37,9 @@
        from: payload.from,
        type: 'incoming'
      };
// #ifdef APP-PLUS
         openDialog()
      // #endif
      // 转义参数以便在URL中传递
      const encodedParams = encodeURIComponent(JSON.stringify(callParams));
@@ -58,21 +60,21 @@
  }
  // 播放来电铃声 - 定义在 messageHandler 外部
  function playRingtone() {
    try {
      if (typeof uni !== 'undefined' && uni.createInnerAudioContext) {
        const ringtone = uni.createInnerAudioContext();
        // 使用默认铃声,注意路径写法
        ringtone.src = '/static/audio/ringtone.mp3';
        ringtone.loop = true;
        ringtone.volume = 0.8;
        ringtone.play();
        // 存储铃声实例
        ringtoneInstance = ringtone; // 使用局部变量而不是window
      }
    } catch (error) {
    }
  }
  // function playRingtone() {
  //   try {
  //     if (typeof uni !== 'undefined' && uni.createInnerAudioContext) {
  //       const ringtone = uni.createInnerAudioContext();
  //       // 使用默认铃声,注意路径写法
  //       ringtone.src = '/static/audio/ringtone.mp3';
  //       ringtone.loop = true;
  //       ringtone.volume = 0.8;
  //       ringtone.play();
  //       // 存储铃声实例
  //       ringtoneInstance = ringtone; // 使用局部变量而不是window
  //     }
  //   } catch (error) {
  //   }
  // }
  // 触发震动
  function triggerVibration() {
@@ -113,32 +115,32 @@
  }
  // 停止铃声
  function stopRingtone() {
    try {
      if (ringtoneInstance) {
        ringtoneInstance.stop();
        ringtoneInstance.destroy();
        ringtoneInstance = null;
      }
    } catch (error) {
      // 即使出错也重置实例
      ringtoneInstance = null;
    }
  }
  // 监听停止铃声事件
  if (typeof uni !== 'undefined' && uni.$on) {
    uni.$on('stopRingtone', stopRingtone);
    // 监听WebView消息事件
    uni.$on('webViewMessage', (data) => {
      // 检查是否是停止铃声消息
      if (data?.type === 'stopVoice') {
        stopRingtone();
        console.log('📳 收到WebView停止铃声消息,已停止铃声');
      }
    });
  }
  // function stopRingtone() {
  //   try {
  //     if (ringtoneInstance) {
  //       ringtoneInstance.stop();
  //       ringtoneInstance.destroy();
  //       ringtoneInstance = null;
  //     }
  //   } catch (error) {
  //     // 即使出错也重置实例
  //     ringtoneInstance = null;
  //   }
  // }
  //
  // // 监听停止铃声事件
  // if (typeof uni !== 'undefined' && uni.$on) {
  //   uni.$on('stopRingtone', stopRingtone);
  //   // 监听WebView消息事件
  //   uni.$on('webViewMessage', (data) => {
  //     // 检查是否是停止铃声消息
  //     if (data?.type === 'stopVoice') {
  //       stopRingtone();
  //       console.log('📳 收到WebView停止铃声消息,已停止铃声');
  //
  //     }
  //   });
  // }
  // 初始化WebSocket连接
  function initWS() {