share.js 4.5 KB

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