index.js 9.2 KB

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