index.js 12 KB

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