poster.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // pages/invitationDetails/poster/poster.js
  2. import QRCode from '../../../utils/weapp-qrcode.js'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. shareImage: "https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220811/%E4%BA%8C%E5%9B%BE/%E5%9B%BE%E7%89%87/%E9%82%80%E8%AF%B7%E6%B4%BB%E5%8A%A8%20%E5%A4%87%E4%BB%BD%202%402x.png",
  10. bgImage: '',
  11. qrcode_content: ""
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (options) {
  17. this.setData({
  18. qrcode_content: getApp().invite_id
  19. })
  20. console.log(this.data.qrcode_content,'options.qrcode_content');
  21. this.setQrcode()
  22. },
  23. /**
  24. * 生命周期函数--监听页面初次渲染完成
  25. */
  26. onReady: function () {
  27. },
  28. /**
  29. * 生命周期函数--监听页面显示
  30. */
  31. onShow: function () {},
  32. /**
  33. * 生成动态二维码
  34. */
  35. setQrcode() {
  36. new QRCode('myQrcode', {
  37. text: this.data.qrcode_content,
  38. width: 75,
  39. height: 75,
  40. padding: 0, // 生成二维码四周自动留边宽度,不传入默认为0
  41. correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
  42. callback: (res) => {
  43. this.setData({
  44. qrCode: res.path
  45. })
  46. console.log(res, 'res');
  47. // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去,看需求
  48. }
  49. })
  50. },
  51. /**
  52. * 背景图转存本地(小程序drawImage不识别cdn图片)
  53. */
  54. onSaveAlbum() {
  55. const that = this
  56. wx.downloadFile({
  57. url: this.data.shareImage,
  58. success(res) {
  59. console.log(res.tempFilePath, 'res.tempFilePath');
  60. that.setData({
  61. bgImage: res.tempFilePath
  62. })
  63. that.generateImage()
  64. }
  65. })
  66. },
  67. /**
  68. * 画图
  69. */
  70. generateImage() {
  71. const ctx = wx.createCanvasContext('myCanvas')
  72. ctx.drawImage(this.data.bgImage, 0, 0, 610, 1168)
  73. ctx.drawImage(this.data.qrCode, 230, 944, 150, 150)
  74. ctx.draw(true, () => {
  75. let that = this
  76. // 获取图片路径
  77. wx.canvasToTempFilePath({
  78. x: 0,
  79. y: 0,
  80. width: 610,
  81. height: 1168,
  82. destWidth: 610 * 2,
  83. destHeight: 1168 * 2,
  84. canvasId: 'myCanvas',
  85. success(res) {
  86. console.log(res, '图片');
  87. that.setData({
  88. pictureUrl: res.tempFilePath,
  89. })
  90. that.saveImage()
  91. }
  92. })
  93. })
  94. },
  95. /**
  96. * 保存到本地
  97. */
  98. saveImage() {
  99. wx.showToast({
  100. title: '保存成功'
  101. })
  102. wx.saveImageToPhotosAlbum({
  103. filePath: this.data.pictureUrl,
  104. success: (res) => {
  105. wx.showToast({
  106. title: '保存成功',
  107. duration: 500,
  108. })
  109. },
  110. fail: (err) => {
  111. wx.showToast({
  112. title: '获取权限失败',
  113. duration: 1000,
  114. })
  115. }
  116. })
  117. },
  118. /**
  119. * 生命周期函数--监听页面隐藏
  120. */
  121. onHide: function () {
  122. },
  123. /**
  124. * 生命周期函数--监听页面卸载
  125. */
  126. onUnload: function () {
  127. },
  128. /**
  129. * 页面相关事件处理函数--监听用户下拉动作
  130. */
  131. onPullDownRefresh: function () {
  132. },
  133. /**
  134. * 页面上拉触底事件的处理函数
  135. */
  136. onReachBottom: function () {
  137. },
  138. /**
  139. * 用户点击右上角分享
  140. */
  141. onShareAppMessage: function () {
  142. }
  143. })