45
45
<template v-else >
46
46
<el-empty description =" 默认词组中没有收藏单词" />
47
47
</template >
48
+ <audio autoplay ref =" audioPlayer" :src =" audioUrl" >123</audio >
48
49
</template >
49
50
50
51
<script setup lang="ts">
51
52
import Header from ' @/components/Header.vue' ;
52
53
import type { ResponseData , Word } from ' @/types' ;
53
54
import { request } from ' @/utils/service' ;
54
55
import { Refresh } from ' @element-plus/icons-vue' ;
56
+ import axios from ' axios' ;
55
57
import { ElNotification } from ' element-plus' ;
56
58
import { computed , onMounted , ref , type Ref } from ' vue' ;
57
59
import { useRoute , useRouter } from ' vue-router' ;
58
60
59
- const buttonTexts = [' 上一个 ' , ' 文章链接 ' , ' 下一个' ];
61
+ const buttonTexts = [' 文章链接 ' , ' 发音 ' , ' 上一个 ' , ' 下一个' ];
60
62
61
63
const route = useRoute ();
62
64
const router = useRouter ();
@@ -66,6 +68,8 @@ const wordList: Ref<Word[]> = ref([]);
66
68
const currentIndex = ref (0 );
67
69
const currentTranslation = ref ();
68
70
const originalPageUrl = ref (' ' );
71
+ const audioUrl = ref (' ' );
72
+ const audioPlayer: Ref <HTMLAudioElement | null > = ref (null );
69
73
70
74
const currentWord = computed (() => wordList .value [currentIndex .value ] ?? { en: ' ' });
71
75
@@ -78,6 +82,21 @@ onMounted(async () => {
78
82
await getGroupDetail ();
79
83
});
80
84
85
+ async function handleClick() {
86
+ console .log (currentWord .value .en );
87
+ const response: any = await axios ({
88
+ url: ` ${import .meta .env .VITE_BACKEND_URL }/youdao/ ` , // 后端 API
89
+ method: ' POST' ,
90
+ data: { word: currentWord .value .en },
91
+ responseType: ' blob' , // 获取音频为 Blob
92
+ });
93
+ console .log (response .data );
94
+ const audioBlob = response .data ;
95
+ const u = URL .createObjectURL (audioBlob );
96
+ audioUrl .value = u ;
97
+ audioPlayer .value ?.play ();
98
+ }
99
+
81
100
async function getWordList() {
82
101
const t: ResponseData = await request ({ url: ' /word/bygroup' , method: ' post' , data: { groupId: groupId .value } });
83
102
@@ -120,6 +139,9 @@ function navigateWord(text: string) {
120
139
window .open (` http://${originalPageUrl .value } ` , ' _blank' );
121
140
}
122
141
}
142
+ if (text === ' 发音' ) {
143
+ handleClick ();
144
+ }
123
145
isTranslationVisible .value = false ;
124
146
}
125
147
</script >
0 commit comments