123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import {
- createStoreBindings
- } from 'mobx-miniprogram-bindings'
- import {
- store
- } from '~/store/index'
- let innerAudioContext
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- vState: false,
- vStart: '00:00',
- vEnd: '00:00',
- vProgress: 0,
- dState: false,
- dStart: '00:00',
- dEnd: '00:00',
- dProgress: 0,
- currentType: '',
- victory: {},
- defeated: {},
- equal: false
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- this.storeBindings = createStoreBindings(this, {
- store,
- fields: {
- userInfo: 'userInfo',
- readDetail: 'readDetail',
- pkData: 'pkData'
- },
- })
- this.storeBindings.updateStoreBindings()
- this.compareScore()
- this.innerAudioContext = wx.createInnerAudioContext()
- },
- compareScore() {
- /* let pkData = {
- audioPath: "https://reader-wx.ai160.com/audio/reader/103138024.mp3",
- avatar: "wxfile://,tmp_ba795553cc21cd941badc3ce06597c245f08fd6a674fe165.jpg",
- nickName: "小舞",
- score: 96,
- } */
- let pkData = this.data.pkData
- let score = pkData.score
- let myResult = {
- audioPath: this.data.readDetail.tempFilePath,
- nickName: this.data.userInfo.nickName || this.data.userInfo.uid,
- avatar: this.data.userInfo.avatar,
- score: this.data.readDetail.myOverall
- }
- this.setData({
- equal: score == myResult.score,
- victory: myResult.score > score ? myResult : pkData,
- defeated: myResult.score <= score ? myResult : pkData,
- })
- },
- playAudio({
- currentTarget
- }) {
- let type = currentTarget.dataset.type
- // 重置音频对象
- if (type != this.data.currentType) {
- this.innerAudioContext.stop();
- }
- // 处理音频播放
- if (type == 'victory' && !this.data.vState) {
- if (this.data.currentType != 'victory') {
- this.innerAudioContext.src = this.data.victory.audioPath
- }
- this.setData({
- vState: true,
- dState: false
- })
- } else if (type == 'victory' && this.data.vState) {
- this.innerAudioContext.pause();
- return this.setData({
- vState: false
- })
- } else if (type == 'defeated' && !this.data.dState) {
- if (this.data.currentType != 'defeated') {
- this.innerAudioContext.src = this.data.defeated.audioPath;
- }
- this.setData({
- dState: true,
- vState: false
- })
- } else if (type == 'defeated' && this.data.dState) {
- this.innerAudioContext.pause();
- return this.setData({
- dState: false
- })
- }
- this.setData({
- currentType: type
- })
- // this.innerAudioContext.onCanplay(() => {
- this.innerAudioContext.play();
- // })
- this.innerAudioContext.onTimeUpdate(() => {
- this.setDuration(this.innerAudioContext.currentTime)
- })
- },
- // 设置时间文案
- setDuration(s) {
- let t = '';
- s = Math.floor(s);
- if (s > -1) {
- let min = Math.floor(s / 60) % 60;
- let sec = s % 60;
- if (min < 10) {
- t += "0";
- }
- t += min + ":";
- if (sec < 10) {
- t += "0";
- }
- t += sec;
- }
- let label = this.data.currentType == 'victory' ? 'vStart' : 'dStart'
- let progressV = this.data.currentType == 'victory' ? 'vProgress' : 'dProgress'
- this.setData({
- [label]: t,
- [progressV]: Math.round((this.innerAudioContext.currentTime / this.innerAudioContext.duration) * 100)
- })
- },
- result() {
- wx.redirectTo({
- url: `/pages/reading/index?videoId=${this.data.pkData.exampleId}&readingType=pk&reset=true`,
- })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- this.innerAudioContext.destroy()
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- }
- })
|