DialogContent.vue 41 KB

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