Compare commits

..

6 Commits

22 changed files with 84 additions and 119 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.6 KiB

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 201 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 83 KiB

View File

@@ -1,9 +1,6 @@
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { Avatar, Divider, Image, Space, SpinLoading, Toast } from "antd-mobile"; import { Avatar, Divider, Space, SpinLoading, Toast } from "antd-mobile";
import { VoiceIcon } from "@workspace/shared"; import { VoiceIcon } from "@workspace/shared";
import dogSvg from "@/assets/translate/dog.svg";
import catSvg from "@/assets/translate/cat.svg";
import pigSvg from "@/assets/translate/pig.svg";
import { Message } from "../../../types"; import { Message } from "../../../types";
import "./index.less"; import "./index.less";
import { Refresh } from "@icon-park/react"; import { Refresh } from "@icon-park/react";
@@ -21,90 +18,36 @@ function Index(props: DefinedProps) {
const [currentPlayingId, setCurrentPlayingId] = useState<number>(); const [currentPlayingId, setCurrentPlayingId] = useState<number>();
useEffect(() => { useEffect(() => {
if (isRecording) { if (isRecording && currentPlayingId) {
stopAllAudio(); audioRefs.current[currentPlayingId].pause();
audioRefs.current[currentPlayingId].currentTime = 0;
setCurrentPlayingId(undefined);
} }
}, [isRecording]); }, [isRecording, currentPlayingId]);
const onVoiceChange = () => {
setIsPlating(!isPlaying);
};
const playAudio = (id: number, audioUrl: string) => { const playAudio = (id: number, audioUrl: string) => {
if (isRecording) { if (isRecording) {
Toast.show("录音中,无法播放音频"); Toast.show("录音中,无法播放音频");
return; return;
} }
if (currentPlayingId === id && audioRefs.current[id]) {
if (audioRefs.current[id]) {
audioRefs.current[id].pause();
audioRefs.current[id].currentTime = 0;
}
setCurrentPlayingId(undefined);
setIsPlating(false);
return;
}
stopAllAudio();
if (!audioRefs.current[id]) {
audioRefs.current[id] = new Audio(audioUrl);
}
const audio = audioRefs.current[id];
audio.currentTime = 0;
audio.onended = () => {
setCurrentPlayingId(undefined);
setIsPlating(false);
};
audio.onerror = () => {
const error = audio.error;
switch (error?.code) {
case MediaError.MEDIA_ERR_ABORTED:
console.warn("音频播放被用户中断");
break;
case MediaError.MEDIA_ERR_NETWORK:
console.warn("网络错误,无法加载音频");
break;
case MediaError.MEDIA_ERR_DECODE:
console.warn("解码失败,可能是格式不支持");
break;
case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
console.warn("浏览器不支持该音频格式");
break;
default:
console.warn("未知错误");
}
Toast.show("音频播放失败");
setIsPlating(false);
};
audio
.play()
.then(() => {
setCurrentPlayingId(id);
setIsPlating(true);
})
.catch((error) => {
console.error("音频播放失败:", error);
Toast.show("音频播放失败");
});
};
const stopAllAudio = () => {
if (currentPlayingId && audioRefs.current[currentPlayingId]) { if (currentPlayingId && audioRefs.current[currentPlayingId]) {
audioRefs.current[currentPlayingId].pause(); audioRefs.current[currentPlayingId].pause();
audioRefs.current[currentPlayingId].currentTime = 0; audioRefs.current[currentPlayingId].currentTime = 0;
setIsPlating(false); }
if (currentPlayingId !== id) {
setCurrentPlayingId(id);
audioRefs.current[id] = new Audio(audioUrl);
audioRefs.current[id].play();
audioRefs.current[id].onended = () => {
setCurrentPlayingId(undefined);
};
} else {
audioRefs.current[id].pause();
audioRefs.current[id].currentTime = 0;
setCurrentPlayingId(undefined); setCurrentPlayingId(undefined);
} }
Object.values(audioRefs.current).forEach((audio) => {
if (!audio.paused) {
audio.pause();
audio.currentTime = 0;
}
});
}; };
const renderAvatar = (item: Message) => { const renderAvatar = (item: Message) => {
return <Avatar src={item.petAvatar || ""} style={{ "--border-radius": "32px" }} />; return <Avatar src={item.petAvatar || ""} style={{ "--border-radius": "32px" }} />;
}; };
@@ -157,54 +100,30 @@ function Index(props: DefinedProps) {
return ( return (
<div className="message"> <div className="message">
{data.map((item, index) => ( {data.map((item, index) => (
<div className="item" key={index} onClick={() => playAudio(item.id, item.contentText)}> <div className="item" key={index}>
{renderAvatar(item)} {renderAvatar(item)}
<div className="rig"> <div className="rig">
<div> <div>
<span className="name">{item.petName}</span> <span className="name">
{item.isTranslating && !item.isRefresh ? "" : item.petName ?? "未知宠物"}
</span>
<Divider direction="vertical" style={{ margin: "0px 8px" }} /> <Divider direction="vertical" style={{ margin: "0px 8px" }} />
<span className="">{item.createTime}</span> <span className="">{item.createTime}</span>
</div> </div>
<div className="voice-container"> <div className="voice-container" onClick={() => playAudio(item.id, item.contentText)}>
<VoiceIcon <VoiceIcon
onChange={onVoiceChange} // onChange={onVoiceChange}
isPlaying={isPlaying && currentPlayingId === item.id} isPlaying={currentPlayingId === item.id}
/> />
<div className="time">{item.contentDuration}''</div> <div className="time">{item.contentDuration}''</div>
</div> </div>
{renderTranslateResult(item)} {renderTranslateResult(item)}
{/* {item.isTranslating ? (
<div className="translate">
<SpinLoading color="default" style={{ "--size": "12px" }} />
<span>翻译中...</span>
</div>
) : item.transStatus === 1 ? (
<div className="translate">
{item.transResult?.length ? (
item.transResult
) : (
<Space justify={"between"}>
<span>未知语句</span>
<Refresh onClick={(e) => refreshMessage(item.id, e)} size="12" fill="#333" />
</Space>
)}
</div>
) : (
<Space justify={"between"} className="translate">
<span>翻译失败,请重试</span>
<Refresh onClick={(e) => refreshMessage(item.id, e)} size="12" fill="#333" />
</Space>
// <div className="translate">
// <span> 翻译失败,请重试</span>
// <Refresh onClick={(e) => refreshMessage(item.id, e)} size="12" fill="#333" />
// </div>
)} */}
</div> </div>
</div> </div>
))} ))}
<div style={{ height: "130px", width: "100%" }}></div> <div style={{ height: "80px", width: "100%" }}></div>
</div> </div>
); );
} }

View File

@@ -2,7 +2,7 @@ import { useCallback, useEffect, useState } from "react";
import { Image, Toast } from "antd-mobile"; import { Image, Toast } from "antd-mobile";
import MessageCom from "./component/message"; import MessageCom from "./component/message";
import VoiceRecord from "./component/voice"; import VoiceRecord from "./component/voice";
import { XPopup, FloatingMenu, type FloatMenuItemConfig } from "@workspace/shared"; import { XPopup, type FloatMenuItemConfig } from "@workspace/shared";
import type { Message } from "../types"; import type { Message } from "../types";
import { useGetDialog } from "@/api/getDialog"; import { useGetDialog } from "@/api/getDialog";
import { useUploadAudio } from "@/api/translate"; import { useUploadAudio } from "@/api/translate";
@@ -10,7 +10,7 @@ import { useUploadAudio } from "@/api/translate";
import dogSvg from "@/assets/translate/dog.svg"; import dogSvg from "@/assets/translate/dog.svg";
import catSvg from "@/assets/translate/cat.svg"; import catSvg from "@/assets/translate/cat.svg";
import pigSvg from "@/assets/translate/pig.svg"; import pigSvg from "@/assets/translate/pig.svg";
import { MoreTwo, Petrol } from "@icon-park/react"; import { MoreTwo } from "@icon-park/react";
import SearchCom from "./component/search"; import SearchCom from "./component/search";
interface DefinedProps { interface DefinedProps {
searchVisible: boolean; searchVisible: boolean;
@@ -31,16 +31,25 @@ function Index(props: DefinedProps) {
const { loading: _audioLoading, error: _audioError, uploadAudio } = useUploadAudio(); const { loading: _audioLoading, error: _audioError, uploadAudio } = useUploadAudio();
const [messages, setMessages] = useState<Message[]>([]); const [messages, setMessages] = useState<Message[]>([]);
const [isRecording, setIsRecording] = useState(false); //是否录音中 const [isRecording, setIsRecording] = useState(false); //是否录音中
const [currentLanguage, setCurrentLanguage] = useState<FloatMenuItemConfig>(); const [_currentLanguage, setCurrentLanguage] = useState<FloatMenuItemConfig>();
const [visible, setVisible] = useState<boolean>(false); const [visible, setVisible] = useState<boolean>(false);
const [dialogId, setDialogId] = useState<number>(0); const [dialogId, setDialogId] = useState<number>(0);
useEffect(() => { useEffect(() => {
setCurrentLanguage(menuItems[0]); setCurrentLanguage(menuItems[0]);
fetchInitialMessages(); fetchInitialMessages();
}, []); }, []);
// 滚动到底部
const scrollToBottom = useCallback(() => {
const container = document.querySelector(".message");
if (container) {
container.scrollTop = container.scrollHeight;
console.log("container", container.scrollHeight);
}
}, []);
// 添加初始化数据的逻辑 // 添加初始化数据的逻辑
const fetchInitialMessages = async () => { const fetchInitialMessages = async () => {
try { try {
@@ -59,6 +68,15 @@ function Index(props: DefinedProps) {
} }
}; };
// 监听消息变化,自动滚动到底部
useEffect(() => {
if (messages.length > 0) {
requestAnimationFrame(() => {
scrollToBottom();
});
}
}, [messages, scrollToBottom]);
//完成录音 //完成录音
const onRecordingComplete = useCallback( const onRecordingComplete = useCallback(
(audioUrl: string, actualDuration: number, formData: FormData) => { (audioUrl: string, actualDuration: number, formData: FormData) => {
@@ -95,6 +113,7 @@ function Index(props: DefinedProps) {
...msg, ...msg,
...translatedData.data, ...translatedData.data,
isTranslating: false, isTranslating: false,
isRefresh: false,
} }
: msg : msg
) )
@@ -110,6 +129,7 @@ function Index(props: DefinedProps) {
transStatus: translatedData.data.transStatus, transStatus: translatedData.data.transStatus,
createTime: translatedData.data.createTime, createTime: translatedData.data.createTime,
isTranslating: false, isTranslating: false,
isRefresh: false,
} }
: msg : msg
) )
@@ -125,6 +145,7 @@ function Index(props: DefinedProps) {
? { ? {
...msg, ...msg,
isTranslating: false, isTranslating: false,
isRefresh: false,
translatedText: "翻译失败,请重试", translatedText: "翻译失败,请重试",
} }
: msg : msg
@@ -142,6 +163,7 @@ function Index(props: DefinedProps) {
? { ? {
...msg, ...msg,
isTranslating: true, isTranslating: true,
isRefresh: true,
} }
: msg : msg
) )
@@ -153,13 +175,13 @@ function Index(props: DefinedProps) {
setIsRecording(flag); setIsRecording(flag);
}; };
const onLanguage = (item: FloatMenuItemConfig) => { // const onLanguage = (item: FloatMenuItemConfig) => {
if (item.type === "add") { // if (item.type === "add") {
setVisible(true); // setVisible(true);
} else { // } else {
setCurrentLanguage(item); // setCurrentLanguage(item);
} // }
}; // };
return ( return (
<div className="translate-container"> <div className="translate-container">
@@ -169,11 +191,34 @@ function Index(props: DefinedProps) {
</div> </div>
)} )}
{/* <div onClick={() => scrollToBottom()}>111</div> */}
<MessageCom <MessageCom
data={messages} data={messages}
isRecording={isRecording} isRecording={isRecording}
onRefresh={refreshMessages} onRefresh={refreshMessages}
></MessageCom> ></MessageCom>
{/* <div
className="message-container"
style={{
flex: 1,
overflowY: "auto",
minHeight: 0,
display: "flex",
flexDirection: "column",
}}
>
<MessageCom
data={messages}
isRecording={isRecording}
onRefresh={refreshMessages}
></MessageCom>
<div ref={messagesEndRef} style={{ float: "left", clear: "both", height: "1px" }} />
</div> */}
{/* <div className="he" ref={messagesEndRef} /> */}
<VoiceRecord <VoiceRecord
dialogId={dialogId} dialogId={dialogId}
onRecordingComplete={onRecordingComplete} onRecordingComplete={onRecordingComplete}

View File

@@ -11,4 +11,5 @@ export interface Message {
isPlaying?: boolean; isPlaying?: boolean;
messageStatus?: 0 | 1; messageStatus?: 0 | 1;
transStatus?: 0 | 1; transStatus?: 0 | 1;
isRefresh?: boolean;
} }