DialogContent.vue 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. <template>
  2. <div class="dialog-content-wrapper">
  3. <!-- 遮罩层 -->
  4. <div v-if="showMask" class="mask-layer" ref="maskLayer">
  5. <div class="play-button-container">
  6. <button class="play-button" @click="startPlayback">
  7. <el-icon class="play-icon"><VideoPlay /></el-icon>
  8. </button>
  9. </div>
  10. </div>
  11. <!-- 标题 -->
  12. <div class="title-box">
  13. <!-- 返回 -->
  14. <div class="title-left">
  15. <div class="box-icon" @click="goBackToMain">
  16. <el-icon class="left-icon"><ArrowLeftBold /></el-icon>
  17. {{ backText }}
  18. </div>
  19. </div>
  20. <!-- 标题 -->
  21. <div class="title-center">
  22. <div class="title-text" :title="currentSection?.name">
  23. {{ currentSection?.name }}
  24. </div>
  25. </div>
  26. <!-- 左右按钮 -->
  27. <div class="title-right">
  28. <div class="arrow-icon-circle" @click="playPrevious" :class="{ 'disabled': currentSectionIndex === 0 && currentDialogueIndex === 0 }">
  29. <el-icon class="arrow-icon"><CaretLeft /></el-icon>
  30. </div>
  31. <div class="arrow-icon-circle" @click="togglePlay">
  32. <span class="play-text">{{ isPlaying ? '暂停' : '自动' }}</span>
  33. </div>
  34. <div class="arrow-icon-circle" @click="playNext" :class="{ 'disabled': currentSectionIndex === scriptData.sections.length - 1 && currentDialogueIndex === currentSection.dialogues.length - 1 }">
  35. <el-icon class="arrow-icon"><CaretRight /></el-icon>
  36. </div>
  37. </div>
  38. </div>
  39. <!-- 内容区域 -->
  40. <div class="content-box">
  41. <!-- 人物形象 -->
  42. <div
  43. v-if="currentDialogue && (currentDialogue.type === 'digital' || currentDialogue.type === 'quest')"
  44. :key="`character-${currentDialogueIndex}`"
  45. class="character"
  46. :class="{
  47. 'left': getCharacterSide(currentDialogue.roleName) === 'left',
  48. 'right': getCharacterSide(currentDialogue.roleName) === 'right'
  49. }"
  50. :style="{ backgroundImage: `url(${getCharacterImage(currentDialogue.roleName)})` }"
  51. ></div>
  52. <!-- 对话卡片 -->
  53. <div
  54. v-if="currentDialogue && (currentDialogue.type === 'digital' || currentDialogue.type === 'quest')"
  55. :key="`dialogue-${currentDialogueIndex}`"
  56. class="dialogue-card"
  57. :class="{
  58. 'left': getCharacterSide(currentDialogue.roleName) === 'left',
  59. 'right': getCharacterSide(currentDialogue.roleName) === 'right'
  60. }"
  61. >
  62. <div class="dialogue-header">
  63. <span class="role-name">{{ currentDialogue?.roleName }}</span>
  64. </div>
  65. <div class="dialogue-content" v-html="parseMarkdown(currentDialogue.content)"></div>
  66. </div>
  67. <!-- 用户输入卡片 -->
  68. <div
  69. v-if="currentDialogue.type === 'user'"
  70. class="dialogue-card user-input-card"
  71. >
  72. <div class="dialogue-header">
  73. <span class="role-name">我</span>
  74. </div>
  75. <div class="dialogue-content">
  76. <textarea
  77. :value="userInput"
  78. @input="e => userInput = e.target.value"
  79. class="user-input-textarea"
  80. placeholder="请输入内容..."
  81. @keyup.enter.exact="submitUserInput"
  82. ></textarea>
  83. <div class="input-actions">
  84. <button class="cancel-btn" @click="cancelUserInput">清空</button>
  85. <button class="submit-btn" @click="submitUserInput">发送</button>
  86. </div>
  87. </div>
  88. </div>
  89. <!-- 输入按钮区域 -->
  90. <div class="input-buttons-container" v-if="currentDialogue.type === 'user'">
  91. <!-- 语音输入按钮 -->
  92. <div class="voice-input-outer" :class="{ 'recording': isVoiceRecording }">
  93. <VoiceInput
  94. @voiceRecognized="handleVoiceRecognized"
  95. @recordingStatusChanged="handleRecordingStatusChanged"
  96. />
  97. </div>
  98. </div>
  99. </div>
  100. <img :src="currentBackgroundImage" alt="背景图" class="background-image">
  101. </div>
  102. </template>
  103. <script setup>
  104. import { ref, computed, onMounted, onUnmounted, watch } from 'vue'
  105. import { useRouter } from 'vue-router'
  106. import { ArrowLeftBold, CaretLeft, CaretRight, Grid, VideoPlay } from '@element-plus/icons-vue'
  107. import VoiceInput from '@/components/ai/voice/VoiceInput.vue'
  108. import { marked } from 'marked'
  109. import {CreateDialogue, sendChatMessageStream} from "@/api/questions.js";
  110. import {useAudioPlayer} from "@/api/tts/useAudioPlayer.js";
  111. const { playAudioChunk , stopPlayback, setOnPlaybackComplete } = useAudioPlayer();
  112. // 路由实例
  113. const router = useRouter()
  114. const props = defineProps({
  115. scriptData: {
  116. type: Object,
  117. default: () => ({
  118. title: "",
  119. sections: []
  120. })
  121. },
  122. scriptRoles: {
  123. type: Array,
  124. default: () => []
  125. },
  126. backText: {
  127. type: String,
  128. default: '返回课程'
  129. }
  130. })
  131. // 对话相关状态
  132. // 当前章节索引
  133. const currentSectionIndex = ref(0)
  134. // 当前对话索引
  135. const currentDialogueIndex = ref(0)
  136. // 是否正在播放
  137. const isPlaying = ref(false)
  138. // 用户输入内容
  139. const userInput = ref('')
  140. // 语音录音状态
  141. const isVoiceRecording = ref(false)
  142. // 实时语音识别结果
  143. const voiceRecognizedText = ref("")
  144. // 录音开始时的光标位置
  145. const recordingStartCursorPos = ref(0)
  146. // 录音开始时的原始文本
  147. const recordingStartText = ref("")
  148. // 音频对象
  149. // 背景音频
  150. const backgroundAudio = ref(null)
  151. // 对话音频
  152. const dialogueAudio = ref(null)
  153. // 遮罩层显示状态
  154. const showMask = ref(true)
  155. // 计算属性
  156. // 当前章节信息
  157. const currentSection = computed(() => {
  158. return props.scriptData.sections[currentSectionIndex.value]
  159. })
  160. // 当前对话信息
  161. const currentDialogue = computed(() => {
  162. if (!currentSection.value) return null
  163. return currentSection.value.dialogues[currentDialogueIndex.value]
  164. })
  165. const currentBackgroundImage = computed(() => {
  166. if (!currentSection.value || !currentSection.value.backgroundImage || !currentSection.value.backgroundImage.url) {
  167. return ''
  168. }
  169. return currentSection.value.backgroundImage.url
  170. })
  171. // 当前对话缓存
  172. const currentDialogueCache = ref(null)
  173. // 方法
  174. const handleVoiceRecognized = (text) => {
  175. console.log('语音识别结果:', text)
  176. if (isVoiceRecording.value) {
  177. // 在同一次录音过程中,实时更新文本框内容
  178. voiceRecognizedText.value = text
  179. const textarea = document.querySelector('.user-input-textarea')
  180. if (textarea) {
  181. // 使用录音开始时的原始文本和光标位置
  182. const startPos = recordingStartCursorPos.value
  183. const originalText = recordingStartText.value
  184. // 在光标位置插入实时识别结果
  185. userInput.value = originalText.substring(0, startPos) + text + originalText.substring(startPos)
  186. }
  187. } else {
  188. // 在录音结束时,将最终的语音内容追加到userInput.value
  189. const textarea = document.querySelector('.user-input-textarea')
  190. if (textarea) {
  191. // 使用录音开始时的光标位置和原始文本
  192. const startPos = recordingStartCursorPos.value
  193. const originalText = recordingStartText.value
  194. // 在光标位置插入文本
  195. userInput.value = originalText.substring(0, startPos) + text + originalText.substring(startPos)
  196. // 重新设置光标位置到插入文本的末尾
  197. setTimeout(() => {
  198. textarea.selectionStart = textarea.selectionEnd = startPos + text.length
  199. }, 0)
  200. } else {
  201. // 如果没有找到输入框,直接替换整个内容
  202. userInput.value = text
  203. }
  204. // 清空临时变量
  205. voiceRecognizedText.value = ""
  206. }
  207. }
  208. // 处理录音状态变化
  209. const handleRecordingStatusChanged = (isRecording) => {
  210. console.log('录音状态:', isRecording)
  211. const wasRecording = isVoiceRecording.value
  212. isVoiceRecording.value = isRecording
  213. // 如果是从未录音状态切换到录音状态,记录当前光标位置和文本内容
  214. if (!wasRecording && isRecording) {
  215. const textarea = document.querySelector('.user-input-textarea')
  216. if (textarea) {
  217. recordingStartCursorPos.value = textarea.selectionStart
  218. recordingStartText.value = userInput.value
  219. } else {
  220. recordingStartCursorPos.value = 0
  221. recordingStartText.value = userInput.value
  222. }
  223. }
  224. // 如果是从录音状态切换到非录音状态,只需要清空临时变量
  225. if (wasRecording && !isRecording) {
  226. // 清空临时变量
  227. voiceRecognizedText.value = ""
  228. }
  229. }
  230. // 提交用户输入
  231. const submitUserInput = async () => {
  232. if (userInput.value.trim()) {
  233. console.log('用户输入:', userInput.value)
  234. await createAiChart();
  235. await doSendMessage();
  236. }
  237. }
  238. // 取消用户输入
  239. const cancelUserInput = () => {
  240. userInput.value = ''
  241. }
  242. // 解析 Markdown 内容
  243. const parseMarkdown = (content) => {
  244. if (!content) return ''
  245. return marked(content)
  246. }
  247. const getCharacterSide = () => {
  248. return currentDialogueIndex.value % 2 === 0 ? 'left' : 'right'
  249. }
  250. // 根据角色ID获取角色名称
  251. const getRole = (roleName) => {
  252. return props.scriptRoles.find(r => r.name === roleName)
  253. }
  254. const getCharacterImage = (roleName) => {
  255. const role = getRole(roleName)
  256. return role ? role.avatar : ''
  257. }
  258. const stopAllAudio = () => {
  259. if (backgroundAudio.value) {
  260. backgroundAudio.value.pause()
  261. backgroundAudio.value.currentTime = 0
  262. }
  263. if (dialogueAudio.value) {
  264. dialogueAudio.value.pause()
  265. dialogueAudio.value.currentTime = 0
  266. }
  267. }
  268. const playBackgroundAudio = () => {
  269. // 停止之前的背景音
  270. if (backgroundAudio.value) {
  271. backgroundAudio.value.pause()
  272. backgroundAudio.value.currentTime = 0
  273. }
  274. // 播放当前环节的背景音
  275. if (currentSection.value?.backgroundAudio?.url && isPlaying.value) {
  276. backgroundAudio.value = new Audio(currentSection.value.backgroundAudio.url)
  277. backgroundAudio.value.loop = true
  278. backgroundAudio.value.volume = 1
  279. backgroundAudio.value.play().catch(e => console.error('背景音播放失败:', e))
  280. }
  281. }
  282. const playDialogueAudio = (isAutoPlay = false) => {
  283. // 停止之前的对话语音
  284. if (dialogueAudio.value) {
  285. dialogueAudio.value.pause()
  286. dialogueAudio.value.currentTime = 0
  287. }
  288. // 播放当前对话的语音
  289. if (currentDialogue.value?.voiceoverUrl) {
  290. const audio = new Audio(currentDialogue.value.voiceoverUrl)
  291. dialogueAudio.value = audio
  292. // 音频结束事件
  293. audio.onended = () => {
  294. // 如果是自动播放状态,继续播放下一条
  295. if (isAutoPlay && isPlaying.value) {
  296. setTimeout(() => {
  297. if (!playNext(true)) {
  298. // 播放完毕
  299. isPlaying.value = false
  300. stopAllAudio()
  301. }
  302. }, 100)
  303. }
  304. }
  305. // 播放音频
  306. audio.play().catch(e => {
  307. console.error('对话语音播放失败:', e)
  308. // 播放失败时,2秒后跳转(暂时注销,因为会导致user类型数字人回复播报跳转后语音播报报错,直接2秒跳转)
  309. // if (isAutoPlay && isPlaying.value) {
  310. // setTimeout(() => {
  311. // if (!playNext(true)) {
  312. // // 播放完毕
  313. // isPlaying.value = false
  314. // stopAllAudio()
  315. // }
  316. // }, 2000)
  317. // }
  318. })
  319. } else {
  320. // 如果没有语音文件,2秒后跳转
  321. if (isAutoPlay && isPlaying.value && currentDialogue.value?.type !== 'user') {
  322. setTimeout(() => {
  323. if (!playNext(true)) {
  324. // 播放完毕
  325. isPlaying.value = false
  326. stopAllAudio()
  327. }
  328. }, 2000)
  329. }
  330. }
  331. }
  332. const togglePlay = () => {
  333. isPlaying.value = !isPlaying.value
  334. if (isPlaying.value) {
  335. // 播放背景音
  336. playBackgroundAudio()
  337. // 开始播放序列
  338. playSequence()
  339. } else {
  340. // 暂停所有音频
  341. stopAllAudio()
  342. }
  343. }
  344. // 自动播放序列
  345. const playSequence = () => {
  346. if (!isPlaying.value) return
  347. // 如果当前是用户输入卡片,暂停播放等待用户输入
  348. if (currentDialogue.value?.type === 'user') return
  349. // 播放当前对话语音,传递isAutoPlay参数
  350. playDialogueAudio(true)
  351. }
  352. const playPrevious = () => {
  353. // 停止当前音频
  354. stopAllAudio()
  355. // 如果正在进行数字人对话,调用stopStream清理
  356. recoverQuestDialogue()
  357. stopPlayback(false) // 不触发回调
  358. if (conversationInProgress.value) {
  359. stopStream()
  360. }
  361. if (currentDialogueIndex.value > 0) {
  362. currentDialogueIndex.value--
  363. } else if (currentSectionIndex.value > 0) {
  364. currentSectionIndex.value--
  365. const section = props.scriptData.sections[currentSectionIndex.value]
  366. currentDialogueIndex.value = section.dialogues.length - 1
  367. }
  368. // 播放背景音
  369. playBackgroundAudio()
  370. // 播放当前对话语音
  371. if (isPlaying.value) {
  372. playDialogueAudio(true)
  373. } else {
  374. playDialogueAudio()
  375. }
  376. }
  377. const playNext = (isAutoPlay = false) => {
  378. // 停止当前对话语音
  379. if (dialogueAudio.value) {
  380. dialogueAudio.value.pause()
  381. dialogueAudio.value.currentTime = 0
  382. }
  383. // 如果正在进行数字人对话,调用stopStream清理
  384. recoverQuestDialogue()
  385. stopPlayback(false)
  386. if (conversationInProgress.value) {
  387. stopStream()
  388. }
  389. if (currentSection.value && currentDialogueIndex.value < currentSection.value.dialogues.length - 1) {
  390. currentDialogueIndex.value++
  391. // 根据是否为自动播放状态决定如何播放语音
  392. if (isPlaying.value) {
  393. playDialogueAudio(true)
  394. } else {
  395. playDialogueAudio()
  396. }
  397. return true
  398. } else if (currentSectionIndex.value < props.scriptData.sections.length - 1) {
  399. currentSectionIndex.value++
  400. currentDialogueIndex.value = 0
  401. // 播放背景音
  402. playBackgroundAudio()
  403. // 根据是否为自动播放状态决定如何播放语音
  404. if (isPlaying.value) {
  405. playDialogueAudio(true)
  406. } else {
  407. playDialogueAudio()
  408. }
  409. return true
  410. }
  411. return false
  412. }
  413. // 返回主页按钮点击事件
  414. const goBackToMain = () => {
  415. // 停止所有音频
  416. stopAllAudio()
  417. // 跳转到 ai-general-course 页面,保持侧边栏选中状态
  418. router.push('/ai-general-course')
  419. }
  420. // 开始播放
  421. const maskLayer = ref(null)
  422. const startPlayback = () => {
  423. // 消失动画
  424. if (maskLayer.value) {
  425. maskLayer.value.classList.add('fade-out')
  426. // 等待动画完成后隐藏遮罩层
  427. setTimeout(() => {
  428. showMask.value = false
  429. }, 500)
  430. }
  431. // 播放当前对话语音
  432. playDialogueAudio()
  433. }
  434. // 键盘事件处理,键盘左右箭头控制对话
  435. const handleKeydown = (event) => {
  436. // 如果遮罩层显示,不处理键盘事件
  437. if (showMask.value) {
  438. return
  439. }
  440. // 如果当前是用户输入对话,不处理键盘事件,让默认行为生效(在输入框中左右移动光标)
  441. if (currentDialogue.value?.type === 'user') {
  442. return
  443. }
  444. // 处理左右箭头键
  445. switch (event.key) {
  446. case 'ArrowLeft':
  447. playPrevious()
  448. event.preventDefault() // 防止默认行为
  449. break
  450. case 'ArrowRight':
  451. playNext()
  452. event.preventDefault() // 防止默认行为
  453. break
  454. default:
  455. // 其他按键不做处理
  456. break
  457. }
  458. }
  459. // 监听环节变化
  460. watch(currentSectionIndex, () => {
  461. playBackgroundAudio()
  462. })
  463. // 监听 scriptData 变化(侧边栏切换话题时)
  464. watch(() => props.scriptData, (newVal, oldVal) => {
  465. if (newVal && oldVal && newVal !== oldVal) {
  466. // 停止所有音频
  467. stopAllAudio()
  468. // 如果正在进行数字人对话,调用stopStream清理
  469. recoverQuestDialogue()
  470. stopPlayback(false)
  471. if (conversationInProgress.value) {
  472. stopStream()
  473. }
  474. // 清空索引
  475. currentSectionIndex.value = 0
  476. currentDialogueIndex.value = 0
  477. // 重置播放状态
  478. isPlaying.value = false
  479. // 显示遮罩层
  480. showMask.value = true
  481. // 清空用户输入
  482. userInput.value = ''
  483. // 清空对话缓存
  484. currentDialogueCache.value = null
  485. }
  486. }, { deep: true })
  487. // 会话ID
  488. const activeConversationId = ref(null)
  489. const conversationInProgress = ref(false)
  490. const conversationInAbortController = ref()
  491. const receiveMessageFullText = ref('')
  492. //创建对话
  493. const createAiChart = async () => {
  494. let role = props.scriptRoles.find(r => r.name === currentDialogue.value.roleName)
  495. // 智能问答
  496. await CreateDialogue({ roleId: role.id })
  497. .then(res => {
  498. console.log("创建会话:", res.data);
  499. activeConversationId.value = res.data
  500. })
  501. .catch(error => {
  502. console.error('请求出错:', error)
  503. })
  504. }
  505. /** 真正执行【发送】消息操作 */
  506. const doSendMessage = async () => {
  507. // 校验
  508. if (userInput.value.length < 1) {
  509. console.error('发送失败,原因:内容为空!')
  510. return
  511. }
  512. if (activeConversationId.value == null) {
  513. console.error('还没创建对话,不能发送!')
  514. return
  515. }
  516. let userInputTemp = userInput.value;
  517. let currentDialogueTemp = currentSection.value.dialogues[currentDialogueIndex.value-1]
  518. userInputTemp += "(此内容是帮我解答的问题,问题是:" + currentDialogueTemp.content + ",回复要求:根据问题处理我回答的内容是否正确并给予鼓励或夸赞;注意请使用精简回答,尽量控制字体数量在50个字内)"
  519. // 执行发送
  520. await doSendMessageStream({
  521. conversationId: activeConversationId.value,
  522. content: userInputTemp,
  523. contentAnswer: null,
  524. })
  525. // 清空输入框
  526. userInput.value = ''
  527. }
  528. /** 显示问题回答对话 */
  529. const showQuestAnswerDialogue = () => {
  530. // 缓存当前对话
  531. currentDialogueCache.value = JSON.parse(JSON.stringify(currentDialogue.value))
  532. // 将当前对话类型设置为数字人对话
  533. currentDialogue.value.type = "digital"
  534. // 设置默认内容为"让我思考一下..."
  535. currentDialogue.value.content = "让我思考一下..."
  536. }
  537. /** 回复对话 */
  538. const recoverQuestDialogue = () => {
  539. // 如果有缓存的对话
  540. if (currentDialogueCache.value){
  541. // 恢复当前对话
  542. const currentSection = props.scriptData.sections[currentSectionIndex.value]
  543. if (currentSection) {
  544. currentSection.dialogues[currentDialogueIndex.value] = JSON.parse(JSON.stringify(currentDialogueCache.value))
  545. }
  546. // 清空缓存
  547. currentDialogueCache.value = null
  548. }
  549. }
  550. /** 真正执行【发送】消息操作 */
  551. const doSendMessageStream = async userMessage => {
  552. // 创建 AbortController 实例,以便中止请求
  553. conversationInAbortController.value = new AbortController()
  554. // 标记对话进行中
  555. conversationInProgress.value = true
  556. // 设置为空
  557. receiveMessageFullText.value = ''
  558. showQuestAnswerDialogue()
  559. try {
  560. // 发送 event stream
  561. let isFirstChunk = true // 是否是第一个 chunk 消息段
  562. await sendChatMessageStream(
  563. userMessage.conversationId,
  564. userMessage.content,
  565. userMessage.contentAnswer,
  566. conversationInAbortController.value,
  567. true, // enableContext 参数
  568. async res => {
  569. const { code, data, msg } = JSON.parse(res.data)
  570. if (code !== 0) {
  571. console.log(`对话异常! ${msg}`)
  572. stopStream();
  573. return
  574. }
  575. if (data.eventType === 'TEXT') {
  576. // 如果内容为空,就不处理。
  577. if (data.receive?.content === '') {
  578. return
  579. }
  580. receiveMessageFullText.value += data.receive.content
  581. // 更新数字人对话框内容
  582. currentDialogue.value.content = receiveMessageFullText.value
  583. // 首次返回需要添加一个 message 到页面,后面的都是更新
  584. if (isFirstChunk) {
  585. isFirstChunk = false
  586. //第一次返回
  587. } else {
  588. //更新最后一条消息
  589. }
  590. }
  591. if (data.eventType === 'AUDIO') {
  592. // 处理音频消息
  593. await playAudioChunk(data.audioData);
  594. }
  595. },
  596. error => {
  597. console.log(`对话异常! ${error}`)
  598. stopStream()
  599. // 需要抛出异常,禁止重试
  600. throw error
  601. },
  602. () => {
  603. console.log(`结束对话! `)
  604. stopStream()
  605. }
  606. )
  607. } catch (error) {
  608. console.error('发送消息失败:', error)
  609. stopStream()
  610. }
  611. }
  612. /** 停止 stream 流式调用 */
  613. const stopStream = async () => {
  614. // 如果 stream 进行中的 message,就需要调用 controller 结束
  615. if (conversationInAbortController.value) {
  616. conversationInAbortController.value.abort()
  617. }
  618. // 销毁语音读取
  619. // stopPlayback();
  620. // 设置为 false
  621. conversationInProgress.value = false
  622. console.log(`结束对话!更改状态: `,conversationInProgress.value)
  623. }
  624. // 处理音频播放完成
  625. const handleAudioPlaybackComplete = () => {
  626. console.log('智能问答音频播放完成');
  627. // AI回答完成后,如果之前是播放状态,继续播放
  628. stopAllAudio();
  629. // 如果处于自动播放状态,继续播放下一条对话
  630. if (isPlaying.value) {
  631. if (playNext()) {
  632. // 继续自动播放序列
  633. playSequence();
  634. } else {
  635. // 播放完毕
  636. isPlaying.value = false;
  637. stopAllAudio();
  638. }
  639. }
  640. };
  641. // 组件挂载时添加键盘事件监听
  642. onMounted(() => {
  643. window.addEventListener('keydown', handleKeydown)
  644. // 播放背景音
  645. playBackgroundAudio()
  646. // // 播放当前对话语音
  647. // playDialogueAudio()
  648. // 设置音频播放完成回调
  649. setOnPlaybackComplete(handleAudioPlaybackComplete)
  650. })
  651. // 组件卸载时移除键盘事件监听和停止音频
  652. onUnmounted(() => {
  653. window.removeEventListener('keydown', handleKeydown)
  654. stopAllAudio()
  655. stopPlayback(false)
  656. })
  657. </script>
  658. <style scoped lang="scss">
  659. @use "sass:math";
  660. @function rpx($px) {
  661. @return math.div($px, 750) * 100vw;
  662. }
  663. .dialog-content-wrapper {
  664. width: 100%;
  665. height: 100%;
  666. position: absolute;
  667. top: 0;
  668. left: 0;
  669. z-index: 100;
  670. }
  671. .title-box {
  672. position: absolute;
  673. top: 0;
  674. left: 0;
  675. right: 0;
  676. height: rpx(60);
  677. display: flex;
  678. align-items: center;
  679. justify-content: space-between;
  680. color: white;
  681. padding: 0 rpx(20);
  682. z-index: 10;
  683. }
  684. .title-left {
  685. width: 33.33%;
  686. height: 100%;
  687. display: flex;
  688. align-items: center;
  689. gap: rpx(5);
  690. .box-icon {
  691. display: flex;
  692. align-items: center;
  693. color: #0064BE;
  694. gap: rpx(5);
  695. padding: rpx(5) rpx(10);
  696. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  697. border: rpx(1) solid rgba(0, 100, 192);
  698. border-radius: rpx(30);
  699. backdrop-filter: blur(10px);
  700. cursor: pointer;
  701. transition: all 0.3s ease;
  702. font-size: rpx(9);
  703. font-weight: 500;
  704. width: fit-content;
  705. }
  706. .box-icon:hover {
  707. background-color: rgba(255, 255, 255, 90%);
  708. transform: translateX(-3px);
  709. }
  710. .left-icon {
  711. font-size: rpx(12);
  712. }
  713. }
  714. .title-center {
  715. width: 33.33%;
  716. display: flex;
  717. align-items: center;
  718. justify-content: center;
  719. background-image: url('@/assets/dialogue/number-title.png');
  720. background-size: contain;
  721. background-repeat: no-repeat;
  722. background-position: center;
  723. height: 100%;
  724. }
  725. .title-text {
  726. height: 100%;
  727. font-size: rpx(12);
  728. font-weight: bold;
  729. white-space: nowrap;
  730. overflow: hidden;
  731. text-overflow: ellipsis;
  732. max-width: 50%;
  733. display: flex;
  734. align-items: center;
  735. justify-content: left;
  736. }
  737. .title-right {
  738. width: 33.33%;
  739. height: 100%;
  740. display: flex;
  741. align-items: center;
  742. justify-content: flex-end;
  743. gap: rpx(10);
  744. .arrow-icon-circle {
  745. width: rpx(20);
  746. height: rpx(20);
  747. border-radius: 50%;
  748. border: rpx(1) solid rgba(0, 100, 192);
  749. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  750. display: flex;
  751. align-items: center;
  752. justify-content: center;
  753. cursor: pointer;
  754. transition: all 0.3s ease;
  755. }
  756. .arrow-icon-circle:hover:not(.disabled) {
  757. transform: scale(1.1);
  758. box-shadow: 0 rpx(1) rpx(6) rgba(0, 0, 0, 0.3);
  759. }
  760. .arrow-icon-circle:active:not(.disabled) {
  761. transform: scale(0.95);
  762. }
  763. .arrow-icon-circle.disabled {
  764. opacity: 0.5;
  765. cursor: not-allowed;
  766. border-color: rgba(0, 100, 192, 0.3);
  767. background: linear-gradient(135deg, rgba(160, 220, 240, 0.5), rgba(80, 190, 240, 0.5));
  768. }
  769. .arrow-icon {
  770. font-size: rpx(15);
  771. color: #0064BE;
  772. }
  773. .play-text {
  774. font-size: rpx(7);
  775. color: #0064BE;
  776. font-weight: 500;
  777. }
  778. }
  779. .background-image {
  780. width: 100%;
  781. height: 100%;
  782. object-fit: cover;
  783. z-index: 1;
  784. position: relative;
  785. }
  786. /* 遮罩层样式 */
  787. .mask-layer {
  788. position: absolute;
  789. top: 0;
  790. left: 0;
  791. right: 0;
  792. bottom: 0;
  793. background-color: rgba(0, 0, 0, 0.7);
  794. z-index: 20;
  795. display: flex;
  796. justify-content: center;
  797. align-items: center;
  798. opacity: 1;
  799. transition: all 0.5s ease-out;
  800. }
  801. .mask-layer.fade-out {
  802. opacity: 0;
  803. transform: scale(1.1);
  804. z-index: -1;
  805. }
  806. .play-button-container {
  807. display: flex;
  808. justify-content: center;
  809. align-items: center;
  810. }
  811. .play-button {
  812. width: rpx(80);
  813. height: rpx(80);
  814. border-radius: 50%;
  815. border: none;
  816. background: transparent;
  817. display: flex;
  818. justify-content: center;
  819. align-items: center;
  820. cursor: pointer;
  821. outline: none;
  822. -webkit-tap-highlight-color: transparent;
  823. }
  824. .play-icon {
  825. font-size: rpx(40);
  826. color: #A0DCF0;
  827. transition: all 0.3s ease;
  828. cursor: pointer;
  829. }
  830. .play-icon:hover {
  831. transform: scale(1.2);
  832. color: #50BEF0;
  833. text-shadow: 0 0 rpx(10) rgba(64, 158, 255, 0.5);
  834. }
  835. .content-box {
  836. position: absolute;
  837. top: rpx(60);
  838. left: 0;
  839. right: 0;
  840. bottom: 0;
  841. display: flex;
  842. align-items: flex-end;
  843. justify-content: center;
  844. padding-bottom: rpx(10);
  845. z-index: 10;
  846. }
  847. .character {
  848. position: absolute;
  849. bottom: 0;
  850. width: rpx(135);
  851. height: rpx(240);
  852. background-size: contain;
  853. background-position: bottom;
  854. background-repeat: no-repeat;
  855. opacity: 0;
  856. z-index: 1;
  857. // background-color: #fff;
  858. }
  859. .character.left {
  860. left: rpx(17);
  861. transform: translateX(-100%);
  862. animation: characterEnterLeft 0.8s ease forwards;
  863. }
  864. .character.right {
  865. right: rpx(17);
  866. transform: translateX(100%) scaleX(-1);
  867. animation: characterEnterRight 0.8s ease forwards;
  868. }
  869. @keyframes characterEnterLeft {
  870. 0% {
  871. opacity: 0;
  872. transform: translateX(-100%) scale(0.8);
  873. }
  874. 70% {
  875. opacity: 0.9;
  876. transform: translateX(10%) scale(1.05);
  877. }
  878. 100% {
  879. opacity: 1;
  880. transform: translateX(0) scale(1);
  881. }
  882. }
  883. @keyframes characterEnterRight {
  884. 0% {
  885. opacity: 0;
  886. transform: translateX(100%) scale(0.8) scaleX(-1);
  887. }
  888. 70% {
  889. opacity: 0.9;
  890. transform: translateX(-10%) scale(1.05) scaleX(-1);
  891. }
  892. 100% {
  893. opacity: 1;
  894. transform: translateX(0) scale(1) scaleX(-1);
  895. }
  896. }
  897. .dialogue-card {
  898. background: rgba(255, 255, 255, 0.9);
  899. border-radius: rpx(6);
  900. padding: rpx(8);
  901. max-width: 20%;
  902. min-width: rpx(150);
  903. box-shadow: 0 rpx(3.5) rpx(10) rgba(0, 0, 0, 0.15);
  904. position: absolute;
  905. bottom: rpx(50);
  906. width: auto;
  907. display: inline-block;
  908. z-index: 2;
  909. }
  910. .dialogue-card.left {
  911. left: rpx(145);
  912. animation: dialogueEnterLeft 0.6s ease forwards;
  913. }
  914. .dialogue-card.right {
  915. right: rpx(145);
  916. animation: dialogueEnterRight 0.6s ease forwards;
  917. }
  918. .dialogue-card.left::before {
  919. content: '';
  920. position: absolute;
  921. left: rpx(-11.5);
  922. bottom: rpx(12);
  923. width: 0;
  924. height: 0;
  925. border-top: rpx(7) solid transparent;
  926. border-bottom: rpx(7) solid transparent;
  927. border-right: rpx(12) solid rgba(255, 255, 255, 0.9);
  928. transform: translateY(0);
  929. }
  930. .dialogue-card.right::before {
  931. content: '';
  932. position: absolute;
  933. right: rpx(-11.5);
  934. bottom: rpx(12);
  935. width: 0;
  936. height: 0;
  937. border-top: rpx(7) solid transparent;
  938. border-bottom: rpx(7) solid transparent;
  939. border-left: rpx(12) solid rgba(255, 255, 255, 0.9);
  940. transform: translateY(0);
  941. }
  942. @keyframes dialogueEnterLeft {
  943. from {
  944. opacity: 0;
  945. transform: translateX(-rpx(30)) translateY(rpx(20));
  946. }
  947. to {
  948. opacity: 1;
  949. transform: translateX(0) translateY(0);
  950. }
  951. }
  952. @keyframes dialogueEnterRight {
  953. from {
  954. opacity: 0;
  955. transform: translateX(rpx(30)) translateY(rpx(20));
  956. }
  957. to {
  958. opacity: 1;
  959. transform: translateX(0) translateY(0);
  960. }
  961. }
  962. .dialogue-header {
  963. position: absolute;
  964. top: rpx(-11);
  965. left: rpx(12);
  966. background: #409EFF;
  967. color: white;
  968. padding: rpx(1.2) rpx(6);
  969. border-radius: rpx(5);
  970. font-size: rpx(8);
  971. box-shadow: 0 rpx(2.5) rpx(10) rgba(0, 0, 0, 0.2);
  972. }
  973. .dialogue-card.right .dialogue-header {
  974. left: rpx(10);
  975. }
  976. .role-name {
  977. font-weight: 600;
  978. color: white;
  979. font-size: rpx(10);
  980. }
  981. .dialogue-content {
  982. font-size: rpx(12);
  983. line-height: 1.2;
  984. color: #333;
  985. text-align: left;
  986. display: flex;
  987. flex-direction: column;
  988. align-items: center;
  989. justify-content: center;
  990. width: 100%;
  991. }
  992. .user-input-card {
  993. max-width: 50%;
  994. position: absolute;
  995. left: 50%;
  996. transform: translateX(-50%);
  997. bottom: rpx(70);
  998. right: auto;
  999. animation: dialogueEnterCenter 0.6s ease forwards;
  1000. }
  1001. .user-input-card::before {
  1002. display: none;
  1003. }
  1004. @keyframes dialogueEnterCenter {
  1005. from {
  1006. opacity: 0;
  1007. transform: translateX(-50%) translateY(rpx(20));
  1008. }
  1009. to {
  1010. opacity: 1;
  1011. transform: translateX(-50%) translateY(0);
  1012. }
  1013. }
  1014. .user-input-textarea {
  1015. width: 95%;
  1016. min-height: rpx(50);
  1017. max-height: rpx(150);
  1018. border: none;
  1019. outline: none;
  1020. background: transparent;
  1021. font-size: rpx(10);
  1022. line-height: 1.2;
  1023. color: #333;
  1024. resize: none;
  1025. font-family: inherit;
  1026. overflow-y: auto;
  1027. text-align: left;
  1028. padding: rpx(5) 0;
  1029. }
  1030. .voice-input-content {
  1031. width: 95%;
  1032. min-height: rpx(50);
  1033. max-height: rpx(150);
  1034. font-size: rpx(10);
  1035. line-height: 1.2;
  1036. color: #333;
  1037. text-align: left;
  1038. padding: rpx(5) 0;
  1039. overflow-y: auto;
  1040. }
  1041. .voice-input-placeholder {
  1042. width: 95%;
  1043. min-height: rpx(50);
  1044. font-size: rpx(10);
  1045. line-height: 1.2;
  1046. color: #999;
  1047. text-align: left;
  1048. padding: rpx(5) 0;
  1049. font-style: italic;
  1050. }
  1051. /* 滚动条样式 */
  1052. .user-input-textarea::-webkit-scrollbar {
  1053. width: rpx(0);
  1054. }
  1055. .user-input-textarea::-webkit-scrollbar-track {
  1056. background: rgba(0, 0, 0, 0.05);
  1057. border-radius: rpx(3);
  1058. }
  1059. .user-input-textarea::-webkit-scrollbar-thumb {
  1060. background: rgba(64, 158, 255, 0.5);
  1061. border-radius: rpx(3);
  1062. }
  1063. .user-input-textarea::-webkit-scrollbar-thumb:hover {
  1064. background: rgba(64, 158, 255, 0.8);
  1065. }
  1066. .input-actions {
  1067. display: flex;
  1068. justify-content: flex-end;
  1069. gap: rpx(6);
  1070. margin-top: rpx(6);
  1071. width: 100%;
  1072. }
  1073. .cancel-btn, .submit-btn {
  1074. padding: rpx(2.5) rpx(10);
  1075. border: none;
  1076. border-radius: rpx(4);
  1077. font-size: rpx(8);
  1078. cursor: pointer;
  1079. transition: all 0.3s ease;
  1080. }
  1081. .cancel-btn {
  1082. background: #E0E0E0;
  1083. color: #666;
  1084. }
  1085. .submit-btn {
  1086. background: #409EFF;
  1087. color: white;
  1088. }
  1089. .cancel-btn:hover, .submit-btn:hover {
  1090. transform: scale(1.05);
  1091. }
  1092. .cancel-btn:active, .submit-btn:active {
  1093. transform: scale(0.95);
  1094. }
  1095. .dialogue-content :deep(p) {
  1096. margin: 0 0 rpx(4) 0;
  1097. }
  1098. .dialogue-content :deep(strong),
  1099. .dialogue-content :deep(b) {
  1100. font-weight: bold;
  1101. }
  1102. .dialogue-content :deep(em),
  1103. .dialogue-content :deep(i) {
  1104. font-style: italic;
  1105. }
  1106. .dialogue-content :deep(ul),
  1107. .dialogue-content :deep(ol) {
  1108. margin: rpx(4) 0;
  1109. padding-left: rpx(10);
  1110. }
  1111. .dialogue-content :deep(li) {
  1112. margin: rpx(2) 0;
  1113. }
  1114. .dialogue-content :deep(code) {
  1115. background-color: #f0f0f0;
  1116. padding: rpx(1) rpx(3);
  1117. border-radius: rpx(2);
  1118. font-family: monospace;
  1119. font-size: rpx(9);
  1120. }
  1121. .dialogue-content :deep(a) {
  1122. color: #409EFF;
  1123. text-decoration: underline;
  1124. }
  1125. .input-buttons-container {
  1126. position: relative;
  1127. display: flex;
  1128. align-items: center;
  1129. justify-content: center;
  1130. width: 100%;
  1131. z-index: 10;
  1132. transition: all 0.3s ease;
  1133. transform: scale(0.9);
  1134. }
  1135. .voice-input-outer,
  1136. .keyboard-input-outer {
  1137. position: absolute;
  1138. display: flex;
  1139. align-items: center;
  1140. justify-content: center;
  1141. transition: all 0.3s ease;
  1142. }
  1143. .voice-input-outer {
  1144. position: relative;
  1145. width: rpx(50);
  1146. height: rpx(50);
  1147. border-radius: 50%;
  1148. background: transparent;
  1149. display: flex;
  1150. align-items: center;
  1151. justify-content: center;
  1152. // 正常状态只显示一圈
  1153. &::before {
  1154. content: '';
  1155. position: absolute;
  1156. width: 85%;
  1157. height: 85%;
  1158. border-radius: 50%;
  1159. border: rpx(2) solid rgba(80, 190, 240, 0.6);
  1160. }
  1161. // 录音时显示波纹效果
  1162. &.recording {
  1163. &::before {
  1164. animation: pulse 2s infinite;
  1165. }
  1166. &::after {
  1167. content: '';
  1168. position: absolute;
  1169. width: 85%;
  1170. height: 85%;
  1171. border-radius: 50%;
  1172. border: rpx(2) solid rgba(80, 190, 240, 0.4);
  1173. animation: pulse 2s infinite 0.5s;
  1174. }
  1175. }
  1176. :deep(.voice-input-container) {
  1177. position: relative;
  1178. z-index: 10;
  1179. .speech-btn {
  1180. width: rpx(40);
  1181. height: rpx(40);
  1182. border-radius: 50%;
  1183. border: rpx(2) solid rgba(0, 100, 192);
  1184. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  1185. display: flex;
  1186. align-items: center;
  1187. justify-content: center;
  1188. padding: 0;
  1189. gap: 0;
  1190. transition: all 0.3s ease;
  1191. cursor: pointer;
  1192. position: relative;
  1193. overflow: hidden;
  1194. &:hover {
  1195. transform: scale(1.05);
  1196. box-shadow: 0 rpx(4) rpx(12) rgba(0, 0, 0, 0.3);
  1197. }
  1198. &:active {
  1199. transform: scale(0.95);
  1200. }
  1201. &::before {
  1202. content: '';
  1203. position: absolute;
  1204. top: 50%;
  1205. left: 50%;
  1206. width: 0;
  1207. height: 0;
  1208. border-radius: 50%;
  1209. background: rgba(255, 255, 255, 0.5);
  1210. transform: translate(-50%, -50%);
  1211. transition: width 0.6s, height 0.6s;
  1212. }
  1213. &:active::before {
  1214. width: rpx(80);
  1215. height: rpx(80);
  1216. }
  1217. .el-icon {
  1218. font-size: rpx(20);
  1219. color: #0064BE;
  1220. z-index: 1;
  1221. }
  1222. .countdown-text {
  1223. display: none;
  1224. }
  1225. .waveform-container {
  1226. display: none;
  1227. }
  1228. }
  1229. }
  1230. }
  1231. @keyframes pulse {
  1232. 0% {
  1233. transform: scale(1);
  1234. opacity: 1;
  1235. }
  1236. 100% {
  1237. transform: scale(1.15);
  1238. opacity: 0;
  1239. }
  1240. }
  1241. .keyboard-input-outer {
  1242. width: rpx(30);
  1243. height: rpx(30);
  1244. border-radius: 50%;
  1245. background: transparent;
  1246. &.active {
  1247. width: rpx(40);
  1248. height: rpx(40);
  1249. left: 50%;
  1250. transform: translateX(-50%);
  1251. z-index: 11;
  1252. .keyboard-btn {
  1253. width: rpx(36);
  1254. height: rpx(36);
  1255. border: rpx(2) solid rgba(0, 100, 192);
  1256. box-shadow: 0 rpx(3) rpx(8) rgba(0, 0, 0, 0.3);
  1257. .keyboard-icon {
  1258. font-size: rpx(18);
  1259. }
  1260. }
  1261. }
  1262. &.inactive {
  1263. width: rpx(24);
  1264. height: rpx(24);
  1265. left: calc(50% + rpx(40));
  1266. transform: translateX(-50%);
  1267. z-index: 10;
  1268. .keyboard-btn {
  1269. width: rpx(22);
  1270. height: rpx(22);
  1271. border: rpx(1) solid rgba(128, 128, 128, 0.5);
  1272. box-shadow: none;
  1273. background: linear-gradient(135deg, #E0E0E0, #C0C0C0);
  1274. .keyboard-icon {
  1275. font-size: rpx(12);
  1276. color: #808080;
  1277. }
  1278. }
  1279. }
  1280. }
  1281. .keyboard-btn {
  1282. width: rpx(28);
  1283. height: rpx(28);
  1284. border-radius: 50%;
  1285. border: rpx(1) solid rgba(0, 100, 192);
  1286. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  1287. display: flex;
  1288. align-items: center;
  1289. justify-content: center;
  1290. padding: 0;
  1291. gap: 0;
  1292. transition: all 0.3s ease;
  1293. cursor: pointer;
  1294. &:hover {
  1295. transform: scale(1.1);
  1296. box-shadow: 0 rpx(2) rpx(8) rgba(0, 0, 0, 0.3);
  1297. }
  1298. &:active {
  1299. transform: scale(0.95);
  1300. }
  1301. .keyboard-icon {
  1302. font-size: rpx(14);
  1303. color: #0064BE;
  1304. }
  1305. }
  1306. </style>