import userApi from '../../api/user'; Component({ /** * 组件的属性列表 */ properties: { start:{ type:Boolean, value:false } }, /** * 组件的初始数据 */ data: { emptyAudio:'', haveOrderAudio:'', initing:true }, lifetimes:{ attached(){ this.data.initing = true; this.downloadResource().then(()=>{ this.bgam = wx.getBackgroundAudioManager(); this.bgam.title = '后台通知'; this.bgam.audioType = 'music'; this.bgam.coverImgUrl = 'https://dman-1311994147.cos.ap-chengdu.myqcloud.com/20250227/DoADoEhKi4U4ab75d180b27fa14b7c34ae7969762809_d7ee9018-828e-43c7-8b2b-29d4352c0277.jpg?imageMogr2/thumbnail/800x800/format/webp'; this.bgam.onEnded(()=>{ this.bgam.src = this.data.emptyAudio; }); this.triggerEvent('initSuccess'); }).catch(()=>{ this.triggerEvent('initError'); }) } }, observers:{ "start"(s){ if(s){ this.start(); }else{ this.stop(); } } }, /** * 组件的方法列表 */ methods: { async downloadResource(){ this.data.emptyAudio = `${wx.env.USER_DATA_PATH}/empty.mp3`; const emptyServerPath = 'https://dman-1311994147.cos.ap-chengdu.myqcloud.com/static/silence_file.wav'; this.data.haveOrderAudio = `${wx.env.USER_DATA_PATH}/haveorder.wav`; const haveOrderPath = 'https://dman-1311994147.cos.ap-chengdu.myqcloud.com/static/new_order.mp3' await this.download(this.data.emptyAudio,emptyServerPath); await this.download(this.data.haveOrderAudio,haveOrderPath); }, async download(localPath,serverPath){ try { //判断文件是否存在 const fs = wx.getFileSystemManager(); fs.accessSync(localPath); } catch (error) { await userApi.downloadFile(serverPath,localPath); } }, start(){ this.bgam.src = this.data.emptyAudio; if(this.loopOrderTimer){ clearInterval(this.loopOrderTimer); } this.loopOrderTimer = setInterval(()=>{ this.triggerEvent('onTrigger'); },10000); }, stop(){ if(this.loopOrderTimer){ clearInterval(this.loopOrderTimer); } if(this.emptyAutioPlayTimer){ clearTimeout(this.emptyAutioPlayTimer) } if(this.bgam){ this.bgam.stop(); } }, notice(){ if(this.emptyAutioPlayTimer){ clearTimeout(this.emptyAutioPlayTimer) } // this.bgam.src = this.data.haveOrderAudio; // this.bgam.onEnded(()=>{ // console.log('notice onend'); // this.emptyAutioPlayTimer = setTimeout(()=>{ // this.bgam.src = this.data.emptyAudio; // },1000) // }); const innerAudioContext = wx.createInnerAudioContext({ useWebAudioImplement:true }) innerAudioContext.src = this.data.haveOrderAudio; innerAudioContext.play(); innerAudioContext.onEnded(()=>{ innerAudioContext.destroy(); }) } } })