share.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. this.setData({
  52. codeImage: res.tempFilePath
  53. })
  54. }
  55. })
  56. })
  57. },
  58. //保存图片
  59. saveImage (e) {
  60. const imgUrl = this.data.imgUrl;
  61. const ratio = this.data.ratio;
  62. const featureMap = this.data.featureMap;
  63. const title = this.data.title;
  64. //获取屏幕宽度
  65. const windowWidth = wx.getSystemInfoSync().windowWidth;
  66. //获取图片高度
  67. const imgHeight = windowWidth*ratio;
  68. //先绘制图片
  69. for (let item of imgUrl) {
  70. wx.downloadFile({
  71. url: item,
  72. success: (res) => {
  73. console.log(res.tempFilePath);
  74. const codeImage = this.data.codeImage;
  75. //canvas绘制文字和图片
  76. const ctx = wx.createCanvasContext('myCanvas');
  77. var imgPath = res.tempFilePath;
  78. ctx.setFillStyle('white')
  79. ctx.fillRect(0, 0, windowWidth, imgHeight + 300)
  80. //绘制用户上传的图片
  81. ctx.drawImage(imgPath, 0, 0, windowWidth, imgHeight);
  82. //绘制用户发表的内容和名字
  83. ctx.setFontSize(18)
  84. ctx.setFillStyle('black')
  85. ctx.fillText(title, 25, imgHeight + 90)
  86. ctx.setFontSize(12)
  87. ctx.fillText('在勋章战士上学会了这些,分享给大家', 25, imgHeight + 60)
  88. ctx.setFontSize(22)
  89. ctx.fillText(featureMap.wechatName, 25, imgHeight + 35);
  90. //横线
  91. ctx.moveTo(20, imgHeight + 140)
  92. ctx.lineTo(windowWidth - 40, imgHeight + 140)
  93. ctx.stroke()
  94. //二维码和文字
  95. ctx.drawImage(codeImage, 20, imgHeight + 150, 100, 100);
  96. ctx.setFontSize(16)
  97. ctx.setFillStyle('black')
  98. ctx.fillText('长按二维码', 150, imgHeight + 180)
  99. ctx.fillText('进入小程序查看更多', 150, imgHeight + 210)
  100. ctx.draw()
  101. setTimeout(() => {
  102. this.canvasToImage()
  103. }, 500)
  104. }
  105. })
  106. }
  107. },
  108. canvasToImage () {
  109. let that = this;
  110. //canvas生成图片
  111. wx.canvasToTempFilePath({
  112. x: 0,
  113. y: 0,
  114. width: that.windowWidth,
  115. height: that.imgHeight + 300,
  116. destWidth: that.windowWidth,
  117. destHeight: that.imgHeight + 300,
  118. canvasId: 'myCanvas',
  119. success: (res) => {
  120. console.log(res.tempFilePath);
  121. //保存图片到本地
  122. wx.saveImageToPhotosAlbum({
  123. filePath: res.tempFilePath,
  124. success: (res) => {
  125. wx.showModal({
  126. title: '存图成功',
  127. content: '图片成功保存到相册了,去发朋友圈',
  128. showCancel:false,
  129. confirmText:'我知道了',
  130. confirmColor:'#72B9C3',
  131. success: (res) => {
  132. if (res.confirm) {
  133. console.log('用户点击确定');
  134. this.setData({
  135. flag: !this.data.flag
  136. })
  137. }
  138. //that.hideShareImg()
  139. }
  140. })
  141. }
  142. })
  143. },
  144. fail:function (res) {
  145. console.log(res)
  146. }
  147. })
  148. }
  149. }
  150. })