一、全局引入jweixin-1.6.0.js微信能力插件;
二、初始化微信配置,开启音频相关能力;
initWxConfig() {
let url = window.location.href.split('#')[0]
let data = { url: url }
this.$http.get(this.$api.getTicket, { params: data }).then((res) => {
let data = res.data || {}
if (data.flag) {
this.$nextTick(() => {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.noncestr,
signature: data.signature,
jsApiList: ['startRecord', 'stopRecord', 'playVoice', 'stopVoice', 'uploadVoice', 'translateVoice']
})
})
}
})
},
三、录音上传评测;
wx.startRecord();
let _this = this
wx.stopRecord({
success: function (res) {
_this.localId = res.localId
wx.uploadVoice({
localId: res.localId, // 需要上传的音频的本地ID,由stopRecord接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
_this.serverId = res.serverId // 返回音频的服务器端ID
let params = {
questionId: _this.paper.id,
mediaId: res.serverId,
paperId: _this.paper.paperId
}
// loading
_this.$http
.post(_this.$api.audioCheck, params)
.then((res) => {
_this.nextFlag = true //通过
})
.finally(() => {
Toast.clear()
})
}
})
}
})
四、应用置于后台,定时器、延时器由于浏览器性能机制有所沉睡;采用visibilitychange及时处理;
document.removeEventListener("visibilitychange", this.checkViChange);
checkViChange(){
if (document.hidden) { // 隐藏
this.cacheHiddenTime = moment().unix();
if(this.setInterval){
clearInterval(this.setInterval);
this.setInterval = null;
};
}else{
let cur = moment().unix() - this.cacheHiddenTime;
this.countdown = this.countdown>cur ? (this.countdown-cur) : 0;
this.countDownUtil();
}
},