吉安感知网项目-前端
罗广辉
2026-08-13 1d552a3bad95caae4af15322df28e6240b797693
applications/mobile-web-view/src/appPages/voiceCallDetail/index.vue
@@ -22,7 +22,7 @@
            <strong v-if="callEnded" class="status-text ended-status">{{ endMessage }}</strong>
         </div>
         <div class="button-group">
            <button v-if="state === 'idle' && !receiveParameters" @click="requestCall" :disabled="!connected" class="btn btn-call">呼叫</button>
            <!-- <button v-if="state === 'idle' && !receiveParameters && !callEnded" @click="requestCall" :disabled="!connected" class="btn btn-call">呼叫</button> -->
            <button v-if="state==='ringing'" @click="acceptCall" class="btn btn-accept incoming-accept">接听</button>
            <button v-if="state==='ringing'" @click="rejectCall" class="btn btn-reject incoming-reject">拒绝</button>
@@ -43,7 +43,8 @@
const route = useRoute()
/** ✅ 你的 WS 地址前缀(后面拼 userId) */
// const WS_BASE = 'ws://218.202.104.82:38201/ws/chat?userId='
const WS_BASE = 'ws://218.202.104.82:38201/ws/chat'
// const WS_BASE = 'ws://218.202.104.82:38201/ws/chat'
const WS_BASE = 'ws://220.177.172.27:8100/webrtc/ws/chat'
const newUserInfo = ref(null)
const uid = ref('')
const peerUid = ref('')
@@ -96,6 +97,7 @@
         // 解码并解析voiceparams参数
         const params = JSON.parse(decodeURIComponent(route.query.voiceparams));
         peerUid.value = String(params.peerUid);
         contactName.value = params.callerName || '未知联系人'
         // 如果是来电类型,直接触发来电状态
         if (params.type === 'incoming') {
            log('📞 voiceparams检测到来电,直接进入来电状态');
@@ -188,7 +190,14 @@
function createPC() {
   pc = new RTCPeerConnection({
      iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
      iceServers: [{ urls: 'stun:stun.l.google.com:19302' },
         { urls: 'stun:stun.l.google.com:19302' },
         { urls: 'stun:stun1.l.google.com:19302' },
         { urls: 'stun:stun.stunprotocol.org:3478' },
         // 添加多个公共TURN服务器以提高跨网络连接成功率
         { urls: ['turn:freeturn.net:80', 'turn:freeturn.net:443'], username: 'free', credential: 'free' },
         // 备用TURN服务器
         { urls: 'turn:turn.cloudlifter.io:443', username: 'webrtc', credential: 'webrtc' }],
   })
   pc.onicecandidate = (e) => {
@@ -448,6 +457,7 @@
   send('accept', incomingFrom, null)
   log('✅ 已点击接听,发送 accept 给', incomingFrom)
   state.value = 'calling'
   // 如果 offer 已经提前到达(我们暂存了),立刻 answer
@@ -455,6 +465,9 @@
      answerOffer(lastOfferSdp, incomingFrom)
      lastOfferSdp = null
   }
   // 停止
   const transmitData = { data: { sign:'closeBox' } }
   uni.postMessage(transmitData)
}
function rejectCall() {
@@ -463,6 +476,8 @@
   // 你后端没有 reject,用 busy 表示拒绝/不可接听
   send('busy', incomingFrom, null)
   log('❌ 已拒绝来电,发送 busy 给', incomingFrom)
   const transmitData = { data: { type: 'hangupVoice',sign:'closeBox'  } }
uni.postMessage(transmitData)
   incomingFrom = null
   acceptedByMe = false
@@ -514,6 +529,9 @@
}
function hangup() {
   // 停止铃声
//    const transmitData = { data: { type: 'stopVoice' } }
// uni.postMessage(transmitData)
   cleanup(true)
}
@@ -562,11 +580,14 @@
      } else {
         log('❌ uni对象不存在,无法返回上一页')
      }
      const transmitData = { data: { type: 'hangupVoice' } }
      uni.postMessage(transmitData)
   } else {
      // 对方挂断:显示提示,延迟10秒跳转
      callEnded.value = true
      endMessage.value = '对方已挂断'
      const transmitData = { data: { type: 'hangupVoice',sign:'closeBox'  } }
      uni.postMessage(transmitData)
      // 延迟5秒后跳转页面
      endTimer.value = setTimeout(() => {
         // 返回上一页(uni-app的语音通话列表页面)
@@ -575,9 +596,10 @@
         } else {
            log('❌ uni对象不存在,无法返回上一页')
         }
         // 重置通话结束状态
         callEnded.value = false
      }, 5000)
      }, 2000)
   }
}