index.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import {
  2. getPkResult
  3. } from '~/api/works'
  4. let innerAudioContext
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. pkId: '',
  11. pkRecord: {},
  12. vState: false,
  13. vStart: '00:00',
  14. vEnd: '00:00',
  15. vProgress: 0,
  16. dState: false,
  17. dStart: '00:00',
  18. dEnd: '00:00',
  19. dProgress: 0,
  20. currentType: '',
  21. victory: {},
  22. defeated: {},
  23. equal: false,
  24. // 是否是分享进来的
  25. isPlayback: false
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. async onLoad(options) {
  31. let {
  32. pkRecord,
  33. pkRecordVOS
  34. } = await getPkResult(options.id)
  35. this.setData({
  36. pkId: options.id,
  37. pkRecord,
  38. isplayback: options.playback || false
  39. })
  40. this.compareScore(pkRecordVOS)
  41. this.innerAudioContext = wx.createInnerAudioContext()
  42. },
  43. compareScore(resultData) {
  44. let first = resultData[0]
  45. let second = resultData[1]
  46. let victory = first.userRead.score > second.userRead.score ? first : second
  47. let defeated = second.userRead.score <= first.userRead.score ? second : first
  48. this.setData({
  49. equal: first.userRead.score == second.userRead.score,
  50. victory: first.userRead.score > second.userRead.score ? first : second,
  51. defeated,
  52. vEnd: this.setDuration(victory.userRead.duration),
  53. dEnd: this.setDuration(defeated.userRead.duration),
  54. })
  55. },
  56. playAudio({
  57. currentTarget
  58. }) {
  59. let type = currentTarget.dataset.type
  60. // 重置音频对象
  61. if (type != this.data.currentType) {
  62. this.innerAudioContext.stop();
  63. }
  64. // 处理音频播放
  65. if (type == 'victory' && !this.data.vState) {
  66. if (this.data.currentType != 'victory') {
  67. this.innerAudioContext.src = this.data.victory.userRead.audioPath
  68. }
  69. this.setData({
  70. vState: true,
  71. dState: false
  72. })
  73. } else if (type == 'victory' && this.data.vState) {
  74. this.innerAudioContext.pause();
  75. return this.setData({
  76. vState: false
  77. })
  78. } else if (type == 'defeated' && !this.data.dState) {
  79. if (this.data.currentType != 'defeated') {
  80. this.innerAudioContext.src = this.data.defeated.userRead.audioPath;
  81. }
  82. this.setData({
  83. dState: true,
  84. vState: false
  85. })
  86. } else if (type == 'defeated' && this.data.dState) {
  87. this.innerAudioContext.pause();
  88. return this.setData({
  89. dState: false
  90. })
  91. }
  92. this.setData({
  93. currentType: type
  94. })
  95. // this.innerAudioContext.onCanplay(() => {
  96. this.innerAudioContext.play();
  97. // })
  98. this.innerAudioContext.onTimeUpdate(() => {
  99. let label = this.data.currentType == 'victory' ? 'vStart' : 'dStart'
  100. let progressV = this.data.currentType == 'victory' ? 'vProgress' : 'dProgress'
  101. this.setData({
  102. [label]: this.setDuration(this.innerAudioContext.currentTime),
  103. [progressV]: Math.round((this.innerAudioContext.currentTime / this.innerAudioContext.duration) * 100)
  104. })
  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. return t
  124. },
  125. result() {
  126. wx.redirectTo({
  127. url: `/pages/reading/index?videoId=${this.data.victory.userRead.exampleId}&readingType=pk`,
  128. })
  129. },
  130. /**
  131. * 生命周期函数--监听页面卸载
  132. */
  133. onUnload() {
  134. this.innerAudioContext.destroy()
  135. },
  136. creatShare() {
  137. return new Promise((resolve, reject) => {
  138. let context = wx.createSelectorQuery();
  139. context
  140. .select('#share')
  141. .fields({
  142. node: true,
  143. size: true
  144. }).exec((res) => {
  145. const canvas = res[0].node;
  146. const ctx = canvas.getContext('2d');
  147. const dpr = wx.getSystemInfoSync().pixelRatio;
  148. canvas.width = res[0].width * dpr;
  149. canvas.height = res[0].height * dpr;
  150. ctx.scale(dpr, dpr);
  151. ctx.font = '16px PingFang';
  152. let bgImg = canvas.createImage();
  153. bgImg.src = this.data.equal ? 'https://reader-wx.ai160.com/images/reader/v3/equal.png' : 'https://reader-wx.ai160.com/images/reader/v3/victory.png'
  154. bgImg.onload = () => {
  155. ctx.drawImage(bgImg, 0, 0, 375, 300);
  156. ctx.textAlign = "center";
  157. var lnamex = this.data.equal ? 100 : 100,
  158. lnamey = this.data.equal ? 125 : 115;
  159. ctx.fillText(this.data.victory.user.nickName, lnamex, lnamey)
  160. var rnamex = this.data.equal ? 280 : 280,
  161. rnamey = this.data.equal ? 125 : 140;
  162. ctx.fillText(this.data.defeated.user.nickName, rnamex, rnamey)
  163. ctx.font = '19px PingFang';
  164. var lnumx = this.data.equal ? 100 : 100,
  165. lnumy = this.data.equal ? 150 : 140;
  166. ctx.fillText(this.data.victory.userRead.score + '分', lnumx, lnumy)
  167. var rnumx = this.data.equal ? 280 : 280,
  168. rnumy = this.data.equal ? 150 : 165;
  169. ctx.fillText(this.data.defeated.userRead.score + '分', rnumx, rnumy)
  170. ctx.font = '13px PingFang';
  171. var ltimex = this.data.equal ? 136 : 136,
  172. ltimey = this.data.equal ? 252.5 : 239.5;
  173. ctx.fillText(this.data.vEnd, ltimex, ltimey)
  174. var rtimex = this.data.equal ? 320 : 320,
  175. rtimey = this.data.equal ? 253 : 267;
  176. ctx.fillText(this.data.dEnd, rtimex, rtimey)
  177. // 圆形位置 大小
  178. var size = 55;
  179. var lx = this.data.equal ? 71 : 72,
  180. ly = this.data.equal ? 45 : 38;
  181. var rx = this.data.equal ? 252 : 247,
  182. ry = this.data.equal ? 45 : 60;
  183. ctx.save(); // 保存
  184. ctx.arc(size / 2 + lx, size / 2 + ly, size / 2, 0, Math.PI * 2, false);
  185. ctx.arc(size / 2 + rx, size / 2 + ry, size / 2, 0, Math.PI * 2, false);
  186. ctx.clip();
  187. let leftImg = canvas.createImage();
  188. leftImg.src = this.data.victory.user.avatar
  189. leftImg.onerror = () => {
  190. loadRightImg()
  191. }
  192. leftImg.onload = () => {
  193. ctx.drawImage(leftImg, lx, ly, size, size)
  194. loadRightImg()
  195. }
  196. let loadRightImg = () => {
  197. let rightImg = canvas.createImage();
  198. rightImg.src = this.data.defeated.user.avatar
  199. rightImg.onload = () => {
  200. ctx.drawImage(rightImg, rx, ry, size, size)
  201. setTimeout(() => {
  202. wx.canvasToTempFilePath({
  203. canvas: canvas,
  204. success: (res) => {
  205. resolve({
  206. title: '请欣赏我的课文朗读作品,点赞+评论。',
  207. path: `/pages/pkResult/index?id=${this.data.pkId}&uid=${wx.getStorageSync('uid')}&playback=true`,
  208. imageUrl: res.tempFilePath
  209. })
  210. },
  211. fail(res) {
  212. reject()
  213. }
  214. }, this)
  215. }, 500)
  216. }
  217. rightImg.onerror = () => {
  218. setTimeout(() => {
  219. wx.canvasToTempFilePath({
  220. canvas: canvas,
  221. success(res) {
  222. resolve({
  223. title: '请欣赏我的课文朗读作品,点赞+评论。',
  224. path: `/pages/pkResult/index?uid=${wx.getStorageSync('uid')}`,
  225. imageUrl: res.tempFilePath
  226. })
  227. },
  228. fail(res) {
  229. reject()
  230. }
  231. }, this)
  232. }, 500)
  233. }
  234. }
  235. }
  236. })
  237. })
  238. },
  239. onShareAppMessage({
  240. from,
  241. target
  242. }) {
  243. if (from == 'button') {
  244. const promise = new Promise(resolve => {
  245. this.creatShare().then(res => {
  246. resolve(res)
  247. })
  248. })
  249. return {
  250. title: '课文朗读,从未如此有趣。',
  251. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  252. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png',
  253. promise
  254. }
  255. } else {
  256. return {
  257. title: '课文朗读,从未如此有趣。',
  258. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  259. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  260. }
  261. }
  262. },
  263. })