fix: 首页TTS同样改用blob播放,解决downloadFile导致页面刷新的问题

This commit is contained in:
TradeMate Dev
2026-05-17 14:18:29 +08:00
parent 7b7f90d57a
commit 1fabad0fe9
+31 -3
View File
@@ -487,12 +487,39 @@ const playTryResult = () => {
const hasChinese = /[\u4e00-\u9fa5]/.test(tryText.value) const hasChinese = /[\u4e00-\u9fa5]/.test(tryText.value)
const lang = hasChinese ? 'en' : 'zh' const lang = hasChinese ? 'en' : 'zh'
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
const url = `${BASE_URL}/translate/tts?text=${encodeURIComponent(tryResult.value)}&lang=${lang}` const text = tryResult.value
uni.showLoading({ title: '语音生成中...' }) uni.showLoading({ title: '语音生成中...' })
if (typeof window !== 'undefined' && window.Audio) {
uni.request({
url: `${BASE_URL}/translate/tts?text=${encodeURIComponent(text)}&lang=${lang}`,
method: 'GET',
header: { Authorization: `Bearer ${token}` },
responseType: 'arraybuffer',
success: (res) => {
uni.hideLoading()
if (res.statusCode === 200 && res.data) {
const blob = new Blob([res.data], { type: 'audio/mpeg' })
const url = URL.createObjectURL(blob)
const audio = new Audio(url)
audio.onended = () => { URL.revokeObjectURL(url) }
audio.play().catch(() => {
uni.showToast({ title: '播放失败,请检查音量', icon: 'none' })
})
} else {
uni.showToast({ title: '语音生成失败', icon: 'none' })
}
},
fail: () => {
uni.hideLoading()
uni.showToast({ title: '语音生成失败', icon: 'none' })
},
})
} else {
uni.downloadFile({ uni.downloadFile({
url, url: `${BASE_URL}/translate/tts?text=${encodeURIComponent(text)}&lang=${lang}`,
header: token ? { Authorization: `Bearer ${token}` } : {}, header: { Authorization: `Bearer ${token}` },
success: (res) => { success: (res) => {
uni.hideLoading() uni.hideLoading()
if (res.statusCode === 200) { if (res.statusCode === 200) {
@@ -509,6 +536,7 @@ const playTryResult = () => {
uni.showToast({ title: '语音生成失败', icon: 'none' }) uni.showToast({ title: '语音生成失败', icon: 'none' })
}, },
}) })
}
} }
</script> </script>