share.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // compontents/share/share.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. * 分享弹窗 没怎么用
  6. */
  7. properties: {
  8. },
  9. /**
  10. * 组件的初始数据
  11. */
  12. data: {
  13. flag: true,
  14. postId: '',
  15. imgUrl: []
  16. },
  17. /**
  18. * 组件的方法列表
  19. */
  20. methods: {
  21. //隐藏分享框
  22. hidePopup: function () {
  23. this.setData({
  24. flag: !this.data.flag
  25. })
  26. },
  27. //展示分享框
  28. showPopup (postId, imgUrl) {
  29. this.setData({
  30. flag: !this.data.flag,
  31. postId,
  32. imgUrl
  33. })
  34. },
  35. //保存图片
  36. saveImage (e) {
  37. const imgUrl = this.data.imgUrl;
  38. //先绘制图片
  39. for (let item of imgUrl) {
  40. // wx.downloadFile({
  41. // url: item,
  42. // success: function (res) {
  43. // console.log(res);
  44. // }
  45. // })
  46. //canvas绘制文字和图片
  47. const ctx = wx.createCanvasContext('myCanvas');
  48. var imgPath = item;
  49. ctx.setFillStyle('white')
  50. //获取屏幕宽度
  51. const windowWidth = wx.getSystemInfoSync().windowWidth;
  52. ctx.fillRect(0, 0, windowWidth, 1.6 * windowWidth)
  53. ctx.drawImage(imgPath, 30, 10, 300, 100);
  54. ctx.setFontSize(18)
  55. ctx.setFillStyle('black')
  56. ctx.fillText('我画的图片', 50, 140)
  57. ctx.draw();
  58. }
  59. //canvas生成图片
  60. wx.canvasToTempFilePath({
  61. x: 0,
  62. y: 0,
  63. width: wx.getSystemInfoSync().windowWidth,
  64. height: 0.6 * wx.getSystemInfoSync().windowWidth,
  65. destWidth: wx.getSystemInfoSync().windowWidth,
  66. destHeight:0.6 * wx.getSystemInfoSync().windowWidth,
  67. canvasId: 'myCanvas',
  68. success: (res) => {
  69. console.log('canvas'+res.tempFilePath);
  70. //保存图片到本地
  71. wx.saveImageToPhotosAlbum({
  72. filePath: res.tempFilePath,
  73. success(res) {
  74. wx.showModal({
  75. title: '存图成功',
  76. content: '图片成功保存到相册了,去发朋友圈',
  77. showCancel:false,
  78. confirmText:'好哒',
  79. confirmColor:'#72B9C3',
  80. success: function(res) {
  81. if (res.confirm) {
  82. console.log('用户点击确定');
  83. }
  84. //that.hideShareImg()
  85. }
  86. })
  87. }
  88. })
  89. },
  90. fail:function (res) {
  91. console.log(res)
  92. }
  93. })
  94. }
  95. }
  96. })