video.js 4.0 KB

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