share.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // compontents/share/share.js
  2. const APIClient = require('../../utils/APIClient.js');
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. * 分享弹窗 没怎么用
  7. */
  8. properties: {
  9. },
  10. /**
  11. * 组件的初始数据
  12. */
  13. data: {
  14. flag: true,
  15. postId: '',
  16. imgUrl: [],
  17. featureMap: {},
  18. title: '',
  19. ratio: '',
  20. codeImage: ''
  21. },
  22. /**
  23. * 组件的方法列表
  24. */
  25. methods: {
  26. //隐藏分享框
  27. hidePopup: function () {
  28. this.setData({
  29. flag: !this.data.flag
  30. })
  31. },
  32. //展示分享框
  33. showPopup (postId, imgUrl, featureMap, title, ratio) {
  34. this.setData({
  35. flag: !this.data.flag,
  36. postId,
  37. imgUrl,
  38. featureMap,
  39. title,
  40. ratio
  41. })
  42. const postsId = this.data.postId;
  43. APIClient.getqrCode({
  44. postsId,
  45. ind: '7'
  46. }).success((res) => {
  47. console.log(res.data.data)
  48. wx.downloadFile({
  49. url: res.data.data,
  50. success: (res) => {
  51. //'https:' +
  52. this.setData({
  53. codeImage: res.tempFilePath
  54. })
  55. }
  56. })
  57. })
  58. },
  59. //保存图片
  60. saveImage (e) {
  61. const imgUrl = this.data.imgUrl;
  62. const ratio = this.data.ratio;
  63. const featureMap = this.data.featureMap;
  64. const title = this.data.title;
  65. //获取屏幕宽度
  66. const windowWidth = wx.getSystemInfoSync().windowWidth;
  67. //获取图片高度
  68. const imgHeight = windowWidth*ratio;
  69. //先绘制图片
  70. for (let item of imgUrl) {
  71. wx.downloadFile({
  72. url: item,
  73. success: (res) => {
  74. console.log(res.tempFilePath);
  75. const codeImage = this.data.codeImage;
  76. console.log('二维码' + codeImage)
  77. //canvas绘制文字和图片
  78. const ctx = wx.createCanvasContext('myCanvas');
  79. var imgPath = res.tempFilePath;
  80. ctx.setFillStyle('white')
  81. ctx.fillRect(0, 0, windowWidth, imgHeight + 300)
  82. //绘制用户上传的图片
  83. ctx.drawImage(imgPath, 0, 0, windowWidth, imgHeight);
  84. //绘制用户发表的内容和名字
  85. ctx.setFontSize(18)
  86. ctx.setFillStyle('black')
  87. ctx.fillText(title, 25, imgHeight + 90)
  88. ctx.setFontSize(12)
  89. ctx.fillText('在小学王者班上学会了这些,分享给大家', 25, imgHeight + 60)
  90. ctx.setFontSize(22)
  91. ctx.fillText(featureMap.wechatName, 25, imgHeight + 35);
  92. //横线
  93. ctx.moveTo(20, imgHeight + 140)
  94. ctx.lineTo(windowWidth - 40, imgHeight + 140)
  95. ctx.stroke()
  96. //二维码和文字
  97. ctx.drawImage(codeImage, 20, imgHeight + 150, 100, 100);
  98. ctx.setFontSize(16)
  99. ctx.setFillStyle('black')
  100. ctx.fillText('长按二维码', 150, imgHeight + 180)
  101. ctx.fillText('进入小程序查看更多', 150, imgHeight + 210)
  102. ctx.draw()
  103. setTimeout(() => {
  104. this.canvasToImage()
  105. }, 500)
  106. }
  107. })
  108. }
  109. },
  110. canvasToImage () {
  111. let that = this;
  112. //canvas生成图片
  113. wx.canvasToTempFilePath({
  114. x: 0,
  115. y: 0,
  116. width: that.windowWidth,
  117. height: that.imgHeight + 300,
  118. destWidth: that.windowWidth,
  119. destHeight: that.imgHeight + 300,
  120. canvasId: 'myCanvas',
  121. success: (res) => {
  122. console.log(res.tempFilePath);
  123. //保存图片到本地
  124. wx.saveImageToPhotosAlbum({
  125. filePath: res.tempFilePath,
  126. success: (res) => {
  127. wx.showModal({
  128. title: '存图成功',
  129. content: '图片成功保存到相册了,去发朋友圈',
  130. showCancel:false,
  131. confirmText:'我知道了',
  132. confirmColor:'#72B9C3',
  133. success: (res) => {
  134. if (res.confirm) {
  135. console.log('用户点击确定');
  136. this.setData({
  137. flag: !this.data.flag
  138. })
  139. }
  140. //that.hideShareImg()
  141. }
  142. })
  143. }
  144. })
  145. },
  146. fail:function (res) {
  147. console.log(res)
  148. }
  149. })
  150. }
  151. }
  152. })