index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. currentTarget
  127. }) {
  128. if (currentTarget.dataset.type == 'reading') {
  129. wx.redirectTo({
  130. url: `/pages/reading/index?videoId=${this.data.victory.userRead.exampleId}&readingType=pk`,
  131. })
  132. } else {
  133. wx.switchTab({
  134. url: `/pages/index/index`,
  135. })
  136. }
  137. },
  138. /**
  139. * 生命周期函数--监听页面卸载
  140. */
  141. onUnload() {
  142. this.innerAudioContext.destroy()
  143. },
  144. creatShare() {
  145. return new Promise((resolve, reject) => {
  146. let context = wx.createSelectorQuery();
  147. context
  148. .select('#share')
  149. .fields({
  150. node: true,
  151. size: true
  152. }).exec((res) => {
  153. const canvas = res[0].node;
  154. const ctx = canvas.getContext('2d');
  155. const dpr = wx.getSystemInfoSync().pixelRatio;
  156. canvas.width = res[0].width * dpr;
  157. canvas.height = res[0].height * dpr;
  158. ctx.scale(dpr, dpr);
  159. ctx.font = '16px PingFang';
  160. let bgImg = canvas.createImage();
  161. 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'
  162. bgImg.onload = () => {
  163. ctx.drawImage(bgImg, 0, 0, 375, 300);
  164. ctx.textAlign = "center";
  165. var lnamex = this.data.equal ? 100 : 100,
  166. lnamey = this.data.equal ? 125 : 115;
  167. ctx.fillText(this.data.victory.user.nickName, lnamex, lnamey)
  168. var rnamex = this.data.equal ? 280 : 280,
  169. rnamey = this.data.equal ? 125 : 140;
  170. ctx.fillText(this.data.defeated.user.nickName, rnamex, rnamey)
  171. ctx.font = '19px PingFang';
  172. var lnumx = this.data.equal ? 100 : 100,
  173. lnumy = this.data.equal ? 150 : 140;
  174. ctx.fillText(this.data.victory.userRead.score + '分', lnumx, lnumy)
  175. var rnumx = this.data.equal ? 280 : 280,
  176. rnumy = this.data.equal ? 150 : 165;
  177. ctx.fillText(this.data.defeated.userRead.score + '分', rnumx, rnumy)
  178. ctx.font = '13px PingFang';
  179. var ltimex = this.data.equal ? 136 : 136,
  180. ltimey = this.data.equal ? 252.5 : 239.5;
  181. ctx.fillText(this.data.vEnd, ltimex, ltimey)
  182. var rtimex = this.data.equal ? 320 : 320,
  183. rtimey = this.data.equal ? 253 : 267;
  184. ctx.fillText(this.data.dEnd, rtimex, rtimey)
  185. // 圆形位置 大小
  186. var size = 55;
  187. var lx = this.data.equal ? 71 : 72,
  188. ly = this.data.equal ? 45 : 38;
  189. var rx = this.data.equal ? 252 : 247,
  190. ry = this.data.equal ? 45 : 60;
  191. ctx.save(); // 保存
  192. ctx.arc(size / 2 + lx, size / 2 + ly, size / 2, 0, Math.PI * 2, false);
  193. ctx.arc(size / 2 + rx, size / 2 + ry, size / 2, 0, Math.PI * 2, false);
  194. ctx.clip();
  195. let leftImg = canvas.createImage();
  196. leftImg.src = this.data.victory.user.avatar
  197. leftImg.onerror = () => {
  198. loadRightImg()
  199. }
  200. leftImg.onload = () => {
  201. ctx.drawImage(leftImg, lx, ly, size, size)
  202. loadRightImg()
  203. }
  204. let loadRightImg = () => {
  205. let rightImg = canvas.createImage();
  206. rightImg.src = this.data.defeated.user.avatar
  207. rightImg.onload = () => {
  208. ctx.drawImage(rightImg, rx, ry, size, size)
  209. setTimeout(() => {
  210. wx.canvasToTempFilePath({
  211. canvas: canvas,
  212. success: (res) => {
  213. let userName = this.data.pkRecord.userReadId == this.data.victory.userRead.id ? this.data.victory.user.nickName || this.data.victory.user.eid : this.data.defeated.user.nickName || this.data.defeated.user.eid
  214. if (userName.length > 4) {
  215. userName = userName.slice(0, 4) + '...'
  216. }
  217. resolve({
  218. title: `我挑战了${userName}的作品,这场比拼真精彩!点击加入战局!`,
  219. path: `/pages/pkResult/index?id=${this.data.pkId}&uid=${wx.getStorageSync('uid')}&playback=true`,
  220. imageUrl: res.tempFilePath
  221. })
  222. },
  223. fail(res) {
  224. reject()
  225. }
  226. }, this)
  227. }, 500)
  228. }
  229. rightImg.onerror = () => {
  230. setTimeout(() => {
  231. wx.canvasToTempFilePath({
  232. canvas: canvas,
  233. success(res) {
  234. resolve({
  235. title: '请欣赏我的课文朗读作品,点赞+评论。',
  236. path: `/pages/pkResult/index?uid=${wx.getStorageSync('uid')}`,
  237. imageUrl: res.tempFilePath
  238. })
  239. },
  240. fail(res) {
  241. reject()
  242. }
  243. }, this)
  244. }, 500)
  245. }
  246. }
  247. }
  248. })
  249. })
  250. },
  251. onShareAppMessage({
  252. from,
  253. target
  254. }) {
  255. if (from == 'button') {
  256. const promise = new Promise(resolve => {
  257. this.creatShare().then(res => {
  258. resolve(res)
  259. })
  260. })
  261. return {
  262. title: '课文朗读,从未如此有趣。',
  263. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  264. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png',
  265. promise
  266. }
  267. } else {
  268. return {
  269. title: '课文朗读,从未如此有趣。',
  270. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  271. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  272. }
  273. }
  274. },
  275. })