index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {
  2. getSelfRead
  3. } from '~/api/user'
  4. let videoContext = null
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. list: null,
  11. currentId: '',
  12. videoState: true,
  13. commentShow: false,
  14. canvasHidden: false, //设置画板的显示与隐藏
  15. shareImgPath: '' //用于储存canvas生成的图片
  16. },
  17. onLoad(options) {
  18. this.getSelfRead()
  19. },
  20. // 打开评论
  21. openComment({
  22. target
  23. }) {
  24. this.setData({
  25. commentShow: true,
  26. commentId: target.dataset.id,
  27. });
  28. },
  29. // 评论区点击
  30. commentTap: function(e) {
  31. if (e.target.dataset.type === 'blank') {
  32. this.setData({
  33. commentShow: false
  34. })
  35. }
  36. },
  37. //获取自己作品列表
  38. async getSelfRead() {
  39. let list = await getSelfRead()
  40. console.log(list);
  41. this.setData({
  42. list
  43. })
  44. },
  45. creatShare(video) {
  46. return new Promise((resolve, reject) => {
  47. let context = wx.createSelectorQuery();
  48. context
  49. .select('#share')
  50. .fields({
  51. node: true,
  52. size: true
  53. }).exec((res) => {
  54. const canvas = res[0].node;
  55. const ctx = canvas.getContext('2d');
  56. const dpr = wx.getSystemInfoSync().pixelRatio;
  57. canvas.width = res[0].width * dpr;
  58. canvas.height = res[0].height * dpr;
  59. ctx.scale(dpr, dpr);
  60. ctx.font = '14px PingFang';
  61. let pic = canvas.createImage();
  62. pic.src = video.userRead.coverImg; //可以是本地,也可以是网络图片
  63. pic.onload = () => {
  64. ctx.drawImage(pic, 0, 0, 375, 211);
  65. }
  66. let peiyin = canvas.createImage();
  67. peiyin.src = '/static/image/peiyin.jpg';
  68. peiyin.onload = () => {
  69. ctx.drawImage(peiyin, 0, 211, 375, 89);
  70. // 收藏,一个一个渲染
  71. let sc = canvas.createImage();
  72. sc.src = '/static/image/no_collect.png'
  73. sc.onload = () => {
  74. ctx.drawImage(sc, 12, 220, 20, 20)
  75. ctx.fillText('收藏', 36, 238)
  76. //分享
  77. let fx = canvas.createImage();
  78. fx.src = '/static/index/share.png'
  79. fx.onload = () => {
  80. ctx.drawImage(fx, 78, 220, 22, 22)
  81. ctx.fillText('分享', 104, 238)
  82. //点赞
  83. let dz = canvas.createImage();
  84. dz.src = video.isLike ? '/static/index/heart_colored.png' : '/static/index/heart.png'
  85. dz.onload = () => {
  86. ctx.drawImage(dz, 258, 222, 22, 22)
  87. ctx.fillText(video.userRead.likeAmount, 284, 238)
  88. //评论
  89. let pl = canvas.createImage();
  90. pl.src = '/static/index/comment.png'
  91. pl.onload = () => {
  92. ctx.drawImage(pl, 318, 222, 22, 22)
  93. ctx.fillText(video.userRead.commentAmount, 340, 238)
  94. setTimeout(() => {
  95. wx.canvasToTempFilePath({
  96. canvas: canvas,
  97. success(res) {
  98. resolve({
  99. title: '请欣赏我的课文朗读作品,点赞+评论。',
  100. path: `/pages/index/index?readId=${video.userRead.id}&uid=${wx.getStorageSync('uid')}`,
  101. imageUrl: res.tempFilePath
  102. })
  103. },
  104. fail(res) {
  105. reject()
  106. }
  107. }, this)
  108. }, 500)
  109. }
  110. }
  111. }
  112. }
  113. }
  114. })
  115. })
  116. },
  117. // 改变视频状态
  118. changStatus({
  119. detail
  120. }) {
  121. this.setData(detail)
  122. },
  123. // 开始播放
  124. playVideo({
  125. currentTarget
  126. }) {
  127. this.setData({
  128. videoState: true,
  129. currentId: currentTarget.dataset.id
  130. })
  131. },
  132. /* 改变视频播放状态 ,暂时先不用*/
  133. changeVideoState() {
  134. this.videoContext = wx.createVideoContext('myVideo')
  135. let videoState = this.data.videoState
  136. if (videoState) {
  137. this.videoContext.pause()
  138. } else {
  139. this.videoContext.play()
  140. }
  141. this.setData({
  142. videoState: !videoState
  143. })
  144. },
  145. onShareAppMessage({
  146. from,
  147. target
  148. }) {
  149. if (from == 'button') {
  150. let video = target.dataset.info
  151. const promise = new Promise(resolve => {
  152. this.creatShare(video).then(res => {
  153. resolve(res)
  154. })
  155. })
  156. console.log(video);
  157. return {
  158. title: '请欣赏我的课文朗读作品,点赞+评论。',
  159. path: `/pages/index/index?readId=${video.userRead.id}&uid=${wx.getStorageSync('uid')}`,
  160. imageUrl: video.userRead.coverImg,
  161. promise
  162. }
  163. } else {
  164. return {
  165. title: '课文朗读,从未如此有趣。',
  166. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  167. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  168. }
  169. }
  170. }
  171. })