吉安感知网项目-前端
张含笑
2026-02-28 3383015d8c40890a1509e7fa502bf05845e4e189
feat:来电人昵称
4 files modified
107 ■■■■ changed files
applications/mobile-web-view/src/appPages/voiceCallDetail/index.vue 1 ●●●● patch | view | raw | blame | history
uniapps/work-app/src/hooks/useGlobalWS.js 88 ●●●●● patch | view | raw | blame | history
uniapps/work-app/src/pages/voiceCall/index.vue 7 ●●●● patch | view | raw | blame | history
uniapps/work-app/src/store/modules/app/index.js 11 ●●●●● patch | view | raw | blame | history
applications/mobile-web-view/src/appPages/voiceCallDetail/index.vue
@@ -96,6 +96,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检测到来电,直接进入来电状态');
uniapps/work-app/src/hooks/useGlobalWS.js
@@ -1,6 +1,7 @@
import { useUserStore } from "@/store/index.js";
import useAppStore from "../store/modules/app/index.js";
import websocketService from "@/utils/websocket.js";
import { getPhoneBookListApi } from '@/api/voiceCall/index.js'
// #ifdef APP-PLUS
import { openDialog, allowFloat, getBatteryCapacity, showIncomingCallNotification, cancelIncomingCallNotification } from '@/uni_modules/lgh-dialog'
// #endif
@@ -32,8 +33,31 @@
  // const WS_BASE = 'wss://wrj.shuixiongit.com/ws/chat?userId=';
  const WS_BASE = 'ws://218.202.104.82:38201/ws/chat';
    // 消息处理
  function messageHandler(payload) {
    // 获取通讯录数据
  async function fetchContactList() {
    try {
      const params = {
        current: 1,
        size: 1000, // 一次性获取更多数据
        nickName: ''
      };
      const res = await getPhoneBookListApi(params);
      const response = res.data.data;
      // 过滤掉与当前登录用户userId一致的联系人
      const filteredRecords = response.records.filter(contact => {
        return contact.userId && String(contact.userId) !== String(userStore.userInfo.new_userInfo.userId);
      });
      // 更新通讯录到全局状态管理
      appStore.updateContactList(filteredRecords);
      return filteredRecords;
    } catch (error) {
      console.error('📞 获取通讯录失败:', error);
      return [];
    }
  }
  // 消息处理
  async function messageHandler(payload) {
    // 先尝试直接处理消息(适用于mobile-web-view的voiceCallDetail页面的消息格式)
    const t = (payload.type || '').toString()
@@ -41,17 +65,26 @@
    // 处理语音通话请求
    if (t === 'call') {
      console.log('📞 全局收到来电 call,来自', payload.from)
      // 如果通讯录为空,尝试获取一次
      if (appStore.contactList.length === 0) {
        await fetchContactList();
      }
      // 从全局状态管理中获取联系人信息
      const contact = appStore.getContactByUserId(payload.from)
      const callerName = contact?.nickName || '未知联系人'
      // 触发震动
      // triggerVibration();
      // 构建来电参数
      const callParams = {
        peerUid: payload.from,
        from: payload.from,
        type: 'incoming'
        type: 'incoming',
        callerName: callerName
      };
      // #ifdef APP-PLUS
      // 发送来电通知(锁屏/后台都会弹出 + 亮屏)
      showIncomingCallNotification(payload.from || '未知来电')
      showIncomingCallNotification(callerName || '未知来电')
      // 拉起应用到前台
            openDialog()
      // #endif
@@ -72,23 +105,6 @@
      return
    }
  }
  // 播放来电铃声 - 定义在 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() {
@@ -128,33 +144,6 @@
    }
  }
  // 停止铃声
  // 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 isVoiceCallDetailActive() {
@@ -177,7 +166,6 @@
      return
    }
    recoverTimer = setInterval(() => {
      // console.log('shouldRecoverWS()',shouldRecoverWS(),'----------',websocketService.getConnected())
      if (shouldRecoverWS() && !websocketService.getConnected()) {
        initWS()
      }
@@ -207,7 +195,7 @@
      // 获取当前用户ID,优先使用store中的用户信息
      const userId = userStore.userInfo?.new_userInfo.userId || defaultUserId;
            console.log('🌐 全局WebSocket初始化开始,用户ID:', userId)
            // console.log('🌐 全局WebSocket初始化开始,用户ID:', userId)
      // 检查是否已经有活跃的WebSocket连接
      if (!websocketService.getConnected() || websocketService.userId !== userId) {
uniapps/work-app/src/pages/voiceCall/index.vue
@@ -43,12 +43,13 @@
<script setup>
import { getStatusBarHeight } from '@/utils/common';
import { useUserStore } from "@/store/index.js"
import { useUserStore, useAppStore } from "@/store/index.js"
const topMargin = getStatusBarHeight()
import { getPhoneBookListApi } from '@/api/voiceCall/index.js'
import { onShow } from '@dcloudio/uni-app'
import defaultAvatar from '/static/images/defaultAvatar.svg'
const userStore = useUserStore()
const appStore = useAppStore()
const userId = userStore.userInfo.new_userInfo.userId
// 联系人数据
const contacts = ref([])
@@ -81,8 +82,12 @@
    })
    if (defaultParam.value.current === 1) {
      contacts.value = filteredRecords
      // 更新通讯录到全局状态管理
      appStore.updateContactList(filteredRecords)
    } else {
      contacts.value = [...contacts.value, ...filteredRecords]
      // 更新通讯录到全局状态管理
      appStore.updateContactList(contacts.value)
    }
    // 判断是否还有更多数据
uniapps/work-app/src/store/modules/app/index.js
@@ -10,8 +10,12 @@
    theme: storage.get(THEME_KEY) || 'light',
    deviceUpdateKey: 0, //设备刷新key
    jobUpdateKey: 0, //任务刷新key
    contactList: [], // 通讯录列表,用于快速查找联系人信息
  }),
  getters: {
    getContactByUserId: (state) => (userId) => {
      return state.contactList.find(contact => String(contact.userId) === String(userId))
    },
    getSystemInfo (state) {
      return state.systemInfo
    },
@@ -74,6 +78,13 @@
          icon: 'error'
        })
      })
    },
    /**
     * 更新通讯录列表
     * @param {Array} contacts 通讯录列表
     */
    updateContactList (contacts) {
      this.contactList = contacts
    }
  }
})