video.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. let innerAudioContext
  2. import {
  3. setDuration
  4. } from '~/utils/util'
  5. module.exports = Behavior({
  6. data: {
  7. currentId: '',
  8. sliderValue: 0
  9. },
  10. properties: {},
  11. pageLifetimes: {
  12. hide() {
  13. this.resetAudio()
  14. },
  15. },
  16. lifetimes: {
  17. attached() {
  18. this.innerAudioContext = wx.createInnerAudioContext()
  19. this.innerAudioContext.onTimeUpdate(res => {
  20. this.setData({
  21. sliderValue: Math.round(this.innerAudioContext.currentTime / this.innerAudioContext.duration * 100),
  22. currentTime: setDuration(this.innerAudioContext.currentTime)
  23. })
  24. })
  25. this.innerAudioContext.onEnded(res => {
  26. this.resetAudio()
  27. })
  28. }
  29. },
  30. methods: {
  31. // 开始播放
  32. playVideo({
  33. currentTarget
  34. }) {
  35. this.setData({
  36. currentId: currentTarget.dataset.id
  37. })
  38. },
  39. // 播放音频
  40. playAudio({
  41. currentTarget
  42. }) {
  43. if (this.data.currentId == currentTarget.dataset.id) {
  44. return this.resetAudio()
  45. }
  46. if (this.innerAudioContext) {
  47. this.resetAudio()
  48. }
  49. /* else {
  50. this.innerAudioContext = wx.createInnerAudioContext()
  51. this.innerAudioContext.onEnded(res => {
  52. this.resetAudio()
  53. })
  54. } */
  55. this.innerAudioContext.src = currentTarget.dataset.audio
  56. // this.innerAudioContext.onCanplay(() => {
  57. setTimeout(() => {
  58. this.innerAudioContext.play();
  59. }, 500)
  60. // })
  61. this.setData({
  62. currentId: currentTarget.dataset.id
  63. })
  64. },
  65. // 重置音频
  66. resetAudio() {
  67. if (this.innerAudioContext) {
  68. this.innerAudioContext.stop();
  69. }
  70. this.setData({
  71. currentId: '',
  72. sliderValue: 0
  73. })
  74. },
  75. // 打开评论
  76. openComment({
  77. target
  78. }) {
  79. this.selectComponent('#comment').open(target.dataset.id)
  80. },
  81. // 分享
  82. creatShare(video) {
  83. return new Promise((resolve, reject) => {
  84. let context = wx.createSelectorQuery();
  85. context
  86. .select('#share')
  87. .fields({
  88. node: true,
  89. size: true
  90. }).exec((res) => {
  91. const canvas = res[0].node;
  92. const ctx = canvas.getContext('2d');
  93. const dpr = wx.getSystemInfoSync().pixelRatio;
  94. canvas.width = res[0].width * dpr;
  95. canvas.height = res[0].height * dpr;
  96. ctx.scale(dpr, dpr);
  97. ctx.font = '14px PingFang';
  98. let pic = canvas.createImage();
  99. pic.src = video.userRead.coverImg; //可以是本地,也可以是网络图片
  100. pic.onload = () => {
  101. ctx.drawImage(pic, 0, 0, 375, 211);
  102. }
  103. let peiyin = canvas.createImage();
  104. peiyin.src = '/static/peiyin.jpg';
  105. peiyin.onload = () => {
  106. ctx.drawImage(peiyin, 0, 211, 375, 89);
  107. // 收藏,一个一个渲染
  108. let sc = canvas.createImage();
  109. sc.src = '/static/no_collect.png'
  110. sc.onload = () => {
  111. ctx.drawImage(sc, 12, 220, 20, 20)
  112. ctx.fillText('收藏', 36, 238)
  113. //分享
  114. let fx = canvas.createImage();
  115. fx.src = '/static/share.png'
  116. fx.onload = () => {
  117. ctx.drawImage(fx, 78, 220, 22, 22)
  118. ctx.fillText('分享', 104, 238)
  119. //点赞
  120. let dz = canvas.createImage();
  121. dz.src = video.isLike ? '/static/heart_colored.png' : '/static/heart.png'
  122. dz.onload = () => {
  123. ctx.drawImage(dz, 258, 222, 22, 22)
  124. ctx.fillText(video.likes, 284, 238)
  125. //评论
  126. let pl = canvas.createImage();
  127. pl.src = '/static/comment.png'
  128. pl.onload = () => {
  129. ctx.drawImage(pl, 318, 222, 22, 22)
  130. ctx.fillText(video.commentAmount, 340, 238)
  131. setTimeout(() => {
  132. wx.canvasToTempFilePath({
  133. canvas: canvas,
  134. success(res) {
  135. resolve({
  136. title: '我的新作品发布啦,快来捧场点赞!',
  137. path: `/pages/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  138. imageUrl: res.tempFilePath
  139. })
  140. },
  141. fail(res) {
  142. reject()
  143. }
  144. }, this)
  145. }, 500)
  146. }
  147. }
  148. }
  149. }
  150. }
  151. })
  152. })
  153. },
  154. }
  155. })