video.js 4.1 KB

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