文件缓存处理

This commit is contained in:
yanghang
2025-07-03 11:16:51 +08:00
parent b2b730ceef
commit 32be7fbfb7
4 changed files with 113 additions and 28 deletions
+3
View File
@@ -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));
}
});
+46 -17
View File
@@ -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) =>{
+12 -11
View File
@@ -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)
})
}
})
}
}
+52
View File
@@ -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
}
}
})