From cfcda80e03a40f372f7e05ea4123ec2c0a1e6e31 Mon Sep 17 00:00:00 2001 From: yanghang Date: Thu, 17 Jul 2025 16:00:09 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=20=E8=AF=BE=E7=A8=8B=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E6=A0=A1=E9=AA=8C=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/common.js | 32 +++++++++++++++++++++ src/pages/course/components/talkingItem.vue | 30 ++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/api/common.js b/src/api/common.js index 61154a68..7b3a4a92 100644 --- a/src/api/common.js +++ b/src/api/common.js @@ -157,6 +157,38 @@ export function downloadVoiceFile(url) { }); }); } + + +export function getAudioByText(data) { + const token = getToken(); + const header = { + "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", + }; + if (token) + header['summary'] = token + let baseUrl = get_base_url(); + + return new Promise((resolve, reject) => { + uni.request({ + url: `${baseUrl}/trastudy/traStdyInfo/readContent`, + header, + method:"POST", + data, + responseType: "arraybuffer", + success(result) { + if(result.statusCode === 200){ + resolve(result.data) + }else{ + reject(new Error(result)) + } + }, + fail(error) { + reject(new Error(error)); + } + }); + }); +} + // 对象存储预览 /traoss/tras3/show/{文件ID} export function previewFile(id, responseType = 'arraybuffer') { const token = getToken(); diff --git a/src/pages/course/components/talkingItem.vue b/src/pages/course/components/talkingItem.vue index b2d61564..edc1b881 100644 --- a/src/pages/course/components/talkingItem.vue +++ b/src/pages/course/components/talkingItem.vue @@ -185,7 +185,7 @@ import { watch, unref, ref, toRefs, computed, nextTick,onMounted } from 'vue' import ImagePreview from '@/components/image-preview/image-preview.vue' import VideoPreview from '@/components/image-preview/video-preview.vue' -import { downloadVoiceFile, uploadVoiceFile, } from "@/api/common.js" +import { downloadVoiceFile, uploadVoiceFile, getAudioByText } from "@/api/common.js" import { studyParagraphOverApi, getAskAboutRawApi } from "@/api/study.js" import ChartFour from "./chartFour.vue" import ChartThree from "./chartThree.vue" @@ -373,6 +373,8 @@ const audioCacheObj = computed(() => storageStore.audioObj) teachNo: unref(teacherInfo)?.teacherNo, // 必填 content: _content } + + let _url = '/trastudy/traStdyInfo/readContent?' + Object.keys(_params).map(t=>{ return encodeURIComponent(t) + '=' + encodeURIComponent(_params[t]) }).join('&') @@ -384,6 +386,19 @@ const audioCacheObj = computed(() => storageStore.audioObj) talkVoiceObj.value.src = url talkVoiceObj.value.play() }else{ + // getAudioByText(_params).then(res=>{ + // // uni.env.USER_DATA_PATH || + // let filePath = `${'/'}/${dataInfo.value.id}.mp3` + // saveFile(res, filePath, () => { + // voiceLoading.value = false + // itemVoice.value = filePath + // talkVoiceObj.value.src = filePath + // talkVoiceObj.value.play() + // storageStore.setStorage('audio', _url + _params.teachNo, itemVoice.value) + // }) + // }).catch(err=>{ + // console.log(err) + // }) downloadVoiceFile(_url).then(res=>{ if(res?.data){ common.msg(res.data.message) @@ -401,6 +416,19 @@ const audioCacheObj = computed(() => storageStore.audioObj) }) } } + const saveFile = (buffer,filePath, cb = ()=>{}) => { + let fileManager = uni.getFileSystemManager() + fileManager.writeFile({ + filePath: filePath, + data: buffer, + encoding: 'binary', + success: cb, + file: ()=>{ + voiceLoading.value = false + console.log('文件保存失败!') + } + }) + } const initVoice = (readType = 'qns', item, isplay=false) => { if(unref(dataInfo).talkingVoice){ itemVoice.value = unref(dataInfo).talkingVoice