share.js 5.9 KB

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