135 lines
4.9 KiB
TypeScript
135 lines
4.9 KiB
TypeScript
Component({
|
|
properties: {
|
|
visible: { type: Boolean, value: false },
|
|
expanded: { type: Boolean, value: false },
|
|
loading: { type: Boolean, value: false },
|
|
wordDict: { type: Object, value: {} },
|
|
showBackIcon: { type: Boolean, value: false },
|
|
prototypeWord: { type: String, value: '' },
|
|
isWordEmptyResult: { type: Boolean, value: false },
|
|
dictDefaultTabValue: { type: String, value: '0' },
|
|
activeWordAudioType: { type: String, value: '' },
|
|
wordAudioPlaying: { type: Boolean, value: false },
|
|
wordAudioIconName: { type: String, value: 'sound' }
|
|
},
|
|
methods: {
|
|
onClose() {
|
|
const self = this as any
|
|
try { self.wordAudioContext && self.wordAudioContext.stop() } catch (e) {}
|
|
try {
|
|
if (self.wordAudioContext) {
|
|
self.wordAudioContext.offPlay()
|
|
self.wordAudioContext.offEnded()
|
|
self.wordAudioContext.offError()
|
|
self.wordAudioContext.destroy()
|
|
self.wordAudioContext = undefined
|
|
}
|
|
} catch (e) {}
|
|
if (self.wordAudioIconTimer) {
|
|
clearInterval(self.wordAudioIconTimer)
|
|
self.wordAudioIconTimer = undefined
|
|
}
|
|
self.setData({ wordAudioPlaying: false, wordAudioIconName: 'sound', activeWordAudioType: '' })
|
|
self.triggerEvent('close')
|
|
},
|
|
onMore() {
|
|
const self = this as any
|
|
self.triggerEvent('more')
|
|
},
|
|
onTabsChange(e: any) {
|
|
const self = this as any
|
|
self.triggerEvent('tabsChange', { value: e?.detail?.value })
|
|
},
|
|
onTabsClick(e: any) {
|
|
const self = this as any
|
|
self.triggerEvent('tabsClick', { value: e?.detail?.value })
|
|
},
|
|
onPlayWordAudio(e: any) {
|
|
const dataset = e?.currentTarget?.dataset || {}
|
|
const type = dataset.type
|
|
const audio = dataset.audio
|
|
const self = this as any
|
|
if (!audio) return
|
|
if (self.data.wordAudioPlaying && self.data.activeWordAudioType === type) {
|
|
try { self.wordAudioContext && self.wordAudioContext.stop() } catch (err) {}
|
|
try {
|
|
if (self.wordAudioContext) {
|
|
self.wordAudioContext.offPlay()
|
|
self.wordAudioContext.offEnded()
|
|
self.wordAudioContext.offError()
|
|
self.wordAudioContext.destroy()
|
|
self.wordAudioContext = undefined
|
|
}
|
|
} catch (err) {}
|
|
if (self.wordAudioIconTimer) {
|
|
clearInterval(self.wordAudioIconTimer)
|
|
self.wordAudioIconTimer = undefined
|
|
}
|
|
self.setData({ wordAudioPlaying: false, wordAudioIconName: 'sound', activeWordAudioType: '' })
|
|
return
|
|
}
|
|
try { self.wordAudioContext && self.wordAudioContext.stop() } catch (err) {}
|
|
try {
|
|
if (self.wordAudioContext) {
|
|
self.wordAudioContext.offPlay()
|
|
self.wordAudioContext.offEnded()
|
|
self.wordAudioContext.offError()
|
|
self.wordAudioContext.destroy()
|
|
self.wordAudioContext = undefined
|
|
}
|
|
} catch (err) {}
|
|
if (self.wordAudioIconTimer) {
|
|
clearInterval(self.wordAudioIconTimer)
|
|
self.wordAudioIconTimer = undefined
|
|
}
|
|
const audioUrl = `https://dict.youdao.com/dictvoice?audio=${audio}`
|
|
self.wordAudioContext = wx.createInnerAudioContext()
|
|
try { (self.wordAudioContext as any).autoplay = false } catch (err) {}
|
|
self.wordAudioContext.onPlay(() => {
|
|
const seq = ['sound', 'sound-low']
|
|
let i = 0
|
|
self.setData({ wordAudioPlaying: true, activeWordAudioType: type, wordAudioIconName: seq[0] })
|
|
self.wordAudioIconTimer = setInterval(() => {
|
|
i = (i + 1) % seq.length
|
|
self.setData({ wordAudioIconName: seq[i] })
|
|
}, 400) as any
|
|
})
|
|
const finalize = () => {
|
|
if (self.wordAudioIconTimer) {
|
|
clearInterval(self.wordAudioIconTimer)
|
|
self.wordAudioIconTimer = undefined
|
|
}
|
|
self.setData({ wordAudioPlaying: false, wordAudioIconName: 'sound', activeWordAudioType: '' })
|
|
try {
|
|
if (self.wordAudioContext) {
|
|
self.wordAudioContext.offPlay()
|
|
self.wordAudioContext.offEnded()
|
|
self.wordAudioContext.offError()
|
|
self.wordAudioContext.destroy()
|
|
self.wordAudioContext = undefined
|
|
}
|
|
} catch (err) {}
|
|
}
|
|
self.wordAudioContext.onEnded(() => { finalize() })
|
|
self.wordAudioContext.onError(() => {
|
|
wx.showToast({ title: '播放失败', icon: 'none' })
|
|
finalize()
|
|
})
|
|
self.wordAudioContext.src = audioUrl
|
|
self.wordAudioContext.play()
|
|
},
|
|
onBack() {
|
|
const self = this as any
|
|
self.triggerEvent('back')
|
|
},
|
|
onWordTap(e: any) {
|
|
const self = this as any
|
|
const dsWord = e && e.currentTarget && e.currentTarget.dataset ? e.currentTarget.dataset.word : ''
|
|
const propWord = (self.data && self.data.prototypeWord) ? self.data.prototypeWord : ''
|
|
const word = dsWord || propWord || ''
|
|
if (!word) return
|
|
self.triggerEvent('wordTap', { word })
|
|
}
|
|
}
|
|
})
|