index.js 9.1 KB

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