video.js 5.6 KB

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