index.js 12 KB

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