share.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {
  2. isActivityWork
  3. } from '~/api/works'
  4. module.exports = Behavior({
  5. data: {},
  6. methods: {
  7. onShareAppMessage({
  8. from,
  9. target
  10. }) {
  11. if (from == 'button') {
  12. let video = target.dataset.info
  13. const promise = new Promise(resolve => {
  14. this.creatShare(video).then(res => {
  15. resolve(res)
  16. })
  17. })
  18. return {
  19. title: '这个小程序太赞了!孩子朗读能力蹭蹭上涨,推荐你试试!',
  20. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  21. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg',
  22. promise
  23. }
  24. } else {
  25. return {
  26. title: '这个小程序太赞了!孩子朗读能力蹭蹭上涨,推荐你试试!',
  27. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  28. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  29. }
  30. }
  31. },
  32. creatShare(video) {
  33. return new Promise(async (resolve, reject) => {
  34. let isActivity = await isActivityWork(video.userRead.id)
  35. let context = wx.createSelectorQuery(video.id);
  36. context
  37. .select('#share')
  38. .fields({
  39. node: true,
  40. size: true
  41. }).exec((res) => {
  42. const canvas = res[0].node;
  43. const ctx = canvas.getContext('2d');
  44. const dpr = wx.getSystemInfoSync().pixelRatio;
  45. canvas.width = res[0].width * dpr;
  46. canvas.height = res[0].height * dpr;
  47. ctx.scale(dpr, dpr);
  48. ctx.font = '14px PingFang';
  49. let pic = canvas.createImage();
  50. pic.src = video.userReadExtend && video.userReadExtend.resourcesType == 1 ? video.userReadExtend.backgroundVirtualImg : video.userRead.coverImg;
  51. pic.onload = () => {
  52. ctx.drawImage(pic, 0, 0, 375, 211);
  53. let peiyin = canvas.createImage();
  54. peiyin.src = '/static/peiyin.jpg';
  55. peiyin.onload = () => {
  56. ctx.drawImage(peiyin, 0, 211, 375, 89);
  57. //分享
  58. let fx = canvas.createImage();
  59. fx.src = '/static/share.png'
  60. fx.onload = () => {
  61. ctx.drawImage(fx, 12, 220, 20, 20)
  62. ctx.fillText('分享', 36, 238)
  63. // 收藏,一个一个渲染
  64. let sc = canvas.createImage();
  65. sc.src = '/static/no_collect.png'
  66. sc.onload = () => {
  67. ctx.drawImage(sc, 110, 220, 19, 19)
  68. ctx.fillText('收藏', 134, 238)
  69. // 评论
  70. let pl = canvas.createImage();
  71. pl.src = '/static/comment.png'
  72. pl.onload = () => {
  73. ctx.drawImage(pl, 228, 222, 22, 22)
  74. ctx.fillText(video.userRead.commentAmount || 0, 340, 238)
  75. //点赞
  76. let dz = canvas.createImage();
  77. dz.src = video.isLike ? '/static/heart_colored.png' : '/static/heart.png'
  78. dz.onload = () => {
  79. ctx.drawImage(dz, 318, 222, 22, 22)
  80. ctx.fillText(video.userRead.likeAmount || 0, 254, 238)
  81. if (video.userReadExtend.resourcesType == 1) {
  82. let aBg = canvas.createImage();
  83. aBg.src = '/static/shareAudioBg.png';
  84. aBg.onload = () => {
  85. ctx.drawImage(aBg, 127.5, 38, 120, 120);
  86. let rate = 0.5
  87. ctx.arc(
  88. Math.floor(375 * rate),
  89. 98,
  90. Math.floor(100 * rate),
  91. 0,
  92. 2 * Math.PI
  93. );
  94. ctx.clip() //裁剪
  95. let coverImg = canvas.createImage();
  96. coverImg.src = video.userRead.coverImg;
  97. coverImg.onload = () => {
  98. ctx.drawImage( //定位在圆圈范围内便会出现
  99. coverImg, //图片暂存路径
  100. 129, 42,
  101. 110, 110,
  102. );
  103. ctx.restore()
  104. }
  105. }
  106. }
  107. setTimeout(() => {
  108. wx.canvasToTempFilePath({
  109. canvas: canvas,
  110. success(res) {
  111. let path = video.user.profession == '官方' ? '/pages/reading/index' : '/pages/pkPage/index'
  112. let title = wx.getStorageSync('uid') == video.user.uid ? '我的新作品发布啦,快来捧场点赞!' : video.userRead.type == 'READ' ? '发现一篇宝藏作品,这声音让人爱了!不信你不着迷!' : '我正在听这篇朗读示范,这发音,播音专业水准!你也来听听!'
  113. if (isActivity) {
  114. title = '点赞、评论、转发三件套,数据今天就过万!'
  115. }
  116. resolve({
  117. title,
  118. path: `${path}?videoId=${video.userRead.id}&uid=${wx.getStorageSync('uid')}&isShare=true`,
  119. imageUrl: res.tempFilePath
  120. })
  121. },
  122. fail(res) {
  123. reject()
  124. }
  125. }, this)
  126. }, 500)
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. })
  134. })
  135. },
  136. }
  137. })