index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. console.log(myResult, score);
  58. this.setData({
  59. equal: score == myResult.score,
  60. victory: myResult.score > score ? myResult : pkData,
  61. defeated: myResult.score <= score ? myResult : pkData,
  62. })
  63. },
  64. playAudio({
  65. currentTarget
  66. }) {
  67. let type = currentTarget.dataset.type
  68. // 重置音频对象
  69. if (type != this.data.currentType) {
  70. this.innerAudioContext.stop();
  71. }
  72. // 处理音频播放
  73. if (type == 'victory' && !this.data.vState) {
  74. if (this.data.currentType != 'victory') {
  75. this.innerAudioContext.src = this.data.victory.audioPath
  76. }
  77. this.setData({
  78. vState: true,
  79. dState: false
  80. })
  81. } else if (type == 'victory' && this.data.vState) {
  82. this.innerAudioContext.pause();
  83. return this.setData({
  84. vState: false
  85. })
  86. } else if (type == 'defeated' && !this.data.dState) {
  87. if (this.data.currentType != 'defeated') {
  88. this.innerAudioContext.src = this.data.defeated.audioPath;
  89. }
  90. this.setData({
  91. dState: true,
  92. vState: false
  93. })
  94. } else if (type == 'defeated' && this.data.dState) {
  95. this.innerAudioContext.pause();
  96. return this.setData({
  97. dState: false
  98. })
  99. }
  100. this.setData({
  101. currentType: type
  102. })
  103. // this.innerAudioContext.onCanplay(() => {
  104. this.innerAudioContext.play();
  105. // })
  106. this.innerAudioContext.onTimeUpdate(() => {
  107. this.setDuration(this.innerAudioContext.currentTime)
  108. })
  109. },
  110. // 设置时间文案
  111. setDuration(s) {
  112. let t = '';
  113. s = Math.floor(s);
  114. if (s > -1) {
  115. let min = Math.floor(s / 60) % 60;
  116. let sec = s % 60;
  117. if (min < 10) {
  118. t += "0";
  119. }
  120. t += min + ":";
  121. if (sec < 10) {
  122. t += "0";
  123. }
  124. t += sec;
  125. }
  126. let label = this.data.currentType == 'victory' ? 'vStart' : 'dStart'
  127. let progressV = this.data.currentType == 'victory' ? 'vProgress' : 'dProgress'
  128. this.setData({
  129. [label]: t,
  130. [progressV]: Math.round((this.innerAudioContext.currentTime / this.innerAudioContext.duration) * 100)
  131. })
  132. },
  133. result() {
  134. wx.redirectTo({
  135. url: `/pages/reading/index?videoId=${this.data.pkData.exampleId}&readingType=pk`,
  136. })
  137. },
  138. /**
  139. * 生命周期函数--监听页面隐藏
  140. */
  141. onHide() {
  142. },
  143. /**
  144. * 生命周期函数--监听页面卸载
  145. */
  146. onUnload() {
  147. this.innerAudioContext.destroy()
  148. },
  149. /**
  150. * 页面相关事件处理函数--监听用户下拉动作
  151. */
  152. onPullDownRefresh() {
  153. },
  154. /**
  155. * 页面上拉触底事件的处理函数
  156. */
  157. onReachBottom() {
  158. },
  159. /**
  160. * 用户点击右上角分享
  161. */
  162. onShareAppMessage() {
  163. }
  164. })