This commit is contained in:
weipengfei 2023-10-12 16:06:59 +08:00
parent 2fd4cd8798
commit 0301413779
3 changed files with 42 additions and 3 deletions

View File

@ -3,6 +3,7 @@ declare namespace Chat {
interface Chat { interface Chat {
dateTime: string dateTime: string
text: string text: string
mp3: Array<any> | null
inversion?: boolean inversion?: boolean
error?: boolean error?: boolean
loading?: boolean loading?: boolean

View File

@ -1,5 +1,5 @@
<script setup lang='ts'> <script setup lang='ts'>
import { computed, ref } from 'vue' import { computed, ref, reactive } from 'vue'
import { NDropdown, useMessage } from 'naive-ui' import { NDropdown, useMessage } from 'naive-ui'
import AvatarComponent from './Avatar.vue' import AvatarComponent from './Avatar.vue'
import TextComponent from './Text.vue' import TextComponent from './Text.vue'
@ -12,6 +12,7 @@ import { copyToClip } from '@/utils/copy'
interface Props { interface Props {
dateTime?: string dateTime?: string
text?: string text?: string
mp3?: Array<any>
inversion?: boolean inversion?: boolean
error?: boolean error?: boolean
loading?: boolean loading?: boolean
@ -90,6 +91,31 @@ async function handleCopy() {
message.error('复制失败') message.error('复制失败')
} }
} }
function radioPlay(){
console.log('播放', props.mp3);
for (let i = 0; i < props.mp3.value.length; i++) {
audioElements.push(new Audio(props.mp3.value[i]));
}
playAudio()
}
//
const audioElements = reactive([]);
//
const playAudio = () => {
for (let i = 0; i < audioElements.length; i++) {
audioElements[i].play();
}
};
//
const pauseAudio = () => {
for (let i = 0; i < audioElements.length; i++) {
audioElements[i].pause();
}
};
</script> </script>
<template> <template>
@ -127,6 +153,14 @@ async function handleCopy() {
@click="handleRegenerate" @click="handleRegenerate"
> >
<SvgIcon icon="ri:restart-line" /> <SvgIcon icon="ri:restart-line" />
</button>
<button
v-if="!inversion"
class="mb-2 transition text-neutral-300 hover:text-neutral-800 dark:hover:text-neutral-300"
@click="radioPlay"
>
播放
<!-- <SvgIcon icon="ri:restart-line" /> -->
</button> </button>
<NDropdown <NDropdown
:trigger="isMobile ? 'click' : 'hover'" :trigger="isMobile ? 'click' : 'hover'"

View File

@ -169,11 +169,12 @@ async function onConversation() {
try { try {
let lastText = '' let lastText = ''
let mp3: any = []
console.log('发送消息', message) console.log('发送消息', message)
const fetchChatAPIOnce = async () => { const fetchChatAPIOnce = async () => {
socket.send(JSON.stringify({ socket.send(JSON.stringify({
tts: 0, tts: 1,
data: [ data: [
{ {
role: 'user', role: 'user',
@ -187,13 +188,15 @@ async function onConversation() {
const msg = JSON.parse(event.data) const msg = JSON.parse(event.data)
// console.log(`: `, msg.payload.choices.text[0].content); // console.log(`: `, msg.payload.choices.text[0].content);
// console.log(`: `, dataSources.value[dataSources.value.length - 1].text); // console.log(`: `, dataSources.value[dataSources.value.length - 1].text);
lastText += msg.payload.choices.text[0].content lastText += msg.payload.choices.text[0].content;
mp3.push(msg.payload.choices.mp3)
updateChat( updateChat(
+uuid, +uuid,
dataSources.value.length - 1, dataSources.value.length - 1,
{ {
dateTime: new Date().toLocaleString(), dateTime: new Date().toLocaleString(),
text: lastText, text: lastText,
mp3: mp3,
inversion: false, inversion: false,
error: false, error: false,
loading: true, loading: true,
@ -632,6 +635,7 @@ const click = () => {
:key="index" :key="index"
:date-time="item.dateTime" :date-time="item.dateTime"
:text="item.text" :text="item.text"
:mp3="item.mp3"
:inversion="item.inversion" :inversion="item.inversion"
:error="item.error" :error="item.error"
:loading="item.loading" :loading="item.loading"