PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 生成小程序码 包含参数

生成小程序码 包含参数

程先生的博客
程先生的博客 原创
2019年09月02日 23:17:45 1183浏览

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

一、通过云函数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
        })
      }
    })
  }



声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议