liming 1 周之前
父节点
当前提交
9c61d2b7d9
共有 3 个文件被更改,包括 37 次插入33 次删除
  1. 30 26
      src/stores/modules/webSocketStore.js
  2. 3 4
      src/views/im/chat/index.vue
  3. 4 3
      src/views/im/hook/messagesHook.js

+ 30 - 26
src/stores/modules/webSocketStore.js

@@ -6,6 +6,7 @@ import { useWalletStore } from "@/stores/modules/walletStore";
 import { getMessageApi } from "@/api/path/im.api";
 import { MSG_TYPE, MSG_TYPE_MAP } from "@/common/constant/msgType";
 
+
 import {
   setMessageHook,
   handleMessageHook,
@@ -52,6 +53,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
 
   // 方法
   actions: {
+    // 获取消息
     async getMessages(params) {
       const { data } = await getMessageApi({
         messageType: params.messageType,
@@ -64,6 +66,34 @@ export const useWebSocketStore = defineStore("webSocketStore", {
           return item;
         }) || [];
     },
+    // 发送消息
+    sendMessage(messageData) {
+      if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
+        console.error("WebSocket未连接");
+        return false;
+      }
+ 
+      // 获取url上uuid参数 
+      const walletStore = useWalletStore();
+      let data = {
+        ...messageData,
+        fromUsername: walletStore.username,
+        from: walletStore.account,
+        to: this.toUserInfo.uuid,
+      }; 
+      try {
+        const MessageType = protobuf.lookupType("protocol.Message");
+        const messagePB = MessageType.create(data);
+        const buffer = MessageType.encode(messagePB).finish();
+        this.socket.send(buffer);
+        data.avatar = walletStore.avatar;
+        setMessageHook(data, this);
+        return true;
+      } catch (error) {
+        console.error("消息编码错误:", error);
+        return false;
+      }
+    },
     // 初始化socket
     startHeartbeat() {
       const self = this;
@@ -135,33 +165,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
         this.reconnect();
       };
     },
-    // 发送消息
-    sendMessage(messageData) {
-      if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
-        console.error("WebSocket未连接");
-        return false;
-      }
 
-      const walletStore = useWalletStore();
-      let data = {
-        ...messageData,
-        fromUsername: walletStore.username,
-        from: walletStore.account,
-      };
-      console.log("发送消息=", data);
-      try {
-        const MessageType = protobuf.lookupType("protocol.Message");
-        const messagePB = MessageType.create(data);
-        const buffer = MessageType.encode(messagePB).finish();
-        this.socket.send(buffer);
-        data.avatar = walletStore.avatar;
-        setMessageHook(data, this);
-        return true;
-      } catch (error) {
-        console.error("消息编码错误:", error);
-        return false;
-      }
-    },
     // 处理消息
     handleMessage(data) {
       const MessageType = protobuf.lookupType("protocol.Message");

+ 3 - 4
src/views/im/chat/index.vue

@@ -341,7 +341,7 @@ const sendAudioMessage = async (event) => {
       content: text.value, // 如果有文本内容
       contentType: MSG_TYPE.AUDIO, // 音频消息类型
       messageType: MESSAGE_TYPE_USER, // 单聊消息
-      to: route.query.uuid, // 接收方ID
+
       fileSuffix: "wav", // 使用webm后缀更准确
       file: audioData, // 将Uint8Array转为普通数组
     };
@@ -363,7 +363,7 @@ const sendMessage = () => {
     content: text.value,
     contentType: MSG_TYPE.TEXT, // 1: 文本消息
     messageType: MESSAGE_TYPE_USER, // 1: 单聊天
-    to: route.query.uuid,
+   
   };
   wsStore.sendMessage(message);
   text.value = "";
@@ -377,7 +377,6 @@ const afterRead = async (file) => {
     content: text.value, // 如果有文本内容
     contentType: MSG_TYPE.IMAGE, // 音频消息类型
     messageType: MESSAGE_TYPE_USER, // 单聊消息
-    to: route.query.uuid, // 接收方ID
     fileSuffix: file.type, // 使用webm后缀更准确
     file: new Uint8Array(arrayBuffer), // 将Uint8Array转为普通数组
   };
@@ -403,7 +402,6 @@ const startAudioOnline = async () => {
   rtcStore.initConnection(MESSAGE_TYPE_USER, {
     fromUsername: walletStore.username,
     from: walletStore.account,
-    to: route.query.uuid,
   });
   // 获取本地媒体流并添加到连接
   try {
@@ -445,6 +443,7 @@ const formatTime = (timestamp) => {
 
 // 页面生命周期
 onMounted(() => {
+  wsStore.toUserInfo.uuid = route.query.uuid;
   wsStore.getMessages({
     uuid: walletStore.account,
     messageType: 1,

+ 4 - 3
src/views/im/hook/messagesHook.js

@@ -1,12 +1,13 @@
 import { MSG_TYPE, MSG_TYPE_MAP } from "@/common/constant/msgType";
 
 // 发送消息处理
-export const setMessageHook = (message, state) => {
+export const setMessageHook = (message, state) => { 
   // 文本消息
   if (message.contentType == MSG_TYPE.TEXT) {
     state.messages.push({
       ...message,
       toUsername: message.friendUsername,
+      
     });
   }
 
@@ -23,7 +24,7 @@ export const setMessageHook = (message, state) => {
   }
 
   // 图片
-  if (message.contentType === MSG_TYPE.IMAGE) { 
+  if (message.contentType === MSG_TYPE.IMAGE) {
     const blob = new Blob([message.file], { type: message.fileSuffix });
     const url = URL.createObjectURL(blob);
     state.messages.push({
@@ -58,7 +59,7 @@ export const handleMessageHook = (message, state) => {
   }
 
   // 图片
-  if (message.contentType === MSG_TYPE.IMAGE) { 
+  if (message.contentType === MSG_TYPE.IMAGE) {
     const blob = new Blob([message.file], { type: message.fileSuffix });
     const url = URL.createObjectURL(blob);
     state.messages.push({