DialogContent.vue 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  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="box-icon" @click="togglePlay">
  29. <span class="play-text">{{ isPlaying ? '暂停' : '自动' }}</span>
  30. </div>
  31. </div>
  32. </div>
  33. <!-- 内容区域 -->
  34. <div class="content-box">
  35. <!-- 人物形象 -->
  36. <div
  37. v-if="currentDialogue && (currentDialogue.type === 'digital' || currentDialogue.type === 'quest')"
  38. :key="`character-${currentDialogueIndex}`"
  39. class="character"
  40. :class="{
  41. 'left': getCharacterSide(currentDialogue.roleName) === 'left',
  42. 'right': getCharacterSide(currentDialogue.roleName) === 'right'
  43. }"
  44. :style="{ backgroundImage: `url(${getCharacterImage(currentDialogue.roleName)})` }"
  45. ></div>
  46. <!-- 对话卡片 -->
  47. <div
  48. v-if="currentDialogue && (currentDialogue.type === 'digital' || currentDialogue.type === 'quest')"
  49. :key="`dialogue-${currentDialogueIndex}`"
  50. class="dialogue-card"
  51. :class="{
  52. 'left': getCharacterSide(currentDialogue.roleName) === 'left',
  53. 'right': getCharacterSide(currentDialogue.roleName) === 'right'
  54. }"
  55. >
  56. <div class="dialogue-header">
  57. <span class="role-name">{{ currentDialogue?.roleName }}</span>
  58. </div>
  59. <div class="dialogue-content" v-html="parseMarkdown(currentDialogue.content)"></div>
  60. </div>
  61. <!-- 诗词显示区域 -->
  62. <div v-if="showPoem" class="poem-display">
  63. <div class="poem-content">
  64. <div class="poem-text" v-html="formatPoemContent(currentPoemContent)" ></div>
  65. </div>
  66. </div>
  67. <!-- 视频显示区域 -->
  68. <div v-if="currentDialogue && currentDialogue.type === 'video'" class="video-display">
  69. <div class="video-frame">
  70. <video
  71. :src="currentDialogue.videoUrl"
  72. class="dialogue-video"
  73. controls
  74. autoplay
  75. @ended="handleVideoEnded"
  76. @contextmenu.prevent
  77. controlslist="nodownload"
  78. >
  79. 您的浏览器不支持视频播放
  80. </video>
  81. </div>
  82. </div>
  83. <!-- 用户输入卡片 -->
  84. <div
  85. v-if="currentDialogue.type === 'user'"
  86. class="dialogue-card user-input-card"
  87. >
  88. <div class="dialogue-header">
  89. <span class="role-name">我</span>
  90. </div>
  91. <div class="dialogue-content">
  92. <textarea
  93. :value="userInput"
  94. @input="e => userInput = e.target.value"
  95. class="user-input-textarea"
  96. placeholder="请输入内容..."
  97. @keyup.enter.exact="submitUserInput"
  98. ></textarea>
  99. <div class="input-actions">
  100. <button class="cancel-btn" @click="cancelUserInput">清空</button>
  101. <button class="submit-btn" @click="submitUserInput">发送</button>
  102. </div>
  103. </div>
  104. </div>
  105. <!-- 输入按钮区域 -->
  106. <div class="input-buttons-container" >
  107. <!-- 上一个对话按钮 -->
  108. <div class="arrow-icon-circle" @click="playPrevious" :class="{ 'disabled': currentSectionIndex === 0 && currentDialogueIndex === 0 }">
  109. <el-icon class="arrow-icon"><CaretLeft /></el-icon>
  110. </div>
  111. <!-- 语音输入按钮 -->
  112. <div class="voice-input-outer" v-if="currentDialogue.type === 'user'" :class="{ 'recording': isVoiceRecording }">
  113. <VoiceInput
  114. inputSelector=".user-input-textarea"
  115. lang="zh-CN"
  116. maxDuration="10"
  117. @voiceRecognized="handleVoiceRecognized"
  118. @recordingStatusChanged="handleRecordingStatusChanged"
  119. />
  120. </div>
  121. <!-- 语音输入按钮占位符 -->
  122. <div class="voice-input-outer placeholder" v-else></div>
  123. <!-- 下一个对话按钮 -->
  124. <div class="arrow-icon-circle" @click="playNext" :class="{ 'disabled': currentSectionIndex === scriptData.sections.length - 1 && currentDialogueIndex === currentSection.dialogues.length - 1 }">
  125. <el-icon class="arrow-icon"><CaretRight /></el-icon>
  126. </div>
  127. </div>
  128. </div>
  129. <!-- 背景图 -->
  130. <img
  131. v-if="currentBackgroundType === 'imageAudio'"
  132. :src="currentBackgroundImage"
  133. alt="背景图"
  134. class="background-image"
  135. >
  136. <!-- 背景视频 -->
  137. <video
  138. v-else-if="currentBackgroundType === 'video'"
  139. ref="backgroundVideoRef"
  140. :src="currentBackgroundVideo"
  141. class="background-video"
  142. loop
  143. muted
  144. playsinline>
  145. 您的浏览器不支持视频播放
  146. </video>
  147. </div>
  148. </template>
  149. <script setup>
  150. import { ref, computed, onMounted, onUnmounted, watch } from 'vue'
  151. import { useRouter } from 'vue-router'
  152. import { ArrowLeftBold, CaretLeft, CaretRight, Grid, VideoPlay } from '@element-plus/icons-vue'
  153. import VoiceInput from '@/components/ai/voice/VoiceInput_Api.vue'
  154. import { marked } from 'marked'
  155. import {CreateDialogue, sendChatMessageStream} from "@/api/questions.js";
  156. import {useAudioPlayer} from "@/api/tts/useAudioPlayer.js";
  157. const { playAudioChunk , stopPlayback, setOnPlaybackComplete, getIsPlaying } = useAudioPlayer();
  158. // 路由实例
  159. const router = useRouter()
  160. const props = defineProps({
  161. scriptData: {
  162. type: Object,
  163. default: () => ({
  164. title: "",
  165. sections: []
  166. })
  167. },
  168. scriptRoles: {
  169. type: Array,
  170. default: () => []
  171. },
  172. backText: {
  173. type: String,
  174. default: '返回课程'
  175. },
  176. // 是否是最后一节课
  177. isLastCourse: {
  178. type: Boolean,
  179. default: false
  180. }
  181. })
  182. const emit = defineEmits(['dialogueEnded'])
  183. // 对话相关状态
  184. // 当前章节索引
  185. const currentSectionIndex = ref(0)
  186. // 当前对话索引
  187. const currentDialogueIndex = ref(0)
  188. // 是否正在播放
  189. const isPlaying = ref(false)
  190. // 用户输入内容
  191. const userInput = ref('')
  192. // 语音录音状态
  193. const isVoiceRecording = ref(false)
  194. // 实时语音识别结果
  195. const voiceRecognizedText = ref("")
  196. // 诗词显示状态
  197. const showPoem = ref(false)
  198. // 当前诗词内容
  199. const currentPoemContent = ref('')
  200. // 音频对象
  201. // 背景音频
  202. const backgroundAudio = ref(null)
  203. // 对话音频
  204. const dialogueAudio = ref(null)
  205. // 背景视频
  206. const backgroundVideoRef = ref(null)
  207. // 遮罩层显示状态
  208. const showMask = ref(true)
  209. // 是否开始播放
  210. const isPlaybackStarted = ref(false)
  211. // 计算属性
  212. // 当前章节信息
  213. const currentSection = computed(() => {
  214. return props.scriptData.sections[currentSectionIndex.value]
  215. })
  216. // 当前对话信息
  217. const currentDialogue = computed(() => {
  218. if (!currentSection.value) return null
  219. return currentSection.value.dialogues[currentDialogueIndex.value]
  220. })
  221. const currentBackgroundImage = computed(() => {
  222. if (!currentSection.value || !currentSection.value.backgroundImage || !currentSection.value.backgroundImage.url) {
  223. return ''
  224. }
  225. return currentSection.value.backgroundImage.url
  226. })
  227. // 当前背景类型
  228. const currentBackgroundType = computed(() => {
  229. return currentSection.value?.backgroundType || 'imageAudio'
  230. })
  231. // 当前背景视频
  232. const currentBackgroundVideo = computed(() => {
  233. if (!currentSection.value || !currentSection.value.backgroundVideo || !currentSection.value.backgroundVideo.url) {
  234. return ''
  235. }
  236. return currentSection.value.backgroundVideo.url
  237. })
  238. // 当前对话缓存
  239. const currentDialogueCache = ref(null)
  240. // 方法
  241. const handleVoiceRecognized = (data) => {
  242. console.log('语音识别结果:', data.originalText)
  243. if (isVoiceRecording.value) {
  244. // 在同一次录音过程中,实时更新文本框内容
  245. voiceRecognizedText.value = data.originalText
  246. userInput.value = data.processedText
  247. } else {
  248. // 在录音结束时,将最终的语音内容追加到userInput.value
  249. const textarea = document.querySelector('.user-input-textarea')
  250. if (textarea) {
  251. userInput.value = data.processedText
  252. // 重新设置光标位置到插入文本的末尾
  253. setTimeout(() => {
  254. textarea.selectionStart = textarea.selectionEnd = data.cursorPos
  255. }, 0)
  256. } else {
  257. // 如果没有找到输入框,直接替换整个内容
  258. userInput.value = data.originalText
  259. }
  260. // 清空临时变量
  261. voiceRecognizedText.value = ""
  262. }
  263. }
  264. // 处理录音状态变化
  265. const handleRecordingStatusChanged = (isRecording) => {
  266. console.log('录音状态:', isRecording)
  267. const wasRecording = isVoiceRecording.value
  268. isVoiceRecording.value = isRecording
  269. // 如果是从录音状态切换到非录音状态,只需要清空临时变量
  270. if (wasRecording && !isRecording) {
  271. // 清空临时变量
  272. voiceRecognizedText.value = ""
  273. }
  274. }
  275. // 提交用户输入
  276. const submitUserInput = async () => {
  277. if (userInput.value.trim()) {
  278. console.log('用户输入:', userInput.value)
  279. await createAiChart();
  280. await doSendMessage();
  281. }
  282. }
  283. // 取消用户输入
  284. const cancelUserInput = () => {
  285. userInput.value = ''
  286. }
  287. // 解析 Markdown 内容
  288. const parseMarkdown = (content) => {
  289. if (!content) return ''
  290. return marked(content)
  291. }
  292. // 格式化诗词内容,在逗号和句号后添加换行
  293. const formatPoemContent = (content) => {
  294. if (!content) return ''
  295. const plainText = content.replace(/[\*#`\[\]\(\)]/g, '')
  296. return plainText.replace(/([。!?;、])/g, '$1<br/>')
  297. }
  298. const getCharacterSide = () => {
  299. return currentDialogueIndex.value % 2 === 0 ? 'left' : 'right'
  300. }
  301. // 根据角色ID获取角色名称
  302. const getRole = (roleName) => {
  303. return props.scriptRoles.find(r => r.name === roleName)
  304. }
  305. const getCharacterImage = (roleName) => {
  306. const role = getRole(roleName)
  307. return role ? role.avatar : ''
  308. }
  309. const stopAllAudio = () => {
  310. if (backgroundAudio.value) {
  311. backgroundAudio.value.pause()
  312. backgroundAudio.value.currentTime = 0
  313. }
  314. if (dialogueAudio.value) {
  315. dialogueAudio.value.pause()
  316. dialogueAudio.value.currentTime = 0
  317. }
  318. }
  319. const playBackgroundAudio = () => {
  320. // 停止之前的背景音
  321. if (backgroundAudio.value) {
  322. backgroundAudio.value.pause()
  323. backgroundAudio.value.currentTime = 0
  324. }
  325. // 只有当背景类型为 imageAudio 时才播放背景音频
  326. if (currentBackgroundType.value === 'imageAudio' && currentSection.value?.backgroundAudio?.url && isPlaying.value) {
  327. backgroundAudio.value = new Audio(currentSection.value.backgroundAudio.url)
  328. backgroundAudio.value.loop = true
  329. backgroundAudio.value.volume = 1
  330. backgroundAudio.value.play().catch(e => console.error('背景音播放失败:', e))
  331. }
  332. // 处理背景视频
  333. if (currentBackgroundType.value === 'video' && isPlaybackStarted.value) {
  334. // 视频已经在模板中渲染,这里只需要确保它在播放状态
  335. if (backgroundVideoRef.value) {
  336. backgroundVideoRef.value.play().catch(e => console.error('背景视频播放失败:', e))
  337. }
  338. }
  339. }
  340. const playDialogueAudio = (isAutoPlay = false) => {
  341. // 停止之前的对话语音
  342. if (dialogueAudio.value) {
  343. dialogueAudio.value.pause()
  344. dialogueAudio.value.currentTime = 0
  345. }
  346. // 播放当前对话的语音
  347. if (currentDialogue.value?.voiceoverUrl) {
  348. const audio = new Audio(currentDialogue.value.voiceoverUrl)
  349. dialogueAudio.value = audio
  350. // 音频结束事件
  351. audio.onended = () => {
  352. // 检查是否是最后一个对话
  353. if (isAtLastDialogue()) {
  354. // 如果是用户输入类型,不立即触发dialogueEnded,等待AI回答完成
  355. if (currentDialogue.value?.type === 'user') {
  356. console.log('用户输入类型,等待AI回答完成后再提示');
  357. return;
  358. }
  359. // 语音播报完成且是最后一个对话,触发事件通知父组件
  360. console.log('普通对话语音播放完成,已到达最后一个对话,触发 dialogueEnded 事件');
  361. emit('dialogueEnded', props.isLastCourse);
  362. isPlaying.value = false;
  363. return;
  364. }
  365. // 如果是自动播放状态,继续播放下一条
  366. if (isAutoPlay && isPlaying.value) {
  367. setTimeout(() => {
  368. if (!playNext(true)) {
  369. // 播放完毕
  370. isPlaying.value = false
  371. stopAllAudio()
  372. }
  373. }, 100)
  374. }
  375. }
  376. // 播放音频
  377. audio.play().catch(e => {
  378. console.error('对话语音播放失败:', e)
  379. // 播放失败时,检查是否是最后一个对话
  380. if (isAtLastDialogue()) {
  381. // 如果是用户输入类型,不立即触发dialogueEnded,等待AI回答完成
  382. if (currentDialogue.value?.type === 'user') {
  383. console.log('用户输入类型,等待AI回答完成后再提示');
  384. return;
  385. }
  386. emit('dialogueEnded', props.isLastCourse);
  387. isPlaying.value = false;
  388. return;
  389. }
  390. // 播放失败时,2秒后跳转
  391. if (isAutoPlay && isPlaying.value) {
  392. setTimeout(() => {
  393. if (!playNext(true)) {
  394. // 播放完毕
  395. isPlaying.value = false
  396. stopAllAudio()
  397. }
  398. }, 2000)
  399. }
  400. })
  401. } else {
  402. // 检查是否是最后一个对话
  403. if (isAtLastDialogue()) {
  404. // 如果是用户输入类型,不立即触发dialogueEnded,等待AI回答完成
  405. if (currentDialogue.value?.type === 'user') {
  406. console.log('用户输入类型,等待AI回答完成后再提示');
  407. return;
  408. }
  409. emit('dialogueEnded', props.isLastCourse);
  410. isPlaying.value = false;
  411. return;
  412. }
  413. // 如果没有语音文件,2秒后跳转
  414. if (isAutoPlay && isPlaying.value && currentDialogue.value?.type !== 'user') {
  415. setTimeout(() => {
  416. if (!playNext(true)) {
  417. // 播放完毕
  418. isPlaying.value = false
  419. stopAllAudio()
  420. }
  421. }, 2000)
  422. }
  423. }
  424. }
  425. const togglePlay = () => {
  426. isPlaying.value = !isPlaying.value
  427. if (isPlaying.value) {
  428. // 播放背景音
  429. playBackgroundAudio()
  430. if(!getIsPlaying() && !conversationInProgress.value){
  431. // 开始播放序列
  432. playSequence()
  433. }
  434. } else {
  435. // 暂停所有音频
  436. stopAllAudio()
  437. }
  438. }
  439. // 处理视频结束事件
  440. const handleVideoEnded = () => {
  441. // 视频播放完毕后,继续播放下一条对话
  442. if (isPlaying.value) {
  443. if (!playNext(true)) {
  444. // 播放完毕,检查是否是最后一个对话
  445. if (isAtLastDialogue()) {
  446. console.log('视频序列:已到达最后一个对话,触发 dialogueEnded 事件');
  447. emit('dialogueEnded', props.isLastCourse);
  448. }
  449. isPlaying.value = false
  450. stopAllAudio()
  451. }
  452. }
  453. }
  454. // 自动播放序列
  455. const playSequence = () => {
  456. if (!isPlaying.value) return
  457. // 如果当前是用户输入卡片,暂停播放等待用户输入
  458. if (currentDialogue.value?.type === 'user') return
  459. // 检查当前对话是否为诗词类型
  460. if (currentDialogue.value?.type === 'poem') {
  461. // 显示诗词并替换内容为最新的诗词
  462. showPoem.value = true
  463. currentPoemContent.value = currentDialogue.value.content
  464. // 检查是否有语音
  465. if (currentDialogue.value?.voiceoverUrl) {
  466. // 播放诗词语音
  467. playDialogueAudio(true)
  468. } else {
  469. // 没有语音,直接切换到下一条对话
  470. setTimeout(() => {
  471. if (!playNext(true)) {
  472. // 播放完毕,检查是否是最后一个对话
  473. if (isAtLastDialogue()) {
  474. console.log('诗词序列:已到达最后一个对话,触发 dialogueEnded 事件');
  475. emit('dialogueEnded', props.isLastCourse);
  476. }
  477. // 播放完毕
  478. isPlaying.value = false
  479. stopAllAudio()
  480. }
  481. }, 500)
  482. }
  483. } else {
  484. // 播放当前对话语音,传递isAutoPlay参数
  485. playDialogueAudio(true)
  486. }
  487. }
  488. const playPrevious = () => {
  489. // 如果已经到达第一句,直接返回,不执行任何操作
  490. if (currentDialogueIndex.value === 0 && currentSectionIndex.value === 0) {
  491. return
  492. }
  493. // 停止当前音频
  494. stopAllAudio()
  495. // 如果正在进行数字人对话,调用stopStream清理
  496. recoverQuestDialogue()
  497. stopPlayback(false) // 不触发回调
  498. if (conversationInProgress.value) {
  499. stopStream()
  500. }
  501. if (currentDialogueIndex.value > 0) {
  502. currentDialogueIndex.value--
  503. } else if (currentSectionIndex.value > 0) {
  504. currentSectionIndex.value--
  505. const section = props.scriptData.sections[currentSectionIndex.value]
  506. currentDialogueIndex.value = section.dialogues.length - 1
  507. // 切换环节时隐藏诗词
  508. showPoem.value = false
  509. currentPoemContent.value = ''
  510. }
  511. // 检查当前对话是否为诗词类型
  512. if (currentDialogue.value?.type === 'poem') {
  513. // 显示诗词并替换内容为最新的诗词
  514. showPoem.value = true
  515. currentPoemContent.value = currentDialogue.value.content
  516. } else if (currentDialogue.value?.type === 'video') {
  517. // 视频类型对话,隐藏诗词
  518. showPoem.value = false
  519. currentPoemContent.value = ""
  520. } else {
  521. showPoem.value = false
  522. currentPoemContent.value = ""
  523. //读取上一条诗词内容显示
  524. for (let i = currentDialogueIndex.value; i >= 0; i--) {
  525. let dialogueTemp = currentSection.value.dialogues[i];
  526. if (dialogueTemp.type === 'poem'){
  527. // 显示诗词并替换内容为最新的诗词
  528. showPoem.value = true
  529. currentPoemContent.value = dialogueTemp.content
  530. }
  531. }
  532. }
  533. // 播放背景音
  534. playBackgroundAudio()
  535. // 播放当前对话语音(非诗词类型)
  536. if (currentDialogue.value?.type !== 'poem') {
  537. if (isPlaying.value) {
  538. playDialogueAudio(true)
  539. } else {
  540. playDialogueAudio()
  541. }
  542. }
  543. }
  544. const playNext = (isAutoPlay = false) => {
  545. // 如果已经到达最后一句,直接返回,不执行任何操作
  546. if (currentSection.value && currentDialogueIndex.value === currentSection.value.dialogues.length - 1 && currentSectionIndex.value === props.scriptData.sections.length - 1) {
  547. return false
  548. }
  549. // 停止当前对话语音
  550. if (dialogueAudio.value) {
  551. dialogueAudio.value.pause()
  552. dialogueAudio.value.currentTime = 0
  553. }
  554. // 如果正在进行数字人对话,调用stopStream清理
  555. recoverQuestDialogue()
  556. stopPlayback(false)
  557. if (conversationInProgress.value) {
  558. stopStream()
  559. }
  560. if (currentSection.value && currentDialogueIndex.value < currentSection.value.dialogues.length - 1) {
  561. currentDialogueIndex.value++
  562. // 检查当前对话是否为诗词类型
  563. if (currentDialogue.value?.type === 'poem') {
  564. // 显示诗词并替换内容为最新的诗词
  565. showPoem.value = true
  566. currentPoemContent.value = currentDialogue.value.content
  567. // 检查是否有语音
  568. if (currentDialogue.value?.voiceoverUrl) {
  569. // 播放诗词语音
  570. if (isPlaying.value) {
  571. playDialogueAudio(true)
  572. } else {
  573. playDialogueAudio()
  574. }
  575. } else {
  576. // 没有语音,直接切换到下一条对话
  577. setTimeout(() => {
  578. playNext(isAutoPlay)
  579. }, 500)
  580. }
  581. return true
  582. } else if (currentDialogue.value?.type === 'video') {
  583. // 视频类型对话,隐藏诗词,不自动播放下一条
  584. showPoem.value = false
  585. currentPoemContent.value = ''
  586. return true
  587. }
  588. // 根据是否为自动播放状态决定如何播放语音
  589. if (isPlaying.value) {
  590. playDialogueAudio(true)
  591. } else {
  592. playDialogueAudio()
  593. }
  594. return true
  595. } else if (currentSectionIndex.value < props.scriptData.sections.length - 1) {
  596. currentSectionIndex.value++
  597. currentDialogueIndex.value = 0
  598. // 切换环节时隐藏诗词
  599. showPoem.value = false
  600. currentPoemContent.value = ''
  601. // 播放背景音
  602. playBackgroundAudio()
  603. // 检查新环节的第一个对话是否为诗词类型
  604. if (currentDialogue.value?.type === 'poem') {
  605. // 显示诗词
  606. showPoem.value = true
  607. currentPoemContent.value = currentDialogue.value.content
  608. // 检查是否有语音
  609. if (currentDialogue.value?.voiceoverUrl) {
  610. // 播放诗词语音
  611. if (isPlaying.value) {
  612. playDialogueAudio(true)
  613. } else {
  614. playDialogueAudio()
  615. }
  616. } else {
  617. // 没有语音,直接切换到下一条对话
  618. setTimeout(() => {
  619. playNext(isAutoPlay)
  620. }, 500)
  621. }
  622. } else if (currentDialogue.value?.type === 'video') {
  623. // 视频类型对话,隐藏诗词
  624. showPoem.value = false
  625. currentPoemContent.value = ''
  626. } else {
  627. // 根据是否为自动播放状态决定如何播放语音
  628. if (isPlaying.value) {
  629. playDialogueAudio(true)
  630. } else {
  631. playDialogueAudio()
  632. }
  633. }
  634. return true
  635. }
  636. return false
  637. }
  638. // 返回主页按钮点击事件
  639. const goBackToMain = () => {
  640. // 停止所有音频
  641. stopAllAudio()
  642. // 跳转到 ai-general-course 页面,保持侧边栏选中状态
  643. router.push('/ai-general-course')
  644. }
  645. // 开始播放
  646. const maskLayer = ref(null)
  647. const startPlayback = () => {
  648. // 设置开始播放状态
  649. isPlaybackStarted.value = true
  650. // 开始播放背景视频
  651. if (backgroundVideoRef.value) {
  652. backgroundVideoRef.value.play().catch(e => console.error('背景视频播放失败:', e))
  653. }
  654. // 消失动画
  655. if (maskLayer.value) {
  656. maskLayer.value.classList.add('fade-out')
  657. // 等待动画完成后隐藏遮罩层
  658. setTimeout(() => {
  659. showMask.value = false
  660. }, 500)
  661. }
  662. // 检查当前对话是否为诗词类型
  663. if (currentDialogue.value?.type === 'poem') {
  664. // 显示诗词
  665. showPoem.value = true
  666. currentPoemContent.value = currentDialogue.value.content
  667. // 检查是否有语音
  668. if (currentDialogue.value?.voiceoverUrl) {
  669. // 播放诗词语音
  670. playDialogueAudio()
  671. } else {
  672. // 没有语音,直接切换到下一条对话
  673. setTimeout(() => {
  674. playNext()
  675. }, 500)
  676. }
  677. } else if (currentDialogue.value?.type === 'video') {
  678. // 视频类型对话,隐藏诗词
  679. showPoem.value = false
  680. currentPoemContent.value = ''
  681. } else {
  682. // 播放当前对话语音
  683. playDialogueAudio()
  684. }
  685. }
  686. // 键盘事件处理,键盘左右箭头控制对话
  687. const handleKeydown = (event) => {
  688. // 如果遮罩层显示,不处理键盘事件
  689. if (showMask.value) {
  690. return
  691. }
  692. // 如果当前是用户输入对话,不处理键盘事件,让默认行为生效(在输入框中左右移动光标)
  693. if (currentDialogue.value?.type === 'user') {
  694. return
  695. }
  696. // 处理左右箭头键
  697. switch (event.key) {
  698. case 'ArrowLeft':
  699. playPrevious()
  700. event.preventDefault() // 防止默认行为
  701. break
  702. case 'ArrowRight':
  703. playNext()
  704. event.preventDefault() // 防止默认行为
  705. break
  706. default:
  707. // 其他按键不做处理
  708. break
  709. }
  710. }
  711. // 监听环节变化
  712. watch(currentSectionIndex, () => {
  713. playBackgroundAudio()
  714. })
  715. // 监听 scriptData 变化(侧边栏切换话题时)
  716. watch(() => props.scriptData, (newVal, oldVal) => {
  717. if (newVal && oldVal && newVal !== oldVal) {
  718. // 停止所有音频
  719. stopAllAudio()
  720. // 如果正在进行数字人对话,调用stopStream清理
  721. recoverQuestDialogue()
  722. stopPlayback(false)
  723. if (conversationInProgress.value) {
  724. stopStream()
  725. }
  726. // 清空索引
  727. currentSectionIndex.value = 0
  728. currentDialogueIndex.value = 0
  729. // 重置播放状态
  730. isPlaying.value = false
  731. // 显示遮罩层
  732. showMask.value = true
  733. // 清空用户输入
  734. userInput.value = ''
  735. // 清空对话缓存
  736. currentDialogueCache.value = null
  737. // 隐藏诗词
  738. showPoem.value = false
  739. currentPoemContent.value = ''
  740. }
  741. }, { deep: true })
  742. // 会话ID
  743. const activeConversationId = ref(null)
  744. const conversationInProgress = ref(false)
  745. const conversationInAbortController = ref()
  746. const receiveMessageFullText = ref('')
  747. //创建对话
  748. const createAiChart = async () => {
  749. let role = props.scriptRoles.find(r => r.name === currentDialogue.value.roleName)
  750. // 智能问答
  751. await CreateDialogue({ roleId: role.id })
  752. .then(res => {
  753. console.log("创建会话:", res.data);
  754. activeConversationId.value = res.data
  755. })
  756. .catch(error => {
  757. console.error('请求出错:', error)
  758. })
  759. }
  760. /** 真正执行【发送】消息操作 */
  761. const doSendMessage = async () => {
  762. // 校验
  763. if (userInput.value.length < 1) {
  764. console.error('发送失败,原因:内容为空!')
  765. return
  766. }
  767. if (activeConversationId.value == null) {
  768. console.error('还没创建对话,不能发送!')
  769. return
  770. }
  771. let userInputTemp = userInput.value;
  772. let currentDialogueTemp = currentSection.value.dialogues[currentDialogueIndex.value-1]
  773. userInputTemp += "(此内容是帮我解答的问题,问题是:" + currentDialogueTemp.content + ",回复要求:根据问题回复我回答的内容是否正确,并给予鼓励或夸赞;注意请使用精简回答,尽量控制字体数量在50个字内)"
  774. // 执行发送
  775. await doSendMessageStream({
  776. conversationId: activeConversationId.value,
  777. content: userInputTemp,
  778. contentAnswer: null,
  779. })
  780. // 清空输入框
  781. userInput.value = ''
  782. }
  783. /** 显示问题回答对话 */
  784. const showQuestAnswerDialogue = () => {
  785. // 缓存当前对话
  786. currentDialogueCache.value = JSON.parse(JSON.stringify(currentDialogue.value))
  787. // 将当前对话类型设置为数字人对话
  788. currentDialogue.value.type = "digital"
  789. // 设置默认内容为"让我思考一下..."
  790. currentDialogue.value.content = "让我思考一下..."
  791. }
  792. //延时恢复对话,避免立即回复导致对话内容被覆盖
  793. const delayRecoverQuestDialogue = () => {
  794. // Message().error('当前网络无反应,请稍后重试!', true);
  795. // 设置默认内容为"让我思考一下..."
  796. currentDialogue.value.content = "当前网络无反应,请稍后重试!"
  797. setTimeout(() => {
  798. recoverQuestDialogue()
  799. }, 1500)
  800. }
  801. /** 回复对话 */
  802. const recoverQuestDialogue = () => {
  803. // 如果有缓存的对话
  804. if (currentDialogueCache.value){
  805. // 恢复当前对话
  806. const currentSection = props.scriptData.sections[currentSectionIndex.value]
  807. if (currentSection) {
  808. currentSection.dialogues[currentDialogueIndex.value] = JSON.parse(JSON.stringify(currentDialogueCache.value))
  809. }
  810. // 清空缓存
  811. currentDialogueCache.value = null
  812. }
  813. }
  814. /** 真正执行【发送】消息操作 */
  815. const doSendMessageStream = async userMessage => {
  816. // 创建 AbortController 实例,以便中止请求
  817. conversationInAbortController.value = new AbortController()
  818. // 标记对话进行中
  819. conversationInProgress.value = true
  820. // 设置为空
  821. receiveMessageFullText.value = ''
  822. showQuestAnswerDialogue()
  823. try {
  824. // 发送 event stream
  825. let isFirstChunk = true // 是否是第一个 chunk 消息段
  826. await sendChatMessageStream(
  827. userMessage.conversationId,
  828. userMessage.content,
  829. userMessage.contentAnswer,
  830. conversationInAbortController.value,
  831. true, // enableContext 参数
  832. async res => {
  833. const { code, data, msg } = JSON.parse(res.data)
  834. if (code !== 0) {
  835. console.log(`对话异常! ${msg}`)
  836. stopStream();
  837. delayRecoverQuestDialogue()
  838. return
  839. }
  840. if (data.eventType === 'TEXT') {
  841. // 如果内容为空,就不处理。
  842. if (data.receive?.content === '') {
  843. return
  844. }
  845. receiveMessageFullText.value += data.receive.content
  846. // 更新数字人对话框内容
  847. currentDialogue.value.content = receiveMessageFullText.value
  848. // 首次返回需要添加一个 message 到页面,后面的都是更新
  849. if (isFirstChunk) {
  850. isFirstChunk = false
  851. //第一次返回
  852. } else {
  853. //更新最后一条消息
  854. }
  855. }
  856. if (data.eventType === 'AUDIO') {
  857. // 处理音频消息
  858. await playAudioChunk(data.audioData);
  859. }
  860. },
  861. error => {
  862. console.log(`对话异常! ${error}`)
  863. stopStream()
  864. delayRecoverQuestDialogue()
  865. // 需要抛出异常,禁止重试
  866. throw error
  867. },
  868. () => {
  869. console.log(`结束对话! `)
  870. stopStream()
  871. // AI回答完成,检查是否是最后一个对话且是用户输入类型
  872. if (isAtLastDialogue() && currentDialogue.value?.type === 'user') {
  873. console.log('AI回答完成,触发 dialogueEnded 事件');
  874. emit('dialogueEnded', props.isLastCourse);
  875. isPlaying.value = false;
  876. }
  877. }
  878. )
  879. } catch (error) {
  880. console.error('发送消息失败:', error)
  881. stopStream()
  882. delayRecoverQuestDialogue()
  883. }
  884. }
  885. /** 停止 stream 流式调用 */
  886. const stopStream = async () => {
  887. // 如果 stream 进行中的 message,就需要调用 controller 结束
  888. if (conversationInAbortController.value) {
  889. conversationInAbortController.value.abort()
  890. }
  891. // 销毁语音读取
  892. // stopPlayback();
  893. // 设置为 false
  894. conversationInProgress.value = false
  895. console.log(`结束对话!更改状态: `,conversationInProgress.value)
  896. }
  897. // 处理音频播放完成
  898. const handleAudioPlaybackComplete = () => {
  899. console.log('智能问答音频播放完成');
  900. // 先清除回调,防止 playNext 内部调用 stopPlayback(false) 时
  901. // 关闭 audioContext 触发 source.onended → processAudioQueue → 再次触发回调,造成二次跳转
  902. setOnPlaybackComplete(null);
  903. stopAllAudio();
  904. // 检查是否是最后一个对话
  905. if (isAtLastDialogue()) {
  906. // 语音播报完成且是最后一个对话,触发事件通知父组件
  907. console.log('已到达最后一个对话,触发 dialogueEnded 事件');
  908. emit('dialogueEnded', props.isLastCourse);
  909. isPlaying.value = false;
  910. return;
  911. }
  912. // 如果处于自动播放状态,继续播放下一条对话
  913. if (isPlaying.value) {
  914. // 恢复回调,供下一条 AI 对话使用
  915. setOnPlaybackComplete(handleAudioPlaybackComplete);
  916. if (playNext(true)) {
  917. // playNext 内部已调用 playDialogueAudio(true),无需再调 playSequence
  918. } else {
  919. // 播放完毕
  920. isPlaying.value = false;
  921. stopAllAudio();
  922. }
  923. }
  924. };
  925. // 判断是否是最后一个对话
  926. const isAtLastDialogue = () => {
  927. if (!props.scriptData.sections || props.scriptData.sections.length === 0) {
  928. return false
  929. }
  930. const lastSectionIndex = props.scriptData.sections.length - 1
  931. const lastSection = props.scriptData.sections[lastSectionIndex]
  932. if (!lastSection.dialogues || lastSection.dialogues.length === 0) {
  933. return false
  934. }
  935. const lastDialogueIndex = lastSection.dialogues.length - 1
  936. return currentSectionIndex.value === lastSectionIndex &&
  937. currentDialogueIndex.value === lastDialogueIndex
  938. }
  939. // 组件挂载时添加键盘事件监听
  940. onMounted(() => {
  941. window.addEventListener('keydown', handleKeydown)
  942. // 播放背景音
  943. playBackgroundAudio()
  944. // // 播放当前对话语音
  945. // playDialogueAudio()
  946. // 设置音频播放完成回调
  947. setOnPlaybackComplete(handleAudioPlaybackComplete)
  948. })
  949. // 组件卸载时移除键盘事件监听和停止音频
  950. onUnmounted(() => {
  951. window.removeEventListener('keydown', handleKeydown)
  952. stopAllAudio()
  953. stopPlayback(false)
  954. })
  955. </script>
  956. <style scoped lang="scss">
  957. @use "sass:math";
  958. @function rpx($px) {
  959. @return math.div($px, 750) * 100vw;
  960. }
  961. .dialog-content-wrapper {
  962. width: 100%;
  963. height: 100%;
  964. position: absolute;
  965. top: 0;
  966. left: 0;
  967. z-index: 100;
  968. }
  969. .title-box {
  970. position: absolute;
  971. top: 0;
  972. left: 0;
  973. right: 0;
  974. height: rpx(60);
  975. display: flex;
  976. align-items: center;
  977. justify-content: space-between;
  978. color: white;
  979. padding: 0 rpx(20);
  980. }
  981. .title-left {
  982. width: 33.33%;
  983. height: 100%;
  984. display: flex;
  985. align-items: center;
  986. gap: rpx(5);
  987. z-index: 30;
  988. .box-icon {
  989. display: flex;
  990. align-items: center;
  991. color: #0064BE;
  992. gap: rpx(5);
  993. padding: rpx(5) rpx(10);
  994. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  995. border: rpx(1) solid rgba(0, 100, 192);
  996. border-radius: rpx(30);
  997. backdrop-filter: blur(10px);
  998. cursor: pointer;
  999. transition: all 0.3s ease;
  1000. font-size: rpx(9);
  1001. font-weight: 500;
  1002. width: fit-content;
  1003. }
  1004. .box-icon:hover {
  1005. background-color: rgba(255, 255, 255, 90%);
  1006. transform: translateX(-3px);
  1007. }
  1008. .left-icon {
  1009. font-size: rpx(12);
  1010. }
  1011. }
  1012. .title-center {
  1013. display: flex;
  1014. align-items: center;
  1015. justify-content: center;
  1016. height: 100%;
  1017. background-image: url('@/assets/dialogue/number-title.png');
  1018. background-size: 100% 85%;
  1019. background-repeat: no-repeat;
  1020. background-position: center;
  1021. width: fit-content;
  1022. padding: 0 rpx(100);
  1023. min-width: rpx(100);
  1024. z-index: 10;
  1025. }
  1026. .title-text {
  1027. height: 100%;
  1028. font-size: rpx(11);
  1029. font-weight: bold;
  1030. display: flex;
  1031. align-items: center;
  1032. justify-content: center;
  1033. white-space: nowrap;
  1034. }
  1035. .title-right {
  1036. width: 33.33%;
  1037. height: 100%;
  1038. display: flex;
  1039. align-items: center;
  1040. justify-content: flex-end;
  1041. gap: rpx(10);
  1042. z-index: 10;
  1043. .box-icon {
  1044. display: flex;
  1045. align-items: center;
  1046. color: #0064BE;
  1047. gap: rpx(5);
  1048. padding: rpx(5) rpx(10);
  1049. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  1050. border: rpx(1) solid rgba(0, 100, 192);
  1051. border-radius: rpx(30);
  1052. backdrop-filter: blur(10px);
  1053. cursor: pointer;
  1054. transition: all 0.3s ease;
  1055. font-size: rpx(9);
  1056. font-weight: 500;
  1057. width: fit-content;
  1058. }
  1059. .box-icon:hover {
  1060. background-color: rgba(255, 255, 255, 90%);
  1061. transform: translateX(-3px);
  1062. }
  1063. .left-icon {
  1064. font-size: rpx(12);
  1065. }
  1066. .play-text {
  1067. font-size: rpx(9);
  1068. color: #0064BE;
  1069. font-weight: 500;
  1070. }
  1071. }
  1072. .background-image {
  1073. width: 100%;
  1074. height: 100%;
  1075. object-fit: cover;
  1076. z-index: 1;
  1077. position: relative;
  1078. }
  1079. .background-video {
  1080. width: 100%;
  1081. height: 100%;
  1082. object-fit: cover;
  1083. z-index: 1;
  1084. position: relative;
  1085. }
  1086. /* 遮罩层样式 */
  1087. .mask-layer {
  1088. position: absolute;
  1089. top: 0;
  1090. left: 0;
  1091. right: 0;
  1092. bottom: 0;
  1093. background-color: rgba(0, 0, 0, 0.7);
  1094. z-index: 20;
  1095. display: flex;
  1096. justify-content: center;
  1097. align-items: center;
  1098. opacity: 1;
  1099. transition: all 0.5s ease-out;
  1100. }
  1101. .mask-layer.fade-out {
  1102. opacity: 0;
  1103. transform: scale(1.1);
  1104. z-index: -1;
  1105. }
  1106. .play-button-container {
  1107. display: flex;
  1108. justify-content: center;
  1109. align-items: center;
  1110. }
  1111. .play-button {
  1112. width: rpx(80);
  1113. height: rpx(80);
  1114. border-radius: 50%;
  1115. border: none;
  1116. background: transparent;
  1117. display: flex;
  1118. justify-content: center;
  1119. align-items: center;
  1120. cursor: pointer;
  1121. outline: none;
  1122. -webkit-tap-highlight-color: transparent;
  1123. }
  1124. .play-icon {
  1125. font-size: rpx(40);
  1126. color: #A0DCF0;
  1127. transition: all 0.3s ease;
  1128. cursor: pointer;
  1129. }
  1130. .play-icon:hover {
  1131. transform: scale(1.2);
  1132. color: #50BEF0;
  1133. text-shadow: 0 0 rpx(10) rgba(64, 158, 255, 0.5);
  1134. }
  1135. .content-box {
  1136. position: absolute;
  1137. top: rpx(60);
  1138. left: 0;
  1139. right: 0;
  1140. bottom: 0;
  1141. display: flex;
  1142. align-items: flex-end;
  1143. justify-content: center;
  1144. padding-bottom: rpx(0);
  1145. z-index: 10;
  1146. }
  1147. .character {
  1148. position: absolute;
  1149. bottom: 0;
  1150. width: rpx(135);
  1151. height: rpx(240);
  1152. background-size: contain;
  1153. background-position: bottom;
  1154. background-repeat: no-repeat;
  1155. opacity: 0;
  1156. z-index: 1;
  1157. // background-color: #fff;
  1158. }
  1159. .character.left {
  1160. left: rpx(17);
  1161. transform: translateX(-100%);
  1162. animation: characterEnterLeft 0.8s ease forwards;
  1163. }
  1164. .character.right {
  1165. right: rpx(17);
  1166. transform: translateX(100%) scaleX(-1);
  1167. animation: characterEnterRight 0.8s ease forwards;
  1168. }
  1169. @keyframes characterEnterLeft {
  1170. 0% {
  1171. opacity: 0;
  1172. transform: translateX(-100%) scale(0.8);
  1173. }
  1174. 70% {
  1175. opacity: 0.9;
  1176. transform: translateX(10%) scale(1.05);
  1177. }
  1178. 100% {
  1179. opacity: 1;
  1180. transform: translateX(0) scale(1);
  1181. }
  1182. }
  1183. @keyframes characterEnterRight {
  1184. 0% {
  1185. opacity: 0;
  1186. transform: translateX(100%) scale(0.8) scaleX(-1);
  1187. }
  1188. 70% {
  1189. opacity: 0.9;
  1190. transform: translateX(-10%) scale(1.05) scaleX(-1);
  1191. }
  1192. 100% {
  1193. opacity: 1;
  1194. transform: translateX(0) scale(1) scaleX(-1);
  1195. }
  1196. }
  1197. .dialogue-card {
  1198. background: rgba(255, 255, 255, 0.9);
  1199. border-radius: rpx(6);
  1200. padding: rpx(8);
  1201. max-width: 35%;
  1202. min-width: rpx(200);
  1203. box-shadow: 0 rpx(3.5) rpx(10) rgba(0, 0, 0, 0.15);
  1204. position: absolute;
  1205. bottom: rpx(50);
  1206. width: auto;
  1207. display: inline-block;
  1208. z-index: 2;
  1209. }
  1210. .dialogue-card.left {
  1211. left: rpx(145);
  1212. animation: dialogueEnterLeft 0.6s ease forwards;
  1213. }
  1214. .dialogue-card.right {
  1215. right: rpx(145);
  1216. animation: dialogueEnterRight 0.6s ease forwards;
  1217. }
  1218. .dialogue-card.left::before {
  1219. content: '';
  1220. position: absolute;
  1221. left: rpx(-11.5);
  1222. bottom: rpx(12);
  1223. width: 0;
  1224. height: 0;
  1225. border-top: rpx(7) solid transparent;
  1226. border-bottom: rpx(7) solid transparent;
  1227. border-right: rpx(12) solid rgba(255, 255, 255, 0.9);
  1228. transform: translateY(0);
  1229. }
  1230. .dialogue-card.right::before {
  1231. content: '';
  1232. position: absolute;
  1233. right: rpx(-11.5);
  1234. bottom: rpx(12);
  1235. width: 0;
  1236. height: 0;
  1237. border-top: rpx(7) solid transparent;
  1238. border-bottom: rpx(7) solid transparent;
  1239. border-left: rpx(12) solid rgba(255, 255, 255, 0.9);
  1240. transform: translateY(0);
  1241. }
  1242. @keyframes dialogueEnterLeft {
  1243. from {
  1244. opacity: 0;
  1245. transform: translateX(-rpx(30)) translateY(rpx(20));
  1246. }
  1247. to {
  1248. opacity: 1;
  1249. transform: translateX(0) translateY(0);
  1250. }
  1251. }
  1252. @keyframes dialogueEnterRight {
  1253. from {
  1254. opacity: 0;
  1255. transform: translateX(rpx(30)) translateY(rpx(20));
  1256. }
  1257. to {
  1258. opacity: 1;
  1259. transform: translateX(0) translateY(0);
  1260. }
  1261. }
  1262. .dialogue-header {
  1263. position: absolute;
  1264. top: rpx(-11);
  1265. left: rpx(12);
  1266. background: #409EFF;
  1267. color: white;
  1268. padding: rpx(1.2) rpx(6);
  1269. border-radius: rpx(5);
  1270. font-size: rpx(8);
  1271. box-shadow: 0 rpx(2.5) rpx(10) rgba(0, 0, 0, 0.2);
  1272. }
  1273. .dialogue-card.right .dialogue-header {
  1274. left: rpx(10);
  1275. }
  1276. .role-name {
  1277. font-weight: 600;
  1278. color: white;
  1279. font-size: rpx(10);
  1280. }
  1281. .dialogue-content {
  1282. font-size: rpx(12);
  1283. line-height: 1.2;
  1284. color: #333;
  1285. text-align: left;
  1286. display: flex;
  1287. flex-direction: column;
  1288. align-items: center;
  1289. justify-content: center;
  1290. width: 100%;
  1291. }
  1292. .user-input-card {
  1293. max-width: 50%;
  1294. position: absolute;
  1295. left: 50%;
  1296. transform: translateX(-50%);
  1297. bottom: rpx(50);
  1298. right: auto;
  1299. animation: dialogueEnterCenter 0.6s ease forwards;
  1300. }
  1301. .user-input-card::before {
  1302. display: none;
  1303. }
  1304. @keyframes dialogueEnterCenter {
  1305. from {
  1306. opacity: 0;
  1307. transform: translateX(-50%) translateY(rpx(20));
  1308. }
  1309. to {
  1310. opacity: 1;
  1311. transform: translateX(-50%) translateY(0);
  1312. }
  1313. }
  1314. .user-input-textarea {
  1315. width: 95%;
  1316. min-height: rpx(50);
  1317. max-height: rpx(150);
  1318. border: none;
  1319. outline: none;
  1320. background: transparent;
  1321. font-size: rpx(10);
  1322. line-height: 1.2;
  1323. color: #333;
  1324. resize: none;
  1325. font-family: inherit;
  1326. overflow-y: auto;
  1327. text-align: left;
  1328. padding: rpx(5) 0;
  1329. }
  1330. .voice-input-content {
  1331. width: 95%;
  1332. min-height: rpx(50);
  1333. max-height: rpx(150);
  1334. font-size: rpx(10);
  1335. line-height: 1.2;
  1336. color: #333;
  1337. text-align: left;
  1338. padding: rpx(5) 0;
  1339. overflow-y: auto;
  1340. }
  1341. .voice-input-placeholder {
  1342. width: 95%;
  1343. min-height: rpx(50);
  1344. font-size: rpx(10);
  1345. line-height: 1.2;
  1346. color: #999;
  1347. text-align: left;
  1348. padding: rpx(5) 0;
  1349. font-style: italic;
  1350. }
  1351. /* 滚动条样式 */
  1352. .user-input-textarea::-webkit-scrollbar {
  1353. width: rpx(0);
  1354. }
  1355. .user-input-textarea::-webkit-scrollbar-track {
  1356. background: rgba(0, 0, 0, 0.05);
  1357. border-radius: rpx(3);
  1358. }
  1359. .user-input-textarea::-webkit-scrollbar-thumb {
  1360. background: rgba(64, 158, 255, 0.5);
  1361. border-radius: rpx(3);
  1362. }
  1363. .user-input-textarea::-webkit-scrollbar-thumb:hover {
  1364. background: rgba(64, 158, 255, 0.8);
  1365. }
  1366. .input-actions {
  1367. display: flex;
  1368. justify-content: flex-end;
  1369. gap: rpx(6);
  1370. margin-top: rpx(6);
  1371. width: 100%;
  1372. }
  1373. .cancel-btn, .submit-btn {
  1374. padding: rpx(2.5) rpx(10);
  1375. border: none;
  1376. border-radius: rpx(4);
  1377. font-size: rpx(8);
  1378. cursor: pointer;
  1379. transition: all 0.3s ease;
  1380. }
  1381. .cancel-btn {
  1382. background: #E0E0E0;
  1383. color: #666;
  1384. }
  1385. .submit-btn {
  1386. background: #409EFF;
  1387. color: white;
  1388. }
  1389. .cancel-btn:hover, .submit-btn:hover {
  1390. transform: scale(1.05);
  1391. }
  1392. .cancel-btn:active, .submit-btn:active {
  1393. transform: scale(0.95);
  1394. }
  1395. .dialogue-content :deep(p) {
  1396. margin: 0 0 rpx(4) 0;
  1397. }
  1398. .dialogue-content :deep(strong),
  1399. .dialogue-content :deep(b) {
  1400. font-weight: bold;
  1401. }
  1402. .dialogue-content :deep(em),
  1403. .dialogue-content :deep(i) {
  1404. font-style: italic;
  1405. }
  1406. .dialogue-content :deep(ul),
  1407. .dialogue-content :deep(ol) {
  1408. margin: rpx(4) 0;
  1409. padding-left: rpx(10);
  1410. }
  1411. .dialogue-content :deep(li) {
  1412. margin: rpx(2) 0;
  1413. }
  1414. .dialogue-content :deep(code) {
  1415. background-color: #f0f0f0;
  1416. padding: rpx(1) rpx(3);
  1417. border-radius: rpx(2);
  1418. font-family: monospace;
  1419. font-size: rpx(9);
  1420. }
  1421. .dialogue-content :deep(a) {
  1422. color: #409EFF;
  1423. text-decoration: underline;
  1424. }
  1425. .input-buttons-container {
  1426. position: relative;
  1427. display: flex;
  1428. align-items: center;
  1429. justify-content: center;
  1430. width: 100%;
  1431. z-index: 10;
  1432. transition: all 0.3s ease;
  1433. transform: scale(0.9);
  1434. gap: rpx(10);
  1435. .arrow-icon-circle {
  1436. width: rpx(20);
  1437. height: rpx(20);
  1438. border-radius: 50%;
  1439. border: rpx(1) solid rgba(0, 100, 192);
  1440. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  1441. display: flex;
  1442. align-items: center;
  1443. justify-content: center;
  1444. cursor: pointer;
  1445. transition: all 0.3s ease;
  1446. &:hover:not(.disabled) {
  1447. transform: scale(1.1);
  1448. box-shadow: 0 rpx(1) rpx(6) rgba(0, 0, 0, 0.3);
  1449. }
  1450. &:active:not(.disabled) {
  1451. transform: scale(0.95);
  1452. }
  1453. &.disabled {
  1454. opacity: 0.5;
  1455. cursor: not-allowed;
  1456. border-color: rgba(0, 100, 192, 0.3);
  1457. background: linear-gradient(135deg, rgba(160, 220, 240, 0.5), rgba(80, 190, 240, 0.5));
  1458. }
  1459. .arrow-icon {
  1460. font-size: rpx(15);
  1461. color: #0064BE;
  1462. }
  1463. }
  1464. }
  1465. .voice-input-outer,
  1466. .keyboard-input-outer {
  1467. position: absolute;
  1468. display: flex;
  1469. align-items: center;
  1470. justify-content: center;
  1471. transition: all 0.3s ease;
  1472. }
  1473. .voice-input-outer {
  1474. position: relative;
  1475. width: rpx(50);
  1476. height: rpx(50);
  1477. border-radius: 50%;
  1478. background: transparent;
  1479. display: flex;
  1480. align-items: center;
  1481. justify-content: center;
  1482. // 正常状态只显示一圈
  1483. &::before {
  1484. content: '';
  1485. position: absolute;
  1486. width: 85%;
  1487. height: 85%;
  1488. border-radius: 50%;
  1489. border: rpx(2) solid rgba(80, 190, 240, 0.6);
  1490. }
  1491. // 录音时显示波纹效果
  1492. &.recording {
  1493. &::before {
  1494. animation: pulse 2s infinite;
  1495. border-color: rgba(0, 100, 192, 0.6);
  1496. }
  1497. &::after {
  1498. content: '';
  1499. position: absolute;
  1500. width: 85%;
  1501. height: 85%;
  1502. border-radius: 50%;
  1503. border: rpx(2) solid rgba(0, 100, 192, 0.4);
  1504. animation: pulse 2s infinite 0.5s;
  1505. }
  1506. :deep(.voice-input-container) {
  1507. .speech-btn {
  1508. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  1509. border-color: rgba(0, 100, 192, 1);
  1510. .el-icon {
  1511. color: #0064BE;
  1512. }
  1513. }
  1514. }
  1515. }
  1516. // 占位符样式
  1517. &.placeholder {
  1518. visibility: hidden;
  1519. }
  1520. :deep(.voice-input-container) {
  1521. position: relative;
  1522. z-index: 10;
  1523. .speech-btn {
  1524. width: rpx(40);
  1525. height: rpx(40);
  1526. border-radius: 50%;
  1527. border: rpx(2) solid rgba(0, 100, 192);
  1528. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  1529. display: flex;
  1530. align-items: center;
  1531. justify-content: center;
  1532. padding: 0;
  1533. gap: 0;
  1534. transition: all 0.3s ease;
  1535. cursor: pointer;
  1536. position: relative;
  1537. overflow: hidden;
  1538. &:hover {
  1539. transform: scale(1.05);
  1540. box-shadow: 0 rpx(4) rpx(12) rgba(0, 0, 0, 0.3);
  1541. }
  1542. &:active {
  1543. transform: scale(0.95);
  1544. }
  1545. &::before {
  1546. content: '';
  1547. position: absolute;
  1548. top: 50%;
  1549. left: 50%;
  1550. width: 0;
  1551. height: 0;
  1552. border-radius: 50%;
  1553. background: rgba(255, 255, 255, 0.5);
  1554. transform: translate(-50%, -50%);
  1555. transition: width 0.6s, height 0.6s;
  1556. }
  1557. &:active::before {
  1558. width: rpx(80);
  1559. height: rpx(80);
  1560. }
  1561. .el-icon {
  1562. font-size: rpx(20);
  1563. color: #0064BE;
  1564. z-index: 1;
  1565. }
  1566. .countdown-text {
  1567. display: block;
  1568. font-size: rpx(8);
  1569. color: #0064BE;
  1570. position: absolute;
  1571. bottom: rpx(5);
  1572. left: 50%;
  1573. transform: translateX(-50%);
  1574. }
  1575. }
  1576. }
  1577. }
  1578. @keyframes pulse {
  1579. 0% {
  1580. transform: scale(1);
  1581. opacity: 1;
  1582. }
  1583. 100% {
  1584. transform: scale(1.15);
  1585. opacity: 0;
  1586. }
  1587. }
  1588. .keyboard-input-outer {
  1589. width: rpx(30);
  1590. height: rpx(30);
  1591. border-radius: 50%;
  1592. background: transparent;
  1593. &.active {
  1594. width: rpx(40);
  1595. height: rpx(40);
  1596. left: 50%;
  1597. transform: translateX(-50%);
  1598. z-index: 11;
  1599. .keyboard-btn {
  1600. width: rpx(36);
  1601. height: rpx(36);
  1602. border: rpx(2) solid rgba(0, 100, 192);
  1603. box-shadow: 0 rpx(3) rpx(8) rgba(0, 0, 0, 0.3);
  1604. .keyboard-icon {
  1605. font-size: rpx(18);
  1606. }
  1607. }
  1608. }
  1609. &.inactive {
  1610. width: rpx(24);
  1611. height: rpx(24);
  1612. left: calc(50% + rpx(40));
  1613. transform: translateX(-50%);
  1614. z-index: 10;
  1615. .keyboard-btn {
  1616. width: rpx(22);
  1617. height: rpx(22);
  1618. border: rpx(1) solid rgba(128, 128, 128, 0.5);
  1619. box-shadow: none;
  1620. background: linear-gradient(135deg, #E0E0E0, #C0C0C0);
  1621. .keyboard-icon {
  1622. font-size: rpx(12);
  1623. color: #808080;
  1624. }
  1625. }
  1626. }
  1627. }
  1628. .keyboard-btn {
  1629. width: rpx(28);
  1630. height: rpx(28);
  1631. border-radius: 50%;
  1632. border: rpx(1) solid rgba(0, 100, 192);
  1633. background: linear-gradient(135deg, #A0DCF0, #50BEF0);
  1634. display: flex;
  1635. align-items: center;
  1636. justify-content: center;
  1637. padding: 0;
  1638. gap: 0;
  1639. transition: all 0.3s ease;
  1640. cursor: pointer;
  1641. &:hover {
  1642. transform: scale(1.1);
  1643. box-shadow: 0 rpx(2) rpx(8) rgba(0, 0, 0, 0.3);
  1644. }
  1645. &:active {
  1646. transform: scale(0.95);
  1647. }
  1648. .keyboard-icon {
  1649. font-size: rpx(14);
  1650. color: #0064BE;
  1651. }
  1652. }
  1653. /* 诗词显示样式 */
  1654. .poem-display {
  1655. position: absolute;
  1656. top: 25%;
  1657. left: 50%;
  1658. transform: translate(-50%, -50%);
  1659. width: rpx(500);
  1660. height: rpx(200);
  1661. padding: rpx(0);
  1662. display: flex;
  1663. justify-content: center;
  1664. align-items: center;
  1665. z-index: 5;
  1666. animation: scrollBackgroundFadeIn 0.8s ease-out;
  1667. }
  1668. .poem-display::before {
  1669. content: '';
  1670. position: absolute;
  1671. top: 0;
  1672. left: 0;
  1673. width: 100%;
  1674. height: 100%;
  1675. background-image: url('@/assets/dialogue/long-scroll.png');
  1676. background-size: 100%;
  1677. background-repeat: no-repeat;
  1678. background-position: center;
  1679. opacity: 0.8;
  1680. z-index: -1;
  1681. }
  1682. .poem-content {
  1683. text-align: center;
  1684. animation: poemFadeIn 1s ease-in-out;
  1685. transition: all 0.5s ease-in-out;
  1686. }
  1687. .poem-text {
  1688. width: 100%;
  1689. max-width: rpx(500);
  1690. max-height: rpx(200);
  1691. height: auto;
  1692. font-family: 'STKaiti', 'KaiTi', '楷体', 'Ma Shan Zheng', cursive;
  1693. font-size: rpx(20);
  1694. color: black;
  1695. position: relative;
  1696. overflow: auto;
  1697. display: flex;
  1698. align-items: center;
  1699. justify-content: center;
  1700. text-align: center;
  1701. transition: all 0.5s ease-in-out;
  1702. }
  1703. .poem-text::-webkit-scrollbar {
  1704. width: rpx(0);
  1705. }
  1706. .poem-text::-webkit-scrollbar-track {
  1707. background: rgba(255, 255, 255, 0.1);
  1708. border-radius: rpx(4);
  1709. }
  1710. .poem-text::-webkit-scrollbar-thumb {
  1711. background: rgba(0, 0, 0, 0.3);
  1712. border-radius: rpx(4);
  1713. }
  1714. .poem-text::-webkit-scrollbar-thumb:hover {
  1715. background: rgba(0, 0, 0, 0.5);
  1716. }
  1717. .video-display {
  1718. position: absolute;
  1719. top: 45%;
  1720. left: 50%;
  1721. transform: translate(-50%, -50%) scale(0.8);
  1722. z-index: 5;
  1723. width: 100%;
  1724. display: flex;
  1725. justify-content: center;
  1726. align-items: center;
  1727. opacity: 0;
  1728. animation: videoFadeIn 0.8s ease-out forwards;
  1729. }
  1730. .video-frame {
  1731. width: rpx(420);
  1732. height: rpx(250);
  1733. padding: rpx(5);
  1734. background: linear-gradient(135deg, rgba(160, 220, 240, 0.8), rgba(80, 190, 240, 0.8));
  1735. border: rpx(2) solid rgba(0, 100, 192, 0.8);
  1736. border-radius: rpx(15);
  1737. box-shadow: 0 rpx(8) rpx(25) rgba(0, 0, 0, 0.4);
  1738. backdrop-filter: blur(rpx(5));
  1739. display: flex;
  1740. justify-content: center;
  1741. align-items: center;
  1742. }
  1743. .dialogue-video {
  1744. width: 100%;
  1745. height: 100%;
  1746. border-radius: rpx(10);
  1747. background-color: #000;
  1748. object-fit: contain;
  1749. }
  1750. .poem-text::before {
  1751. content: '';
  1752. position: absolute;
  1753. top: 0;
  1754. left: 0;
  1755. right: 0;
  1756. bottom: 0;
  1757. animation: poemShine 3s ease-in-out infinite;
  1758. }
  1759. @keyframes poemFadeIn {
  1760. from {
  1761. opacity: 0;
  1762. transform: scale(0.8) translateY(20px);
  1763. }
  1764. to {
  1765. opacity: 1;
  1766. transform: scale(1) translateY(0);
  1767. }
  1768. }
  1769. @keyframes scrollBackgroundFadeIn {
  1770. from {
  1771. opacity: 0;
  1772. background-size: 80%;
  1773. }
  1774. to {
  1775. opacity: 1;
  1776. background-size: 100%;
  1777. }
  1778. }
  1779. .poem-text p {
  1780. margin: rpx(10) 0;
  1781. opacity: 0;
  1782. animation: poemSlideIn 0.5s ease forwards;
  1783. font-weight: 500;
  1784. letter-spacing: rpx(2);
  1785. }
  1786. .poem-text p:nth-child(1) {
  1787. animation-delay: 0.2s;
  1788. }
  1789. .poem-text p:nth-child(2) {
  1790. animation-delay: 0.4s;
  1791. }
  1792. .poem-text p:nth-child(3) {
  1793. animation-delay: 0.6s;
  1794. }
  1795. .poem-text p:nth-child(4) {
  1796. animation-delay: 0.8s;
  1797. }
  1798. .poem-text p:nth-child(5) {
  1799. animation-delay: 1s;
  1800. }
  1801. @keyframes poemSlideIn {
  1802. from {
  1803. opacity: 0;
  1804. transform: translateY(10px);
  1805. }
  1806. to {
  1807. opacity: 1;
  1808. transform: translateY(0);
  1809. }
  1810. }
  1811. // 对话视频显示动画
  1812. @keyframes videoFadeIn {
  1813. 0% {
  1814. opacity: 0;
  1815. transform: translate(-50%, -50%) scale(0.8);
  1816. }
  1817. 70% {
  1818. opacity: 1;
  1819. transform: translate(-50%, -50%) scale(1.05);
  1820. }
  1821. 100% {
  1822. opacity: 1;
  1823. transform: translate(-50%, -50%) scale(1);
  1824. }
  1825. }
  1826. </style>