当前位置:   article > 正文

微信小程序生成二维码海报并分享

小程序生成二维码

背景:点击图标,生成海报后,点击保存相册,可以保存

生成海报:插件wxa-plugin-canvas,此处使用页面异步生成组件方式,官网地址:wxa-plugin-canvas - npm

二维码:调用后端接口生成二维码

1.二维码按钮

  1. <!-- 二维码按钮 -->
  2. <view class="item" bindtap="onCreatePoster">
  3. <van-icon name="scan" size="20px" />
  4. <view class="icon-title">
  5. 二维码
  6. </view>
  7. </view>

2.二维码海报显示图层

  1. <!-- 二维码海报:注意布局要和其他元素独立 -->
  2. <view bindtap="closePoster">
  3. <!-- 一定要设置元素的id="poster" -->
  4. <poster id="poster" config="{{posterConfig}}" bind:success="onPosterSuccess" bind:fail="onPosterFail"></poster>
  5. <view wx:if="{{posterShow}}" class="popup-mask"></view>
  6. <view wx:if="{{posterShow}}" class="posterImg-box">
  7. <image mode="widthFix" class="posterImg" src="{{posterImg}}"></image>
  8. <view class="btn-create" data-pic="{{basicInfo.pic}}" catchtap="savePosterPic">保存到相册</view>
  9. </view>
  10. </view>

3.点击按钮后异步生成海报

需要调用获取图片信息接口wx.getImageInfo(),获取到图片的宽高以做整体宽高配置

  1. /**
  2. * 异步生成海报
  3. */
  4. async onCreatePoster() {
  5. console.log("异步生成海报");
  6. // 获取二维码信息(实质是后端生成的一张二维码图片)
  7. const qrRes = await createQRCode({id: this.data.basicInfo.id});
  8. console.log(qrRes,"qrRes");
  9. // 获取图片信息,图片获取成功后调用方法生成海报
  10. const pic = this.data.basicInfo.pic;
  11. wx.getImageInfo({
  12. src: pic,
  13. success:(res)=> {
  14. console.log(res.width)
  15. // console.log(res.height)
  16. const height = 490 * res.height / res.width
  17. // setData配置数据,数据配置完成后,生成海报
  18. this.createPosterDone(height, qrRes.data);
  19. }
  20. })
  21. },
  22. createPosterDone(picHeight,qrCode){
  23. const _this = this
  24. const _baseHeight = 74 + (picHeight + 120)
  25. this.setData({
  26. posterConfig: {
  27. // 海报总宽高
  28. width: 750,
  29. height: picHeight + 660,
  30. backgroundColor: '#fff',
  31. debug: false,
  32. // 图片所在容器起始位置,宽高等配置
  33. blocks: [{
  34. x: 76,
  35. y: 74,
  36. width: 604,
  37. height: picHeight + 120,
  38. borderWidth: 2,
  39. borderColor: '#c2aa85',
  40. borderRadius: 8
  41. }],
  42. // 图片配置
  43. images: [{
  44. x: 133,
  45. y: 133,
  46. url: _this.data.goodsInfoList.basicInfo.pic, // 商品图片
  47. width: 490,
  48. height: picHeight
  49. },
  50. {
  51. x: 76,
  52. y: _baseHeight + 199,
  53. url: qrCode, // 二维码
  54. width: 222,
  55. height: 222
  56. }
  57. ],
  58. // 文字信息:商品标题、价格、二维码处文字
  59. texts: [{
  60. x: 375,
  61. y: _baseHeight + 80,
  62. width: 650,
  63. lineNum: 2,
  64. text: _this.data.goodsInfoList.basicInfo.name,
  65. textAlign: 'center',
  66. fontSize: 40,
  67. color: '#333'
  68. },
  69. {
  70. x: 375,
  71. y: _baseHeight + 180,
  72. text: '¥' + _this.data.goodsInfoList.basicInfo.minPrice,
  73. textAlign: 'center',
  74. fontSize: 50,
  75. color: '#e64340'
  76. },
  77. {
  78. x: 352,
  79. y: _baseHeight + 320,
  80. text: '长按识别小程序码',
  81. fontSize: 28,
  82. color: '#999'
  83. }
  84. ],
  85. }
  86. }, () => {
  87. Poster.create(this.data.posterConfig, this);
  88. });
  89. },

4.海报生成成功后将海报数据(海报临时路径)和是否显示海报层,存放到data中

  1. data:{
  2. // 二维码海报配置
  3. posterConfig:{},
  4. // poster显示标识
  5. posterShow: false,
  6. // 保存到相册的图片
  7. posterImg: ''
  8. },
  9. onPosterSuccess(e){
  10. console.log("二维码生成成功");
  11. this.setData({
  12. posterImg: e.detail,
  13. posterShow: true
  14. })
  15. },

5.点击海报任何位置,除了保存到相册按钮,隐藏海报层

closePoster方法绑定到最外层

保存按钮使用catchbind阻止冒泡,即可防止点击保存时也会关掉海报层

  1. <!-- 二维码海报:注意布局要和其他元素独立 -->
  2. <view bindtap="closePoster">
  3. <!-- 一定要设置元素的id="poster" -->
  4. <poster id="poster" config="{{posterConfig}}" bind:success="onPosterSuccess" bind:fail="onPosterFail"></poster>
  5. <view wx:if="{{posterShow}}" class="popup-mask"></view>
  6. <view wx:if="{{posterShow}}" class="posterImg-box">
  7. <image mode="widthFix" class="posterImg" src="{{posterImg}}"></image>
  8. <view class="btn-create" data-pic="{{basicInfo.pic}}" catchtap="savePosterPic">保存到相册</view>
  9. </view>
  10. </view>
  1. // 关闭海报
  2. closePoster(){
  3. this.setData({
  4. posterShow: false
  5. })
  6. },

6.点击保存到相册按钮,调用wx.saveImageToPhotosAlbum()方法保存图片到本地

注意wx.saveImageToPhotosAlbum()方法的参数filePath不能是绝对路径或者网络图片,必须是临时图片。所以在生成海报成功后需要将图片保存到data的posterImg中,保存时用这个就可以了

  1. // 保存海报到相册
  2. savePosterPic(e){
  3. console.log("保存到相册",e);
  4. console.log(this.data.posterImg);//http://tmp/xlHB02MBJ50H9887bf9a40b5b5dc24b904e4132afcb0.png
  5. wx.saveImageToPhotosAlbum({
  6. // 不能直接使用this.data.basicInfo.pic的图片
  7. // "saveImageToPhotosAlbum:fail https://file.winwebedu.com/mall/collage-01.jpg not absolute path"
  8. filePath: this.data.posterImg,
  9. success(res) {
  10. wx.showToast({
  11. title: '保存成功',
  12. })
  13. },
  14. fail(err){
  15. console.log(err);
  16. wx.showToast({
  17. title: '保存失败',
  18. })
  19. },
  20. // 无论成功与否关闭海报
  21. complete(){
  22. this.setData({
  23. posterShow: false
  24. });
  25. }
  26. })
  27. },

7.补充,如果要直接生成二维码不使用异步

  1. <poster class="wxcode-box" id="poster" config="{{posterConfig}}" bind:success="onPosterSuccess" bind:fail="onPosterFail">
  2. </poster>
  1. onPosterSuccess(e){
  2. // console.log("二维码生成成功");
  3. const { detail } = e;
  4. console.log(detail);
  5. wx.previewImage({
  6. current: detail,
  7. urls: [detail]
  8. })
  9. },
  10. posterConfig配置:
  11. jdConfig: {
  12. width: 750,
  13. height: 1334,
  14. backgroundColor: '#fff',
  15. debug: false,
  16. pixelRatio: 1,
  17. blocks: [
  18. {
  19. width: 690,
  20. height: 808,
  21. x: 30,
  22. y: 183,
  23. borderWidth: 2,
  24. borderColor: '#f0c2a0',
  25. borderRadius: 20,
  26. },
  27. {
  28. width: 634,
  29. height: 74,
  30. x: 59,
  31. y: 770,
  32. backgroundColor: '#fff',
  33. opacity: 0.5,
  34. zIndex: 100,
  35. },
  36. ],
  37. texts: [
  38. {
  39. x: 113,
  40. y: 61,
  41. baseLine: 'middle',
  42. text: '伟仔',
  43. fontSize: 32,
  44. color: '#8d8d8d',
  45. },
  46. {
  47. x: 30,
  48. y: 113,
  49. baseLine: 'top',
  50. text: '发现一个好物,推荐给你呀',
  51. fontSize: 38,
  52. color: '#080808',
  53. },
  54. {
  55. x: 92,
  56. y: 810,
  57. fontSize: 38,
  58. baseLine: 'middle',
  59. text: '标题标题标题标题标题标题标题标题标题',
  60. width: 570,
  61. lineNum: 1,
  62. color: '#8d8d8d',
  63. zIndex: 200,
  64. },
  65. {
  66. x: 59,
  67. y: 895,
  68. baseLine: 'middle',
  69. text: [
  70. {
  71. text: '2人拼',
  72. fontSize: 28,
  73. color: '#ec1731',
  74. },
  75. {
  76. text: '¥99',
  77. fontSize: 36,
  78. color: '#ec1731',
  79. marginLeft: 30,
  80. }
  81. ]
  82. },
  83. {
  84. x: 522,
  85. y: 895,
  86. baseLine: 'middle',
  87. text: '已拼2件',
  88. fontSize: 28,
  89. color: '#929292',
  90. },
  91. {
  92. x: 59,
  93. y: 945,
  94. baseLine: 'middle',
  95. text: [
  96. {
  97. text: '商家发货&售后',
  98. fontSize: 28,
  99. color: '#929292',
  100. },
  101. {
  102. text: '七天退货',
  103. fontSize: 28,
  104. color: '#929292',
  105. marginLeft: 50,
  106. },
  107. {
  108. text: '运费险',
  109. fontSize: 28,
  110. color: '#929292',
  111. marginLeft: 50,
  112. },
  113. ]
  114. },
  115. {
  116. x: 360,
  117. y: 1065,
  118. baseLine: 'top',
  119. text: '长按识别小程序码',
  120. fontSize: 38,
  121. color: '#080808',
  122. },
  123. {
  124. x: 360,
  125. y: 1123,
  126. baseLine: 'top',
  127. text: '超值好货一起拼',
  128. fontSize: 28,
  129. color: '#929292',
  130. },
  131. ],
  132. images: [
  133. {
  134. width: 62,
  135. height: 62,
  136. x: 30,
  137. y: 30,
  138. borderRadius: 62,
  139. url: 'https://img.yzcdn.cn/vant/cat.jpeg',
  140. },
  141. {
  142. width: 634,
  143. height: 634,
  144. x: 59,
  145. y: 210,
  146. url: 'https://b.yzcdn.cn/vant/icon-demo-1126.png',
  147. },
  148. {
  149. width: 220,
  150. height: 220,
  151. x: 92,
  152. y: 1020,
  153. url: 'https://img.yzcdn.cn/vant/cat.jpeg',
  154. },
  155. {
  156. width: 750,
  157. height: 90,
  158. x: 0,
  159. y: 1244,
  160. url: 'https://b.yzcdn.cn/vant/icon-demo-1126.png',
  161. }
  162. ]
  163. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/639645
推荐阅读
相关标签
  

闽ICP备14008679号