share.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // component/shar-dialog/shar-dialog.js
  2. import httpRequestApi from '../../utils/APIClient';
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. shareType: {
  9. type: 'String',
  10. value: ''
  11. }
  12. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. shareFlag: true,
  18. imgUrls: '',
  19. avatar: '',
  20. author: '',
  21. iconImg: '',
  22. title: '',
  23. path: ''
  24. },
  25. /**
  26. * 组件的方法列表
  27. */
  28. methods: {
  29. poster: function () {
  30. const that = this;
  31. const ctx = wx.createCanvasContext('myCanvas');
  32. ctx.rect(0, 0, 370, 507)
  33. ctx.setFillStyle('#fff')
  34. ctx.fill()
  35. ctx.save()
  36. ctx.beginPath()
  37. ctx.setFillStyle('#fff')
  38. ctx.fill()
  39. ctx.arc(185, 48, 37, 0, 2 * Math.PI, false)
  40. ctx.clip(); //画了圆 再剪切 原始画布中剪切任意形状和尺寸。一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内
  41. // wx.downloadFile({
  42. // url: this.data.avatar,
  43. // success(res) {
  44. // ctx.drawImage(res.tempFilePath, 148, 8, 100, 100);
  45. // }
  46. // });
  47. ctx.drawImage(this.data.avatar, 148, 8, 100, 100); //插入图片
  48. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  49. ctx.setFontSize(20)
  50. ctx.setFillStyle('#535353')
  51. ctx.fillText(this.data.author, (370 - ctx.measureText(this.data.author).width) / 2, 115)
  52. ctx.setFillStyle('#FF4400')
  53. if (this.data.shareType === 'works') {
  54. ctx.fillText('已使出洪荒之力,声情并茂的为', (370 - ctx.measureText('已使出洪荒之力,声情并茂的为').width) / 2, 138)
  55. ctx.fillText(`《${this.data.title}》`, (370 - ctx.measureText(`《${this.data.title}》`).width) / 2, 160)
  56. ctx.fillText(`配了一段惊世之作`, (370 - ctx.measureText(`配了一段惊世之作`).width) / 2, 182)
  57. // 保存图片到本地
  58. // wx.downloadFile({
  59. // url: this.data.iconImg,
  60. // success(res) {
  61. // console.log(res)
  62. // ctx.drawImage(res.tempFilePath, 9, 205, 352, 145);
  63. // }
  64. // });
  65. ctx.drawImage(this.data.iconImg, 9, 205, 352, 145);
  66. ctx.setFontSize(18)
  67. ctx.setFillStyle('#000')
  68. ctx.drawImage('../../../static/groupImg/share_bottom.png', 3, 419, 370, 192);
  69. ctx.setFontSize(20)
  70. ctx.setFillStyle('red')
  71. ctx.fillText('为TA疯狂打CALL', 9, 467)
  72. ctx.setFontSize(18)
  73. ctx.setFillStyle('#000')
  74. ctx.fillText('长按识别二维码,快去听听', 9, 493)
  75. } else {
  76. ctx.fillText('郎朗读书声是世上最美的声音', 50, 138)
  77. ctx.drawImage('../../../static/groupImg/Combined Shape.png', 11, 348, 196, 65);
  78. ctx.drawImage('../../../static/groupImg/Rectangle 41.png', 134, 358, 59, 20);
  79. ctx.setFontSize(14)
  80. ctx.setFillStyle('#fff')
  81. ctx.fillText('五人团', 149, 373)
  82. ctx.setFontSize(16)
  83. ctx.setFillStyle('#A95A00')
  84. ctx.fillText('原价', 24, 373)
  85. ctx.setStrokeStyle('red')
  86. ctx.moveTo(64, 368)
  87. ctx.lineTo(114, 368)
  88. ctx.stroke()
  89. ctx.fillText('¥99元', 64, 373)
  90. ctx.setFontSize(18)
  91. ctx.fillText('团购价仅需', 20, 403)
  92. ctx.fillText('元', 144, 403)
  93. ctx.setFontSize(20)
  94. ctx.setFillStyle('red')
  95. ctx.fillText('¥1', 109, 403)
  96. ctx.drawImage('../../../static/groupImg/share_bottom.png', 3, 419, 370, 192);
  97. ctx.setFontSize(20)
  98. ctx.setFillStyle('red')
  99. ctx.fillText('名额有限,售完截止', 9, 467)
  100. ctx.setFontSize(18)
  101. ctx.setFillStyle('#000')
  102. ctx.fillText('长按识别二维码参加团购', 9, 493)
  103. }
  104. ctx.drawImage('../../../static/groupImg/code.png', 217, 374, 135, 135);
  105. console.log(ctx)
  106. ctx.draw(true, function (res) {
  107. wx.canvasToTempFilePath({
  108. x: 0,
  109. y: 0,
  110. width: 370,
  111. height: 507,
  112. destWidth: 370,
  113. destHeight: 507,
  114. canvasId: 'myCanvas',
  115. success: (res) => {
  116. console.log(res.tempFilePath)
  117. wx.saveImageToPhotosAlbum({
  118. filePath: res.path,
  119. success(res) {
  120. console.log(res);
  121. }
  122. })
  123. that.setData({
  124. imgUrls: res.tempFilePath
  125. })
  126. },
  127. fail: (res) => {
  128. console.log(res)
  129. }
  130. })
  131. })
  132. },
  133. share: function (data) {
  134. console.log(this.data)
  135. console.log(data)
  136. // 下载icon
  137. wx.downloadFile({
  138. url: data.iconImg,
  139. success: (res) => {
  140. this.setData({
  141. iconImg: res.tempFilePath
  142. })
  143. }
  144. });
  145. // 下载头像
  146. wx.downloadFile({
  147. url: data.avatar,
  148. success: (res) => {
  149. this.setData({
  150. avatar: res.tempFilePath
  151. })
  152. }
  153. });
  154. // 生成二维码并下载
  155. httpRequestApi.createQRCode({
  156. page: data.path,
  157. scene: "par=no"
  158. }).success(res => {
  159. console.log(data.path)
  160. console.log(res)
  161. })
  162. this.setData({
  163. shareFlag: !this.data.shareFlag,
  164. // avatar: data.avatar,
  165. author: data.author,
  166. // iconImg: data.iconImg,
  167. title: data.title,
  168. path: data.path
  169. })
  170. },
  171. PreservationImg: function () {
  172. wx.saveImageToPhotosAlbum({
  173. filePath: this.data.imgUrls,
  174. success(res) {
  175. console.log('保存成功')
  176. }
  177. })
  178. },
  179. shareFriend: function () {
  180. this.triggerEvent('customevent', {})
  181. }
  182. },
  183. onShareAppMessage: (res) => {
  184. if (res.from === 'button') {
  185. // 来自页面内转发按钮
  186. console.log(res.target)
  187. }
  188. return {
  189. title: this.data.title,
  190. path: this.data.path
  191. }
  192. },
  193. })