新增音频转文字功能,更新相关API和组件逻辑,优化消息处理流程,提升用户体验和代码可读性。

This commit is contained in:
超级老白兔
2025-10-25 19:11:08 +08:00
parent 27a108027b
commit 422fed2719
4 changed files with 47 additions and 34 deletions

View File

@@ -13,3 +13,20 @@ export const fetchReCallApi = async (params: FetchMomentParams) => {
const { sendCommand } = useWebSocketStore.getState();
sendCommand("CmdRecallMessage", params);
};
// 音频转文字请求参数接口
export interface VoiceToTextParams {
friendMessageId: number;
chatroomMessageId: number;
seq: number;
}
// 音频转文字
export const fetchVoiceToTextApi = async (params: VoiceToTextParams) => {
const { sendCommand } = useWebSocketStore.getState();
sendCommand("CmdVoiceToText", {
friendMessageId: params.friendMessageId,
chatroomMessageId: params.chatroomMessageId,
seq: params.seq,
});
};

View File

@@ -6,6 +6,7 @@ import {
RollbackOutlined,
ExportOutlined,
LinkOutlined,
SoundOutlined,
} from "@ant-design/icons";
import dayjs from "dayjs";
import { ChatRecord } from "@/pages/pc/ckbox/data";
@@ -113,31 +114,12 @@ const ClickMenu: React.FC<ClickMenuProps> = ({
};
// 检查是否为文本消息
const isTextMessage = (): boolean => {
// msgType === 1 是纯文本消息
if (messageData.msgType === 1) return true;
return messageData.msgType === 1;
};
// 尝试解析 content如果是 JSON 且只包含 text/content 字段,也认为是文本
try {
const parsed = JSON.parse(messageData.content || "");
// 如果包含图片、视频、语音等字段,则不是纯文本
if (
parsed.previewImage ||
parsed.tencentUrl ||
parsed.voiceUrl ||
parsed.voice ||
parsed.videoUrl ||
parsed.video ||
parsed.fileUrl ||
parsed.file
) {
return false;
}
// 包含 text 或 content 字段,认为是文本
return !!(parsed.text || parsed.content);
} catch (error) {
// 不是 JSON如果 msgType 不确定,按文本处理
return true;
}
// 检查是否为音频消息
const isAudioMessage = (): boolean => {
return messageData.msgType === 34;
};
const menuItems = [
@@ -156,6 +138,16 @@ const ClickMenu: React.FC<ClickMenuProps> = ({
},
]
: []),
// 只在音频消息时显示转文字选项
...(isAudioMessage()
? [
{
key: "voiceToText",
icon: <SoundOutlined />,
label: "转换文字",
},
]
: []),
{
key: "multipleForwarding",
icon: <CheckSquareOutlined />,

View File

@@ -14,7 +14,7 @@ import styles from "./com.module.scss";
import { useWeChatStore } from "@/store/module/weChat/weChat";
import { useContactStore } from "@/store/module/weChat/contacts";
import { useCkChatStore } from "@/store/module/ckchat/ckchat";
import { fetchReCallApi } from "./api";
import { fetchReCallApi, fetchVoiceToTextApi } from "./api";
import TransmitModal from "./components/TransmitModal";
interface MessageRecordProps {
@@ -709,6 +709,15 @@ const MessageRecord: React.FC<MessageRecordProps> = ({ contract }) => {
});
};
const handVoiceToText = messageData => {
// 音频转文字的处理逻辑
fetchVoiceToTextApi({
friendMessageId: messageData?.wechatFriendId ? messageData.id : 0,
chatroomMessageId: messageData?.wechatFriendId ? 0 : messageData.id,
seq: +new Date(),
});
};
const handQuote = (messageData: ChatRecord) => {
const isGroupUser = !!currentContract?.chatroomId;
const isSend = !!messageData.isSend;
@@ -751,6 +760,10 @@ const MessageRecord: React.FC<MessageRecordProps> = ({ contract }) => {
// 撤回逻辑
handRecall(contextMenu.messageData);
break;
case "voiceToText":
// 音频转文字逻辑
handVoiceToText(contextMenu.messageData);
break;
default:
break;
}

View File

@@ -153,7 +153,6 @@ const messageHandlers: Record<string, MessageHandler> = {
const msg = findMessageById(
message.friendMessageId || message.chatroomMessageId,
);
console.log("CmdVoiceToTextResult", msg);
const content = JSON.parse(msg.content);
if (msg) {
updateMessage(msg.id, {
@@ -163,14 +162,6 @@ const messageHandlers: Record<string, MessageHandler> = {
}),
});
}
// {
// "friendMessageId":753361041,
// "chatroomMessageId":0,
// "text":"以后我会好好对待你。",
// "seq":76698,
// "cmdType":"CmdVoiceToTextResult"
// }
console.log("CmdVoiceToTextResult", message);
},
// 可以继续添加更多处理器...