index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /* // 设置时间文案
  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. var vName = this.data.victory.user.nickName || this.data.victory.user.eid
  173. if (vName.length > 4) {
  174. vName = vName.slice(0, 4) + '...'
  175. }
  176. ctx.fillText(vName, lnamex, lnamey)
  177. var rnamex = this.data.equal ? 280 : 280,
  178. rnamey = this.data.equal ? 125 : 140;
  179. var dName = this.data.defeated.user.nickName || this.data.defeated.user.eid
  180. if (dName.length > 4) {
  181. dName = dName.slice(0, 4) + '...'
  182. }
  183. ctx.fillText(dName, rnamex, rnamey)
  184. ctx.font = '19px PingFang';
  185. var lnumx = this.data.equal ? 100 : 100,
  186. lnumy = this.data.equal ? 150 : 140;
  187. ctx.fillText(this.data.victory.userRead.score + '分', lnumx, lnumy)
  188. var rnumx = this.data.equal ? 280 : 280,
  189. rnumy = this.data.equal ? 150 : 165;
  190. ctx.fillText(this.data.defeated.userRead.score + '分', rnumx, rnumy)
  191. ctx.font = '13px PingFang';
  192. var ltimex = this.data.equal ? 136 : 136,
  193. ltimey = this.data.equal ? 252.5 : 239.5;
  194. ctx.fillText(this.data.vEnd, ltimex, ltimey)
  195. var rtimex = this.data.equal ? 320 : 320,
  196. rtimey = this.data.equal ? 253 : 267;
  197. ctx.fillText(this.data.dEnd, rtimex, rtimey)
  198. // 圆形位置 大小
  199. var size = 55;
  200. var lx = this.data.equal ? 71 : 72,
  201. ly = this.data.equal ? 45 : 38;
  202. var rx = this.data.equal ? 252 : 247,
  203. ry = this.data.equal ? 45 : 60;
  204. ctx.save(); // 保存
  205. ctx.arc(size / 2 + lx, size / 2 + ly, size / 2, 0, Math.PI * 2, false);
  206. ctx.arc(size / 2 + rx, size / 2 + ry, size / 2, 0, Math.PI * 2, false);
  207. ctx.clip();
  208. let leftImg = canvas.createImage();
  209. leftImg.src = this.data.victory.user.avatar
  210. leftImg.onerror = () => {
  211. loadRightImg()
  212. }
  213. leftImg.onload = () => {
  214. ctx.drawImage(leftImg, lx, ly, size, size)
  215. loadRightImg()
  216. }
  217. let loadRightImg = () => {
  218. let rightImg = canvas.createImage();
  219. rightImg.src = this.data.defeated.user.avatar
  220. rightImg.onload = () => {
  221. ctx.drawImage(rightImg, rx, ry, size, size)
  222. setTimeout(() => {
  223. wx.canvasToTempFilePath({
  224. canvas: canvas,
  225. success: (res) => {
  226. 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
  227. if (userName.length > 4) {
  228. userName = userName.slice(0, 4) + '...'
  229. }
  230. resolve({
  231. title: `我挑战了${userName}的作品,这场比拼真精彩!点击加入战局!`,
  232. path: `/pages/pkResult/index?id=${this.data.pkId}&uid=${wx.getStorageSync('uid')}&playback=true`,
  233. imageUrl: res.tempFilePath
  234. })
  235. },
  236. fail(res) {
  237. reject()
  238. }
  239. }, this)
  240. }, 500)
  241. }
  242. rightImg.onerror = () => {
  243. setTimeout(() => {
  244. wx.canvasToTempFilePath({
  245. canvas: canvas,
  246. success(res) {
  247. resolve({
  248. title: '我的新作品发布啦,快来捧场点赞!',
  249. path: `/pages/pkResult/index?uid=${wx.getStorageSync('uid')}`,
  250. imageUrl: res.tempFilePath
  251. })
  252. },
  253. fail(res) {
  254. reject()
  255. }
  256. }, this)
  257. }, 500)
  258. }
  259. }
  260. }
  261. })
  262. })
  263. },
  264. onShareAppMessage({
  265. from,
  266. target
  267. }) {
  268. if (from == 'button') {
  269. const promise = new Promise(resolve => {
  270. this.creatShare().then(res => {
  271. resolve(res)
  272. })
  273. })
  274. return {
  275. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  276. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  277. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg',
  278. promise
  279. }
  280. } else {
  281. return {
  282. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  283. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  284. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  285. }
  286. }
  287. },
  288. })