From 32be7fbfb79576079d53417fec9bf72a3eba0a34 Mon Sep 17 00:00:00 2001 From: yanghang Date: Thu, 3 Jul 2025 11:16:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=BC=93=E5=AD=98=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/common.js | 3 + src/pages/course/components/talkingItem.vue | 63 +++++++++++++++------ src/pages/course/study.vue | 23 ++++---- src/store/storage.js | 52 +++++++++++++++++ 4 files changed, 113 insertions(+), 28 deletions(-) create mode 100644 src/store/storage.js diff --git a/src/api/common.js b/src/api/common.js index 9897be59..6ae46633 100644 --- a/src/api/common.js +++ b/src/api/common.js @@ -4,6 +4,7 @@ import { import { getToken } from '@/common/common.js' +import common from '@/common/common'; export const uploadFile = (file, data) => { @@ -145,9 +146,11 @@ export function downloadVoiceFile(url) { url: `${baseUrl}${url}`, header, success(result) { + console.log(result) resolve(result) }, fail(error) { + console.log(error) reject(new Error(error)); } }); diff --git a/src/pages/course/components/talkingItem.vue b/src/pages/course/components/talkingItem.vue index 4f22146e..e80cecf6 100644 --- a/src/pages/course/components/talkingItem.vue +++ b/src/pages/course/components/talkingItem.vue @@ -138,6 +138,7 @@ import { downloadVoiceFile, uploadVoiceFile, } from "@/api/common.js" import { studyParagraphOverApi } from "@/api/study.js" import ChartFour from "./chartFour.vue" import ChartThree from "./chartThree.vue" +import common from '@/common/common'; import { getUserInfo } from '@/common/common'; import { useStorageStore } from "@/store/storage.js" @@ -220,6 +221,11 @@ const audioCacheObj = computed(() => storageStore.audioObj) } const itemVoice = ref('') const playVoice = (readType = 'pgth') =>{ + let _content = unref(dataInfo)?.pgrphContent || unref(dataInfo)?.qnsContent || unref(dataInfo)?.talkingContent || '' + if(!_content){ + common.msg('没有可以播放的文字信息!') + return + } // readType 阅读内容类型pgrh段落/qns问题 emit('changePlay', unref(dataInfo)?.id) if(itemVoice.value){ @@ -235,29 +241,52 @@ const audioCacheObj = computed(() => storageStore.audioObj) qnsId: unref(dataInfo)?.qnsId || '', readType: readType, teachNo: unref(dataInfo)?.userInfo?.teacherNo, // 必填 - content: unref(dataInfo)?.pgrphContent || unref(dataInfo)?.qnsContent || unref(dataInfo)?.talkingContent, + content: _content } let _url = '/trastudy/traStdyInfo/readContent?' + Object.keys(_params).map(t=>{ return encodeURIComponent(t) + '=' + encodeURIComponent(_params[t]) }).join('&') - let _cache = audioCacheObj.value[_url] - if(_cache){ - voiceLoading.value = false - itemVoice.value = _cache - talkVoiceObj.value.src = _cache - talkVoiceObj.value.play() - return - } - downloadVoiceFile(_url).then(res=>{ - voiceLoading.value = false - itemVoice.value = res.tempFilePath - talkVoiceObj.value.src = res.tempFilePath - talkVoiceObj.value.play() - storageStore.setStorage('audio', _url, itemVoice.value) - }).catch((err)=>{ - voiceLoading.value = false + storageStore.getStorageById('audio', _url, (url)=>{ + if(url){ + voiceLoading.value = false + itemVoice.value = url + talkVoiceObj.value.src = url + talkVoiceObj.value.play() + }else{ + downloadVoiceFile(_url).then(res=>{ + console.log(res) + if(res?.data){ + common.msg(res.data.message) + return + } + voiceLoading.value = false + itemVoice.value = res.tempFilePath + talkVoiceObj.value.src = res.tempFilePath + talkVoiceObj.value.play() + storageStore.setStorage('audio', _url, itemVoice.value) + }).catch((err)=>{ + voiceLoading.value = false + }) + } }) + // let _cache = audioCacheObj.value[_url] + // if(_cache){ + // voiceLoading.value = false + // itemVoice.value = _cache + // talkVoiceObj.value.src = _cache + // talkVoiceObj.value.play() + // return + // } + // downloadVoiceFile(_url).then(res=>{ + // voiceLoading.value = false + // itemVoice.value = res.tempFilePath + // talkVoiceObj.value.src = res.tempFilePath + // talkVoiceObj.value.play() + // storageStore.setStorage('audio', _url, itemVoice.value) + // }).catch((err)=>{ + // voiceLoading.value = false + // }) } } const playQnsVoice = (readType = 'qns', item) =>{ diff --git a/src/pages/course/study.vue b/src/pages/course/study.vue index ad2077a3..e33c48bc 100644 --- a/src/pages/course/study.vue +++ b/src/pages/course/study.vue @@ -266,17 +266,18 @@ const playTeacherVoice = (item) =>{ teacherVoiceObj.src = item.voicePath teacherVoiceObj.play() }else{ - let _cache = audioCacheObj.value[item.teacherNo] - if(_cache){ - teacherVoiceObj.src = _cache - teacherVoiceObj.play() - return - } - downloadVoiceFile('/trastudy/traStdyInfo/trialListening?teachNo='+item.teacherNo).then(res=>{ - item.voicePath = res.tempFilePath - teacherVoiceObj.src = item.voicePath - teacherVoiceObj.play() - storageStore.setStorage('audio', id, item.voicePath) + storageStore.getStorageById('audio', item.teacherNo, (url)=>{ + if(url){ + teacherVoiceObj.src = _cache + teacherVoiceObj.play() + }else{ + downloadVoiceFile('/trastudy/traStdyInfo/trialListening?teachNo='+item.teacherNo).then(res=>{ + item.voicePath = res.tempFilePath + teacherVoiceObj.src = item.voicePath + teacherVoiceObj.play() + storageStore.setStorage('audio', item.teacherNo, item.voicePath) + }) + } }) } } diff --git a/src/store/storage.js b/src/store/storage.js new file mode 100644 index 00000000..c0ae35ff --- /dev/null +++ b/src/store/storage.js @@ -0,0 +1,52 @@ +import { defineStore } from 'pinia'; +import { ref } from 'vue'; + +export const useStorageStore = defineStore('storageStore',{ + state:()=>({ + imgObjStr: '{}', + videoObjStr: '{}', + audioObjStr: '{}' + }), + getters:{ + imgObj(state){ + return JSON.parse(state.imgObjStr) + }, + videoObj(state){ + return JSON.parse(state.videoObjStr) + }, + audioObj(state){ + return JSON.parse(state.audioObjStr) + } + }, + actions:{ + setStorage(type, id, url) { + if(type === 'image'){ + let _obj = JSON.parse(this.imgObjStr) + _obj[id] = url + this.imgObjStr = JSON.stringify(_obj) + }else if(type === 'video'){ + let _obj = JSON.parse(this.videoObjStr) + _obj[id] = url + this.videoObjStr = JSON.stringify(_obj) + }else if(type === 'audio'){ + let _obj = JSON.parse(this.audioObjStr) + _obj[id] = url + this.audioObjStr = JSON.stringify(_obj) + } + }, + getStorageById(type, id, cb = (()=>{})){ + // 两种方式 ,传递回调函数 或 直接取return值 + let _obj + if(type === 'image'){ + _obj = JSON.parse(this.imgObjStr) + }else if(type === 'video'){ + _obj = JSON.parse(this.videoObjStr) + }else if(type === 'audio'){ + _obj = JSON.parse(this.audioObjStr) + } + let _cache = _obj[id]||'' + cb && cb(_cache) + return _cache + } + } +}) \ No newline at end of file