index.js 4.0 KB

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