video.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. module.exports = Behavior({
  2. data: {
  3. currentId: '',
  4. },
  5. properties: {},
  6. pageLifetimes: {
  7. hide() {
  8. this.resetAudio()
  9. },
  10. },
  11. methods: {
  12. // 开始播放
  13. playVideo({
  14. currentTarget
  15. }) {
  16. this.setData({
  17. currentId: currentTarget.dataset.id
  18. })
  19. },
  20. // 播放音频
  21. playAudio({
  22. currentTarget
  23. }) {
  24. if (this.data.currentId == currentTarget.dataset.id) {
  25. return this.innerAudioContext.stop();
  26. }
  27. if (this.innerAudioContext) {
  28. this.innerAudioContext.stop();
  29. } else {
  30. this.innerAudioContext = wx.createInnerAudioContext()
  31. this.innerAudioContext.onEnded(res => {
  32. this.setData({
  33. currentId: ''
  34. })
  35. })
  36. this.innerAudioContext.onStop((res) => {
  37. this.setData({
  38. currentId: ''
  39. })
  40. });
  41. }
  42. this.innerAudioContext.src = currentTarget.dataset.audio
  43. this.innerAudioContext.play();
  44. this.setData({
  45. currentId: currentTarget.dataset.id
  46. })
  47. },
  48. // 重置音频
  49. resetAudio() {
  50. console.log('触发重置');
  51. if (this.innerAudioContext) {
  52. this.innerAudioContext.stop();
  53. }
  54. this.setData({
  55. currentId: ''
  56. })
  57. },
  58. // 打开评论
  59. openComment({
  60. target
  61. }) {
  62. this.selectComponent('#comment').open(target.dataset.id)
  63. },
  64. // 分享
  65. creatShare(video) {
  66. console.log(video);
  67. return new Promise((resolve, reject) => {
  68. let context = wx.createSelectorQuery();
  69. context
  70. .select('#share')
  71. .fields({
  72. node: true,
  73. size: true
  74. }).exec((res) => {
  75. const canvas = res[0].node;
  76. const ctx = canvas.getContext('2d');
  77. const dpr = wx.getSystemInfoSync().pixelRatio;
  78. canvas.width = res[0].width * dpr;
  79. canvas.height = res[0].height * dpr;
  80. ctx.scale(dpr, dpr);
  81. ctx.font = '14px PingFang';
  82. let pic = canvas.createImage();
  83. pic.src = video.userRead.coverImg; //可以是本地,也可以是网络图片
  84. pic.onload = () => {
  85. ctx.drawImage(pic, 0, 0, 375, 211);
  86. }
  87. let peiyin = canvas.createImage();
  88. peiyin.src = '/static/peiyin.jpg';
  89. peiyin.onload = () => {
  90. ctx.drawImage(peiyin, 0, 211, 375, 89);
  91. // 收藏,一个一个渲染
  92. let sc = canvas.createImage();
  93. sc.src = '/static/no_collect.png'
  94. sc.onload = () => {
  95. ctx.drawImage(sc, 12, 220, 20, 20)
  96. ctx.fillText('收藏', 36, 238)
  97. //分享
  98. let fx = canvas.createImage();
  99. fx.src = '/static/share.png'
  100. fx.onload = () => {
  101. ctx.drawImage(fx, 78, 220, 22, 22)
  102. ctx.fillText('分享', 104, 238)
  103. //点赞
  104. let dz = canvas.createImage();
  105. dz.src = video.isLike ? '/static/heart_colored.png' : '/static/heart.png'
  106. dz.onload = () => {
  107. ctx.drawImage(dz, 258, 222, 22, 22)
  108. ctx.fillText(video.likes, 284, 238)
  109. //评论
  110. let pl = canvas.createImage();
  111. pl.src = '/static/comment.png'
  112. pl.onload = () => {
  113. ctx.drawImage(pl, 318, 222, 22, 22)
  114. ctx.fillText(video.commentAmount, 340, 238)
  115. setTimeout(() => {
  116. wx.canvasToTempFilePath({
  117. canvas: canvas,
  118. success(res) {
  119. resolve({
  120. title: '请欣赏我的课文朗读作品,点赞+评论。',
  121. path: `/pages/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  122. imageUrl: res.tempFilePath
  123. })
  124. },
  125. fail(res) {
  126. reject()
  127. }
  128. }, this)
  129. }, 500)
  130. }
  131. }
  132. }
  133. }
  134. }
  135. })
  136. })
  137. },
  138. }
  139. })