video.js 5.7 KB

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