吉安感知网项目-前端
张含笑
2026-02-25 ff84d50405bed732a19469e7cd9d72b1a9042222
feat:铃声和震动
3 files modified
1 files added
129 ■■■■ changed files
applications/mobile-web-view/src/appPages/voiceCallDetail/index.vue 10 ●●●●● patch | view | raw | blame | history
uniapps/work-app/src/hooks/useGlobalWS.js 112 ●●●● patch | view | raw | blame | history
uniapps/work-app/src/static/audio/ringtone.mp3 patch | view | raw | blame | history
uniapps/work-app/src/subPackages/voiceCallDetail/index.vue 7 ●●●● patch | view | raw | blame | history
applications/mobile-web-view/src/appPages/voiceCallDetail/index.vue
@@ -448,6 +448,9 @@
    send('accept', incomingFrom, null)
    log('✅ 已点击接听,发送 accept 给', incomingFrom)
    // 停止铃声
    const transmitData = { data: { type: 'stopVoice' } }
uni.postMessage(transmitData)
    state.value = 'calling'
    // 如果 offer 已经提前到达(我们暂存了),立刻 answer
@@ -463,6 +466,10 @@
    // 你后端没有 reject,用 busy 表示拒绝/不可接听
    send('busy', incomingFrom, null)
    log('❌ 已拒绝来电,发送 busy 给', incomingFrom)
    // 停止铃声
    const transmitData = { data: { type: 'stopVoice' } }
uni.postMessage(transmitData)
    incomingFrom = null
    acceptedByMe = false
@@ -514,6 +521,9 @@
}
function hangup() {
    // 停止铃声
    const transmitData = { data: { type: 'stopVoice' } }
uni.postMessage(transmitData)
    cleanup(true)
}
uniapps/work-app/src/hooks/useGlobalWS.js
@@ -8,6 +8,8 @@
  const callStatus = ref(null)
  // token 存储
  const accessToken = ref('')
  // 存储铃声实例
  let ringtoneInstance = null
  // 设置默认用户ID为3
  const defaultUserId = '2021474815063486465';
@@ -23,6 +25,10 @@
    // 处理语音通话请求
    if (t === 'call') {
      console.log('📞 全局收到来电 call,来自', payload.from)
      // 播放铃声
      playRingtone();
      // 触发震动
      triggerVibration();
      // 构建来电参数
      const callParams = {
        peerUid: payload.from,
@@ -47,31 +53,89 @@
      }
      return
    }
  }
    // 然后处理biz_code格式的消息
    // switch (bizCode) {
    //   case 'JOB_ISREFRESH':
    //     appStore.setJobUpdateKeyAdd()
    //     break
    //   case 'DEVICE_ISREFRESH':
    //     appStore.setDeviceUpdateKeyAdd()
    //     break
    //   case 'DOWNLOAD_PROGRESS':
    //     break
    //   case 'LOGOUT_USER':
    //     userStore.setUserInfo(null)
    //     uni.reLaunch({
    //       url: '/pages/login/index'
    //     })
    //     break
    //   case 'VoiceCall':
    //     // enterRoom(payload, userId.value)
    //     break
    //   default:
    //     // 记录未处理的消息
    //     console.log('未处理的WebSocket消息:', payload)
    //     break;
    // }
  // 播放来电铃声 - 定义在 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 triggerVibration() {
    try {
      if (typeof uni !== 'undefined') {
        // 方法1:使用uni.vibrate(现代API)
        if (uni.vibrate) {
          // 震动400ms
          uni.vibrate({
            duration: 400,
            success: () => {
              console.log('📳 震动已触发');
            },
            fail: (error) => {
              // 方法2:使用uni.vibrateLong(兼容旧设备)
              if (uni.vibrateLong) {
                uni.vibrateLong({
                  success: () => {
                    console.log('📳 兼容模式震动已触发');
                  },
                  fail: (err) => {
                    console.error('兼容模式震动失败:', err);
                  }
                });
              }
            }
          });
        }
        // 方法3:使用plus API(H5+)
        if (typeof plus !== 'undefined' && plus.device && plus.device.vibrate) {
          plus.device.vibrate(400);
          console.log('📳 H5+ 震动已触发');
        }
      }
    } catch (error) {
      console.error('触发震动失败:', error);
    }
  }
  // 停止铃声
  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连接
uniapps/work-app/src/static/audio/ringtone.mp3
Binary files differ
uniapps/work-app/src/subPackages/voiceCallDetail/index.vue
@@ -78,7 +78,12 @@
  viewUrl.value = getWebViewUrl("/voiceCallDetail", { contact: options.contact ,voiceparams: options.voiceparams});
});
function onPostMessage(data) {}
function onPostMessage(data) {
    // 将WebView消息传递给全局WebSocket处理
    if (typeof uni !== 'undefined' && uni.$emit) {
        uni.$emit('webViewMessage', data);
    }
}
</script>