UNPKG

sqb-util

Version:

sqb-util,是送气宝系列的共用工具套

171 lines (151 loc) 3.83 kB
/** * 语音播报 */ const audioObject = { success: 'https://img.songqibao.com/web_static/common/audio/success.mp3', error: 'https://img.songqibao.com/web_static/common/audio/error.mp3', scanFeedback: 'https://img.songqibao.com/web_static/common/audio/scanFeedback.mp3', returnSuccess: 'https://img.songqibao.com/web_static/common/audio/returnSuccess.mp3', scanSuccess: 'https://img.songqibao.com/web_static/common/audio/scanSuccess.mp3', takeSuccess: 'https://img.songqibao.com/web_static/common/audio/takeSuccess.mp3', recycleSuccess: 'https://img.songqibao.com/web_static/common/audio/recycleSuccess.mp3', recallSuccess: 'https://img.songqibao.com/web_static/common/audio/recallSuccess.mp3', receiveSuccess: 'https://img.songqibao.com/web_static/common/audio/receiveSuccess.mp3', paySuccess: 'https://img.songqibao.com/web_static/common/audio/paySuccess.mp3' } class Voice { static instance constructor() { if (Voice.instance) { return Voice.instance } Voice.instance = this this.audioContext = null this.init() } init() { this.audioContext = uni.createInnerAudioContext({ useWebAudioImplement: true }) this.audioContext.autoplay = false this.audioContext.volume = 1 // #ifndef APP-PLUS this.audioContext.obeyMuteSwitch = false // #endif } play(options = {}) { const { type, onPlay, onEnded, vibrate } = options this.audioContext.src = audioObject[type] if (vibrate) { uni.vibrateShort() } this.audioContext.play() this.audioContext.onPlay(() => { onPlay && onPlay() }) this.audioContext.onEnded(() => { this.audioContext.stop() onEnded && onEnded() }) } /** * 成功 * @param {Object} options */ success(options, vibrate = false) { this.play({ type: 'success', ...options, vibrate }) } /** * 出错啦 * @param {Object} options */ error(options, vibrate = true) { this.play({ type: 'error', ...options, vibrate }) } /** * 扫码反馈 * @param {Object} options */ scanFeedback(options) { this.play({ type: 'scanFeedback', ...options }) } /** * 提取成功 * @param {Object} options */ takeSuccess(options) { this.play({ type: 'takeSuccess', ...options }) } /** * 提取成功 * @param {Object} options */ returnSuccess(options) { this.play({ type: 'returnSuccess', ...options }) } /** * 提取成功 * @param {Object} options */ scanSuccess(options) { this.play({ type: 'scanSuccess', ...options }) } /** * 回收成功 */ recycleSuccess(options) { this.play({ type: 'recycleSuccess', ...options }) } /** * 撤销成功 */ recallSuccess(options) { this.play({ type: 'recallSuccess', ...options }) } /** * 接收成功 */ receiveSuccess(options) { this.play({ type: 'receiveSuccess', ...options }) } /** * 支付成功 */ paySuccess(options) { this.play({ type: 'paySuccess', vibrate: true, ...options }) } } export default new Voice()