video.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if (this.innerAudioContext) {
  51. this.innerAudioContext.stop();
  52. }
  53. this.setData({
  54. currentId: ''
  55. })
  56. },
  57. // 打开评论
  58. openComment({
  59. target
  60. }) {
  61. this.selectComponent('#comment').open(target.dataset.id)
  62. },
  63. // 分享
  64. creatShare(video) {
  65. console.log(video);
  66. return new Promise((resolve, reject) => {
  67. let context = wx.createSelectorQuery();
  68. context
  69. .select('#share')
  70. .fields({
  71. node: true,
  72. size: true
  73. }).exec((res) => {
  74. const canvas = res[0].node;
  75. const ctx = canvas.getContext('2d');
  76. const dpr = wx.getSystemInfoSync().pixelRatio;
  77. canvas.width = res[0].width * dpr;
  78. canvas.height = res[0].height * dpr;
  79. ctx.scale(dpr, dpr);
  80. ctx.font = '14px PingFang';
  81. let pic = canvas.createImage();
  82. pic.src = video.userRead.coverImg; //可以是本地,也可以是网络图片
  83. pic.onload = () => {
  84. ctx.drawImage(pic, 0, 0, 375, 211);
  85. }
  86. let peiyin = canvas.createImage();
  87. peiyin.src = '/static/peiyin.jpg';
  88. peiyin.onload = () => {
  89. ctx.drawImage(peiyin, 0, 211, 375, 89);
  90. // 收藏,一个一个渲染
  91. let sc = canvas.createImage();
  92. sc.src = '/static/no_collect.png'
  93. sc.onload = () => {
  94. ctx.drawImage(sc, 12, 220, 20, 20)
  95. ctx.fillText('收藏', 36, 238)
  96. //分享
  97. let fx = canvas.createImage();
  98. fx.src = '/static/share.png'
  99. fx.onload = () => {
  100. ctx.drawImage(fx, 78, 220, 22, 22)
  101. ctx.fillText('分享', 104, 238)
  102. //点赞
  103. let dz = canvas.createImage();
  104. dz.src = video.isLike ? '/static/heart_colored.png' : '/static/heart.png'
  105. dz.onload = () => {
  106. ctx.drawImage(dz, 258, 222, 22, 22)
  107. ctx.fillText(video.likes, 284, 238)
  108. //评论
  109. let pl = canvas.createImage();
  110. pl.src = '/static/comment.png'
  111. pl.onload = () => {
  112. ctx.drawImage(pl, 318, 222, 22, 22)
  113. ctx.fillText(video.commentAmount, 340, 238)
  114. setTimeout(() => {
  115. wx.canvasToTempFilePath({
  116. canvas: canvas,
  117. success(res) {
  118. resolve({
  119. title: '请欣赏我的课文朗读作品,点赞+评论。',
  120. path: `/pages/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  121. imageUrl: res.tempFilePath
  122. })
  123. },
  124. fail(res) {
  125. reject()
  126. }
  127. }, this)
  128. }, 500)
  129. }
  130. }
  131. }
  132. }
  133. }
  134. })
  135. })
  136. },
  137. }
  138. })