index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import behavior from '~/mixins/video'
  2. import {
  3. getreadInfo
  4. } from '~/api/video'
  5. Page({
  6. behaviors: [behavior],
  7. data: {
  8. videoInfo: {}
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad(options) {
  14. let videoId = options.videoId
  15. this.getreadInfo(videoId)
  16. },
  17. async getreadInfo(videoId) {
  18. let videoInfo = await getreadInfo(videoId)
  19. wx.setNavigationBarTitle({
  20. title: videoInfo.userRead.title
  21. })
  22. this.setData({
  23. videoInfo
  24. })
  25. },
  26. /**
  27. * 页面上拉触底事件的处理函数
  28. */
  29. onReachBottom() {
  30. },
  31. creatShare(video) {
  32. return new Promise((resolve, reject) => {
  33. let context = wx.createSelectorQuery();
  34. context
  35. .select('#share')
  36. .fields({
  37. node: true,
  38. size: true
  39. }).exec((res) => {
  40. const canvas = res[0].node;
  41. const ctx = canvas.getContext('2d');
  42. const dpr = wx.getSystemInfoSync().pixelRatio;
  43. canvas.width = res[0].width * dpr;
  44. canvas.height = res[0].height * dpr;
  45. ctx.scale(dpr, dpr);
  46. ctx.font = '14px PingFang';
  47. let pic = canvas.createImage();
  48. pic.src = video.userRead.coverImg; //可以是本地,也可以是网络图片
  49. pic.onload = () => {
  50. ctx.drawImage(pic, 0, 0, 375, 211);
  51. }
  52. let peiyin = canvas.createImage();
  53. peiyin.src = '/static/peiyin.jpg';
  54. peiyin.onload = () => {
  55. ctx.drawImage(peiyin, 0, 211, 375, 89);
  56. // 收藏,一个一个渲染
  57. let sc = canvas.createImage();
  58. sc.src = '/static/star.png'
  59. sc.onload = () => {
  60. ctx.drawImage(sc, 12, 220, 20, 20)
  61. ctx.fillText('收藏', 36, 238)
  62. //分享
  63. let fx = canvas.createImage();
  64. fx.src = '/static/share.png'
  65. fx.onload = () => {
  66. ctx.drawImage(fx, 78, 220, 22, 22)
  67. ctx.fillText('分享', 104, 238)
  68. //点赞
  69. let dz = canvas.createImage();
  70. dz.src = video.isLike ? '/static/heart_colored.png' : '/static/heart.png'
  71. dz.onload = () => {
  72. ctx.drawImage(dz, 258, 222, 22, 22)
  73. ctx.fillText(video.userRead.likeAmount, 284, 238)
  74. //评论
  75. let pl = canvas.createImage();
  76. pl.src = '/static/comment.png'
  77. pl.onload = () => {
  78. ctx.drawImage(pl, 318, 222, 22, 22)
  79. ctx.fillText(video.userRead.commentAmount, 340, 238)
  80. setTimeout(() => {
  81. wx.canvasToTempFilePath({
  82. canvas: canvas,
  83. success(res) {
  84. resolve({
  85. title: '请欣赏我的课文朗读作品,点赞+评论。',
  86. path: `/pages/index/index?readId=${video.userRead.id}&uid=${wx.getStorageSync('uid')}`,
  87. imageUrl: res.tempFilePath
  88. })
  89. },
  90. fail(res) {
  91. reject()
  92. }
  93. }, this)
  94. }, 500)
  95. }
  96. }
  97. }
  98. }
  99. }
  100. })
  101. })
  102. },
  103. onShareAppMessage({
  104. from,
  105. target
  106. }) {
  107. if (from == 'button') {
  108. let video = this.data.videoInfo
  109. const promise = new Promise(resolve => {
  110. this.creatShare(video).then(res => {
  111. resolve(res)
  112. })
  113. })
  114. return {
  115. title: '请欣赏我的课文朗读作品,点赞+评论。',
  116. path: `/pages/index/index?readId=${video.userRead.id}&uid=${wx.getStorageSync('uid')}`,
  117. imageUrl: video.userRead.coverImg,
  118. promise
  119. }
  120. } else {
  121. return {
  122. title: '课文朗读,从未如此有趣。',
  123. path: `/pages/index/index?&uid=${wx.getStorageSync('uid')}`,
  124. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  125. }
  126. }
  127. },
  128. onHide() {
  129. this.setData({
  130. currentId: ''
  131. })
  132. }
  133. })