beefast-mini-deliveryman/components/background-notice/index.js

104 lines
2.6 KiB
JavaScript

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/static/logo_large.jpg';
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.bgam){
this.bgam.stop();
}
},
notice(){
this.bgam.src = this.data.haveOrderAudio;
this.bgam.onEnded(()=>{
this.bgam.src = this.data.emptyAudio;
});
// console.log('notice order');
// const innerAudioContext = wx.createInnerAudioContext({
// useWebAudioImplement:true
// })
// innerAudioContext.src = this.data.haveOrderAudio;
// innerAudioContext.play();
// innerAudioContext.onEnded(()=>{
// innerAudioContext.destroy();
// })
}
}
})