share.js 10 KB

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