video.js 4.8 KB

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