DialogContent.vue 42 KB

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