index.js 9.4 KB

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