|
@@ -6,6 +6,9 @@ import { useWalletStore } from "@/stores/modules/walletStore";
|
|
|
import { getMessageApi } from "@/api/path/im.api";
|
|
|
import { MSG_TYPE, MSG_TYPE_MAP } from "@/common/constant/msgType";
|
|
|
|
|
|
+
|
|
|
+const IM_PATH = import.meta.env.VITE_IM_PATH_FIlE;
|
|
|
+
|
|
|
export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
// 状态定义
|
|
|
state: () => ({
|
|
@@ -16,6 +19,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
maxReconnectAttempts: 5, // 最大重连次数
|
|
|
reconnectInterval: 3000, // 重连间隔
|
|
|
messages: [], // 消息列表
|
|
|
+ toAvatar: "",
|
|
|
unreadMessages: [], // 未读消息列表
|
|
|
uuid: null,
|
|
|
|
|
@@ -42,7 +46,11 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
uuid: params.uuid,
|
|
|
friendUsername: params.friendUsername,
|
|
|
});
|
|
|
- this.messages = data || [];
|
|
|
+ this.messages = data.map(item =>{
|
|
|
+ item.url = item.url ? IM_PATH +item.url : item.url
|
|
|
+ item.avatar = item.avatar ? IM_PATH +item.avatar : item.avatar
|
|
|
+ return item
|
|
|
+ }) || [];
|
|
|
},
|
|
|
// 初始化socket
|
|
|
startHeartbeat() {
|
|
@@ -136,14 +144,15 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
// if (MSG_TYPE_MAP.includes(message.messageType)) {
|
|
|
|
|
|
// 文本消息
|
|
|
- if (message.messageType === MSG_TYPE.TEXT) {
|
|
|
+ message.avatar = this.toAvatar
|
|
|
+ if (message.contentType === MSG_TYPE.TEXT) {
|
|
|
this.messages.push({
|
|
|
...message,
|
|
|
toUsername: message.to,
|
|
|
});
|
|
|
}
|
|
|
// 音频消息
|
|
|
- if (message.messageType === MSG_TYPE.AUDIO) {
|
|
|
+ if (message.contentType === MSG_TYPE.AUDIO) {
|
|
|
const audioBlob = new Blob([message.file], {
|
|
|
type: `audio/${message.fileSuffix}`,
|
|
|
});
|
|
@@ -241,7 +250,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
}
|
|
|
|
|
|
const walletStore = useWalletStore();
|
|
|
- const data = {
|
|
|
+ let data = {
|
|
|
...messageData,
|
|
|
fromUsername: walletStore.username,
|
|
|
from: walletStore.account,
|
|
@@ -253,8 +262,9 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
const buffer = MessageType.encode(messagePB).finish();
|
|
|
this.socket.send(buffer);
|
|
|
|
|
|
+ data.avatar = walletStore.avatar
|
|
|
// 文本消息
|
|
|
- if (data.messageType == MSG_TYPE.TEXT) {
|
|
|
+ if (data.contentType == MSG_TYPE.TEXT) {
|
|
|
this.messages.push({
|
|
|
...data,
|
|
|
toUsername: data.friendUsername,
|
|
@@ -262,15 +272,14 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
}
|
|
|
|
|
|
// 音频消息
|
|
|
- if (data.messageType === MSG_TYPE.AUDIO) {
|
|
|
- const audioBlob = new Blob([data.file], {
|
|
|
- type: `audio/${data.fileSuffix}`,
|
|
|
- });
|
|
|
- const audioUrl = URL.createObjectURL(audioBlob);
|
|
|
+ if (data.contentType === MSG_TYPE.AUDIO) {
|
|
|
+ const blob = new Blob([data.file], { type: data.fileSuffix });
|
|
|
+ const url = URL.createObjectURL(blob);
|
|
|
+ console.log("url=",url)
|
|
|
this.messages.push({
|
|
|
...data,
|
|
|
toUsername: data.to,
|
|
|
- url: audioUrl,
|
|
|
+ localUrl: url,
|
|
|
});
|
|
|
}
|
|
|
|