index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {
  2. createStoreBindings
  3. } from 'mobx-miniprogram-bindings'
  4. import {
  5. store
  6. } from '~/store/index'
  7. let innerAudioContext
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. vState: false,
  14. vStart: '00:00',
  15. vEnd: '00:00',
  16. vProgress: 0,
  17. dState: false,
  18. dStart: '00:00',
  19. dEnd: '00:00',
  20. dProgress: 0,
  21. currentType: '',
  22. victory: {},
  23. defeated: {},
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad(options) {
  29. this.storeBindings = createStoreBindings(this, {
  30. store,
  31. fields: {
  32. userInfo: 'userInfo',
  33. readDetail: 'readDetail',
  34. pkData: 'pkData'
  35. },
  36. })
  37. this.storeBindings.updateStoreBindings()
  38. this.compareScore()
  39. this.innerAudioContext = wx.createInnerAudioContext()
  40. },
  41. compareScore() {
  42. /* let pkData = {
  43. audioPath: "https://reader-wx.ai160.com/audio/reader/103138024.mp3",
  44. avatar: "wxfile://,tmp_ba795553cc21cd941badc3ce06597c245f08fd6a674fe165.jpg",
  45. nickName: "小舞",
  46. score: 96,
  47. } */
  48. let pkData = this.data.pkData
  49. let score = pkData.score
  50. let myResult = {
  51. audioPath: this.data.readDetail.tempFilePath,
  52. nickName: this.data.userInfo.nickName || this.data.userInfo.uid,
  53. avatar: this.data.userInfo.avatar,
  54. score: this.data.readDetail.myOverall
  55. }
  56. this.setData({
  57. victory: myResult.score > score ? myResult : pkData,
  58. defeated: myResult.score < score ? myResult : pkData,
  59. })
  60. },
  61. playAudio({
  62. currentTarget
  63. }) {
  64. let type = currentTarget.dataset.type
  65. // 重置音频对象
  66. if (type != this.data.currentType) {
  67. this.innerAudioContext.stop();
  68. }
  69. // 处理音频播放
  70. if (type == 'victory' && !this.data.vState) {
  71. if (this.data.currentType != 'victory') {
  72. this.innerAudioContext.src = this.data.victory.audioPath
  73. }
  74. this.setData({
  75. vState: true,
  76. dState: false
  77. })
  78. } else if (type == 'victory' && this.data.vState) {
  79. this.innerAudioContext.pause();
  80. return this.setData({
  81. vState: false
  82. })
  83. } else if (type == 'defeated' && !this.data.dState) {
  84. if (this.data.currentType != 'defeated') {
  85. this.innerAudioContext.src = this.data.defeated.audioPath;
  86. }
  87. this.setData({
  88. dState: true,
  89. vState: false
  90. })
  91. } else if (type == 'defeated' && this.data.dState) {
  92. this.innerAudioContext.pause();
  93. return this.setData({
  94. dState: false
  95. })
  96. }
  97. this.setData({
  98. currentType: type
  99. })
  100. // this.innerAudioContext.onCanplay(() => {
  101. this.innerAudioContext.play();
  102. // })
  103. this.innerAudioContext.onTimeUpdate(() => {
  104. this.setDuration(this.innerAudioContext.currentTime)
  105. })
  106. },
  107. // 设置时间文案
  108. setDuration(s) {
  109. let t = '';
  110. s = Math.floor(s);
  111. if (s > -1) {
  112. let min = Math.floor(s / 60) % 60;
  113. let sec = s % 60;
  114. if (min < 10) {
  115. t += "0";
  116. }
  117. t += min + ":";
  118. if (sec < 10) {
  119. t += "0";
  120. }
  121. t += sec;
  122. }
  123. let label = this.data.currentType == 'victory' ? 'vStart' : 'dStart'
  124. let progressV = this.data.currentType == 'victory' ? 'vProgress' : 'dProgress'
  125. this.setData({
  126. [label]: t,
  127. [progressV]: Math.round((this.innerAudioContext.currentTime / this.innerAudioContext.duration) * 100)
  128. })
  129. },
  130. result() {
  131. wx.redirectTo({
  132. url: `/pages/reading/index?videoId=${this.data.pkData.exampleId}&readingType=pk`,
  133. })
  134. },
  135. /**
  136. * 生命周期函数--监听页面隐藏
  137. */
  138. onHide() {
  139. },
  140. /**
  141. * 生命周期函数--监听页面卸载
  142. */
  143. onUnload() {
  144. this.innerAudioContext.destroy()
  145. },
  146. /**
  147. * 页面相关事件处理函数--监听用户下拉动作
  148. */
  149. onPullDownRefresh() {
  150. },
  151. /**
  152. * 页面上拉触底事件的处理函数
  153. */
  154. onReachBottom() {
  155. },
  156. /**
  157. * 用户点击右上角分享
  158. */
  159. onShareAppMessage() {
  160. }
  161. })