video.js 4.1 KB

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