diff --git a/App.vue b/App.vue index 830c256..4c2fa51 100644 --- a/App.vue +++ b/App.vue @@ -25,10 +25,12 @@ } }, }, - onLaunch() { + async onLaunch() { // 提前获取二维码 this.getQrcode() + // 预加载音色列表(不等待完成,后台加载) + this.$store.dispatch('getVoiceList') }, async onShow() { // if(!uni.getStorageSync('token')){ @@ -36,9 +38,16 @@ // url: '/subPages/login/login', // }) // } + + // 确保配置数据和音色列表都已初始化 await this.$store.dispatch('initData') console.log('配置数据初始化完成') + // 如果音色列表还没有加载,再次尝试加载 + if (this.$store.state.voiceList.length === 0) { + this.$store.dispatch('getVoiceList') + } + if(uni.getStorageSync('token')){ this.getUserInfo() } diff --git a/stores/index.js b/stores/index.js index 2400b57..f148891 100644 --- a/stores/index.js +++ b/stores/index.js @@ -15,7 +15,9 @@ const store = new Vuex.Store({ departmentList: [], categoryList: [], userInfo: {}, - Qrcode: '' + Qrcode: '', + voiceList: [], // 音色列表 + defaultVoiceId: null // 默认音色ID }, mutations: { // 构造用于uv-picker的树状结构数组 @@ -71,6 +73,16 @@ const store = new Vuex.Store({ setQrcode(state, data) { state.Qrcode = data }, + setVoiceList(state, data) { + state.voiceList = data + // 如果有音色数据且还没有设置默认音色ID,则设置第一个音色为默认 + if (data && data.length > 0 && !state.defaultVoiceId) { + state.defaultVoiceId = Number(data[0].voiceType) + } + }, + setDefaultVoiceId(state, voiceId) { + state.defaultVoiceId = Number(voiceId) + }, // }, actions: { @@ -124,6 +136,7 @@ const store = new Vuex.Store({ try { await Promise.all([ dispatch('getConfig'), + dispatch('getVoiceList'), // 添加音色列表初始化 // dispatch('getDepartment'), // dispatch('getCategory'), ]) @@ -142,6 +155,27 @@ const store = new Vuex.Store({ commit('setUserInfo', res.result); } }, + // 获取音色列表 + async getVoiceList({ commit }) { + try { + const res = await api.music.list() + if (res.code === 200) { + console.log('音色列表加载成功:', res.result); + commit('setVoiceList', res.result) + return res.result + } else { + console.error('音色列表加载失败:', res); + return [] + } + } catch (error) { + console.error('音色列表加载异常:', error); + return [] + } + }, + // 设置默认音色 + setDefaultVoice({ commit }, voiceId) { + commit('setDefaultVoiceId', voiceId) + }, } }) diff --git a/subPages/home/AudioControls.vue b/subPages/home/AudioControls.vue index fd55403..feef161 100644 --- a/subPages/home/AudioControls.vue +++ b/subPages/home/AudioControls.vue @@ -287,9 +287,9 @@ export default { } } - // 如果所有音频都是导语,返回第一个音频的索引 - console.log('🎵 findFirstNonLeadAudio: 所有音频都是导语,返回第一个音频索引=0'); - return 0; + // 如果所有音频都是导语,返回 -1 表示不播放 + console.log('🎵 findFirstNonLeadAudio: 所有音频都是导语,返回 -1 不播放'); + return -1; }, // 检查并自动加载预加载完成的音频 @@ -324,7 +324,7 @@ export default { // 初始化音频索引为第一个非导语音频 const firstNonLeadIndex = this.findFirstNonLeadAudio(); - this.currentAudioIndex = firstNonLeadIndex >= 0 ? firstNonLeadIndex : 0; + this.currentAudioIndex = firstNonLeadIndex; this.currentTime = 0; this.currentHighlightIndex = -1; @@ -618,7 +618,7 @@ export default { // 初始化音频索引为第一个非导语音频 const firstNonLeadIndex = this.findFirstNonLeadAudio(); - this.currentAudioIndex = firstNonLeadIndex >= 0 ? firstNonLeadIndex : 0; + this.currentAudioIndex = firstNonLeadIndex; this.isPlaying = false; this.currentTime = 0; this.hasAudioData = true; @@ -932,10 +932,10 @@ export default { // 检查音色ID是否匹配 if (cachedData.voiceId && cachedData.voiceId !== this.localVoiceId) { console.warn('缓存音色不匹配:', cachedData.voiceId, '!=', this.localVoiceId); - uni.showToast({ - title: '音色已切换,请重新获取音频', - icon: 'none' - }); + // uni.showToast({ + // title: '音色已切换,请重新获取音频', + // icon: 'none' + // }); return; } @@ -982,10 +982,10 @@ export default { // 检查是否有音色ID if (!this.localVoiceId) { - uni.showToast({ - title: '音色未加载,请稍后重试', - icon: 'none' - }); + // uni.showToast({ + // title: '音色未加载,请稍后重试', + // icon: 'none' + // }); return; } @@ -2112,16 +2112,12 @@ export default { } } - - - - // 显示成功提示 - uni.showToast({ - title: '✅ 倍速功能可用', - icon: 'none', - duration: 1500 - }); + // uni.showToast({ + // title: '✅ 倍速功能可用', + // icon: 'none', + // duration: 1500 + // }); } catch (error) { console.error('💥 检测播放速度支持时出错:', error); @@ -2829,12 +2825,18 @@ export default { // 查找第一个非导语音频 const firstNonLeadIndex = this.findFirstNonLeadAudio(); - this.currentAudioIndex = firstNonLeadIndex >= 0 ? firstNonLeadIndex : 0; + this.currentAudioIndex = firstNonLeadIndex; this.currentTime = 0; this.isPlaying = false; console.log(`🎵 autoPlayCachedAudio: 设置起始索引为${this.currentAudioIndex}(跳过导语)`); + // 如果所有音频都是导语,不播放 + if (firstNonLeadIndex < 0) { + console.log('🎵 autoPlayCachedAudio: 所有音频都是导语,不播放'); + return; + } + // 延迟一下再开始播放,确保UI更新完成 setTimeout(() => { this.playAudio(); diff --git a/subPages/home/articleDetail.vue b/subPages/home/articleDetail.vue index fba6230..a8875a7 100644 --- a/subPages/home/articleDetail.vue +++ b/subPages/home/articleDetail.vue @@ -144,7 +144,7 @@ export default { if (res.result.audios && Object.keys(res.result.audios).length > 0) { // 获取第一个音频URL const audioKeys = Object.keys(res.result.audios) - this.audioUrl = res.result.audios[audioKeys[0]] + this.audioUrl = res.result.audios['501008'] || res.result.audios[audioKeys[0]] this.hasAudio = true } } else { diff --git a/subPages/home/book.vue b/subPages/home/book.vue index 50990cd..c8ec7cb 100644 --- a/subPages/home/book.vue +++ b/subPages/home/book.vue @@ -221,11 +221,22 @@ export default { displayCourseList() { return this.isReversed ? [...this.courseList].reverse() : this.courseList; }, + + // 从Vuex获取音色列表 + voiceList() { + return this.$store.state.voiceList; + }, + + // 从Vuex获取默认音色ID + defaultVoiceId() { + return this.$store.state.defaultVoiceId; + }, + // 判断当前页面是否为文字类型 isTextPage() { // 如果是卡片页面(type为'1'),不显示音频控制栏 if (this.currentPageType === '1') { - return false; + return true; } const currentPageData = this.bookPages[this.currentPage - 1]; @@ -1183,15 +1194,37 @@ export default { // 获取音色列表 拿第一个做默认的音色id async getVoiceList() { - const voiceRes = await this.$api.music.list() - if (voiceRes.code === 200) { - // console.log('音色列表API返回:', voiceRes.result); - // console.log('第一个音色数据:', voiceRes.result[0]); - this.voiceId = Number(voiceRes.result[0].voiceType) - // console.log('获取默认音色ID:', this.voiceId, '类型:', typeof this.voiceId); - + // 优先从Vuex获取音色列表 + if (this.voiceList && this.voiceList.length > 0) { + console.log('从Vuex获取音色列表:', this.voiceList); + this.voiceId = this.defaultVoiceId || Number(this.voiceList[0].voiceType); + console.log('使用Vuex中的默认音色ID:', this.voiceId); + // 同步默认音色设置到audioManager audioManager.setGlobalVoiceId(this.voiceId); + return; + } + + // 如果Vuex中没有数据,则从API获取(兜底方案) + console.log('Vuex中无音色数据,从API获取...'); + try { + const voiceRes = await this.$api.music.list() + if (voiceRes.code === 200) { + console.log('音色列表API返回:', voiceRes.result); + + // 更新Vuex中的音色列表 + this.$store.commit('setVoiceList', voiceRes.result); + + this.voiceId = Number(voiceRes.result[0].voiceType) + console.log('获取默认音色ID:', this.voiceId, '类型:', typeof this.voiceId); + + // 同步默认音色设置到audioManager + audioManager.setGlobalVoiceId(this.voiceId); + } else { + console.error('获取音色列表失败:', voiceRes); + } + } catch (error) { + console.error('获取音色列表异常:', error); } }, toggleNavbar() { diff --git a/subPages/home/music.vue b/subPages/home/music.vue index edbb78f..8cd1f04 100644 --- a/subPages/home/music.vue +++ b/subPages/home/music.vue @@ -60,6 +60,12 @@ export default { ] } }, + computed: { + // 从Vuex获取音色列表 + storeVoiceList() { + return this.$store.state.voiceList; + } + }, methods: { goBack() { uni.navigateBack() @@ -71,12 +77,30 @@ export default { }, confirmSelection() { this.selectVoice(this.selectedVoiceId) - // 这里可以保存选择的音色到本地存储或发送到服务器 + // 保存选择的音色到Vuex + this.$store.dispatch('setDefaultVoice', this.selectedVoiceId); }, async getVoice(){ - const listRes = await this.$api.music.list() - if (listRes.code === 200) { - this.voiceList = listRes.result + // 优先从Vuex获取音色列表 + if (this.storeVoiceList && this.storeVoiceList.length > 0) { + console.log('从Vuex获取音色列表:', this.storeVoiceList); + this.voiceList = this.storeVoiceList; + return; + } + + // 如果Vuex中没有数据,则从API获取(兜底方案) + console.log('Vuex中无音色数据,从API获取...'); + try { + const listRes = await this.$api.music.list() + if (listRes.code === 200) { + this.voiceList = listRes.result + // 更新Vuex中的音色列表 + this.$store.commit('setVoiceList', listRes.result); + } else { + console.error('获取音色列表失败:', listRes); + } + } catch (error) { + console.error('获取音色列表异常:', error); } } }, diff --git a/utils/share.js b/utils/share.js index 96a1aae..a87fd4d 100644 --- a/utils/share.js +++ b/utils/share.js @@ -94,4 +94,5 @@ function addQueryParams(url) { } } -export default share \ No newline at end of file +export default function(){} +// export default share \ No newline at end of file