share.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if(imgUrl.length == 0) {
  70. wx.showModal({
  71. title: '提示',
  72. content: '没有上传图片不能分享',
  73. showCancel:false,
  74. confirmText:'我知道了',
  75. confirmColor:'#72B9C3',
  76. success: (res) => {
  77. if (res.confirm) {
  78. console.log('用户点击确定');
  79. this.setData({
  80. flag: !this.data.flag
  81. })
  82. }
  83. //that.hideShareImg()
  84. }
  85. })
  86. return false;
  87. }
  88. //先绘制图片
  89. for (let item of imgUrl) {
  90. wx.downloadFile({
  91. url: item,
  92. success: (res) => {
  93. console.log(res.tempFilePath);
  94. const codeImage = this.data.codeImage;
  95. console.log('二维码' + codeImage)
  96. //canvas绘制文字和图片
  97. const ctx = wx.createCanvasContext('myCanvas');
  98. var imgPath = res.tempFilePath;
  99. ctx.setFillStyle('white')
  100. ctx.fillRect(0, 0, windowWidth, imgHeight + 300)
  101. //绘制用户上传的图片
  102. ctx.drawImage(imgPath, 0, 0, windowWidth, imgHeight);
  103. //绘制用户发表的内容和名字
  104. ctx.setFontSize(18)
  105. ctx.setFillStyle('black')
  106. ctx.fillText(title, 25, imgHeight + 90)
  107. ctx.setFontSize(12)
  108. ctx.fillText('在小学王者班上学会了这些,分享给大家', 25, imgHeight + 60)
  109. ctx.setFontSize(22)
  110. ctx.fillText(featureMap.wechatName, 25, imgHeight + 35);
  111. //横线
  112. ctx.moveTo(20, imgHeight + 140)
  113. ctx.lineTo(windowWidth - 40, imgHeight + 140)
  114. ctx.stroke()
  115. //二维码和文字
  116. ctx.drawImage(codeImage, 20, imgHeight + 150, 100, 100);
  117. ctx.setFontSize(16)
  118. ctx.setFillStyle('black')
  119. ctx.fillText('长按二维码', 150, imgHeight + 180)
  120. ctx.fillText('进入小程序查看更多', 150, imgHeight + 210)
  121. ctx.draw()
  122. setTimeout(() => {
  123. this.canvasToImage()
  124. }, 500)
  125. }
  126. })
  127. }
  128. },
  129. canvasToImage () {
  130. let that = this;
  131. //canvas生成图片
  132. wx.canvasToTempFilePath({
  133. x: 0,
  134. y: 0,
  135. width: that.windowWidth,
  136. height: that.imgHeight + 300,
  137. destWidth: that.windowWidth,
  138. destHeight: that.imgHeight + 300,
  139. canvasId: 'myCanvas',
  140. success: (res) => {
  141. console.log(res.tempFilePath);
  142. //保存图片到本地
  143. wx.saveImageToPhotosAlbum({
  144. filePath: res.tempFilePath,
  145. success: (res) => {
  146. wx.showModal({
  147. title: '存图成功',
  148. content: '图片成功保存到相册了,去发朋友圈',
  149. showCancel:false,
  150. confirmText:'我知道了',
  151. confirmColor:'#72B9C3',
  152. success: (res) => {
  153. if (res.confirm) {
  154. console.log('用户点击确定');
  155. this.setData({
  156. flag: !this.data.flag
  157. })
  158. }
  159. //that.hideShareImg()
  160. }
  161. })
  162. }
  163. })
  164. },
  165. fail:function (res) {
  166. console.log(res)
  167. }
  168. })
  169. }
  170. }
  171. })