美文网首页
云函数生成二维码并下载保存至本地

云函数生成二维码并下载保存至本地

作者: DreamsZero | 来源:发表于2021-12-01 11:10 被阅读0次

本文主要讲述,如何用云函数生成二维码,并且其中会遇到哪些问题的解决办法

  1. 新建addQrcode函数
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
  try {
       // 生成小程序码
    const result = await cloud.openapi.wxacode.getUnlimited({
      "width":event.width,//二维码宽度
      "scene": event.scene,//二维码参数
      "page": "page",//二维码打开路径
      "check_path": false,//注意:在项目未发布之前,check_path设为false,否则会报错找不到page,注意默认值为true
      "is_hyaline":false//二维码背景是否透明
    })
      // 上传到云存储
    const file = await cloud.uploadFile({
      cloudPath: 'outlets.jpeg',
      fileContent: result.buffer,
    })
    // 上传成功,获取到的临时文件
    return file
  } catch (err) {
    return err
  }
}
  1. 云函数调用
 showQrcode(width){
    wx.showLoading({
      title: '加载中...'
    })
    wx.cloud.callFunction({
      // 云函数名称
      name: 'addQrcode',
      // 传给云函数的参数
      data: {
        scene: "a=1",
        width:width
      },
      success: (res) => {
        this.setData({ img: res.result.fileID });
        wx.hideLoading();
      },
      fail: (err)=>{
        wx.hideLoading();
      }
    })
  },
  1. 获取云函数的参数
   onLoad(options) {
    if (options.query) {
      const scene = decodeURIComponent(options.query.scene);//获取到的scene参数,注意二维码的参数需要decodeURIComponent解码
    }
   }

  1. 云函数返回的二维码下载保存到本地
 downloadImg(url) {
    let imgSrc = url;  //要保存的图片url
    wx.showLoading({
      title: '保存中...'
    })
    wx.cloud.downloadFile({    //下载文件资源到本地
      fileID: imgSrc,
      success: function (res) {
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.hideLoading()
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (err) {
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
              console.log("当初用户拒绝,再次发起授权")
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                showCancel: false,
                success: modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log("settingdata", settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限成功,再次点击图片即可保存',
                          showCancel: false,
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限失败,将无法保存到相册哦~',
                          showCancel: false,
                        })
                      }
                    },
                    fail(failData) {
                      console.log("failData", failData)
                    },
                    complete(finishData) {
                      console.log("finishData", finishData)
                    }
                  })
                }
              })
            }
          },
          complete(res) {
            console.log(res);
            wx.hideLoading()
          }
        })
      }
    })
  },

相关文章

网友评论

      本文标题:云函数生成二维码并下载保存至本地

      本文链接:https://www.haomeiwen.com/subject/nnlsxrtx.html