Blogger Information
Blog 25
fans 0
comment 0
visits 42128
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
生成小程序码 包含参数
程先生的博客
Original
1217 people have browsed it

生成小程序码可以分享到朋友圈,也可以作为用户到店自提核销凭证,云开发生成小程序码核心代码如下:

一、通过云函数Qrcode调用

cloud.openapi.wxacode.createQRCode接口获得从微信服务器传回的二进制文件流buffer,直接上传到云存储,获得fileID

实例

exports.main = async (event, context) => {
  try {
    //获得二进制文件流
    const fStream = await cloud.openapi.wxacode.createQRCode({
      path: 'shop/index/index',
      width: 430
    })
  //  把文件流上传到云存储里
    return await cloud.uploadFile({
    cloudPath: 'qrcode.jpg',
    fileContent: fStream.buffer})
  } catch (err) {
    console.log(err)
    return err
  }
}

注意:云调用的接口需在云函数目录的config.json里提前注册才可以调用

二、在小程序端,根据fileID下载文件到本地,保存到相册,代码:

实例

downloadQrcode: function(event){
    wx.cloud.callFunction({
      name:'Qrcode',
      success: res =>{
        console.log(res)
        wx.cloud.downloadFile({
          fileID: res.result.fileID
        }).then(res => {
          // get temp file path
          console.log(res.tempFilePath)
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success:res=>{
              wx.showToast({
                title: '保存成功',
              })
            }
          })
        }).catch(error => {
          // handle error
        })
      }
    })
  }



Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post