收藏
回答

weapp-qrcode-canvas-2d在微信小程序中生成二维码,新版canvas-2d接口?

// pages/QRcodeShow/QRcodeShow.js
import drawQrcode from 'weapp-qrcode-canvas-2d'
Page({


  /**
   * 页面的初始数据
   */
  data: {
    visitor"1231!!!~~fjajfa",
    name'孙卓',
    age'十六',
    yytime:'2022-12-23 上午10:00',
    QRcodeImg'https://gitee.com/fullhope/threejs-example-for-miniprogram/raw/master/demo/qrcode.jpg',  // 二维码路径
    fkMsg"",
  },
  
  // 全屏预览二维码图片,预览过程可以转发好友 保存二维码图片等操作
  previewImage:function(){
    wx.showShareImageMenu({
        paththis.data.QRcodeImg,
        successres =>{
            console.log(res);
        },
        failerr =>{
            console.log(err);
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoadfunction (options{
    let that = this;
    const query = wx.createSelectorQuery()


    query.select('#myQrcode')
        .fields({
            nodetrue,
            sizetrue
        })
        .exec(async (res) => {
            var canvas = res[0].node
            console.log(res);
            var img = canvas.createImage();
            console.log(img);
            img.src = "../images/yinxinlogo.png"  // logo图片
    
            img.onload = function () {
                // img.onload完成后才能调用 drawQrcode方法
    
                var options = {
                    canvas: canvas, // 画布标识,传入canvas组件实例
                    canvasId'myQrcode'// 绘制的canvasId
                    width260,// 二维码宽度
                    padding30,// 空白内边距
                    paddingColor'#fff'// 内边距颜色
                    background'#fff'// 二维码背景颜色,默认值白色
                    foreground'#000000'// 二维码前景色,默认值黑色
                    text'https://mp.weixin.qq.com/a/~-Hp6Aa6KoKu03rqbjSbfBQ~~'+that.data.visitor, // 二维码内容
                    image: { // 在 canvas 上绘制图片,层级高于二维码
                        imageResource: img,
                        width50// 建议不要设置过大,以免影响扫码
                        height50// 建议不要设置过大,以免影响扫码
                        roundtrue // Logo图片是否为圆形
                    }
                }


                drawQrcode(options)// 调用方法drawQrcode生成二维码
                // that.setData({
                //     fkMsg: options
                // })
                console.log(options.text);
                // let fkMsg = options.text
                // console.log(fkMsg);
                // console.log("访客信息:"+fkMsg.substr(fkMsg.lastIndexOf("~~")+2,4));
                // console.log("访客姓名:"+fkMsg.substr(fkMsg.lastIndexOf("姓名:") + 3,2)); // 访客姓名
                // console.log("访客年龄:"+fkMsg.substr(fkMsg.lastIndexOf("年龄:") + 3,2)); // 访客年龄
                // console.log("访客预约时间:"+fkMsg.substr(fkMsg.lastIndexOf("预约时间:") + 5,18)); // 访客预约时间
                // 获取临时路径(得到之后,想干嘛就干嘛了)
                wx.canvasToTempFilePath({
                    x0,
                    y0,
                    width260,
                    height260,
                    destWidth600,
                    destHeight600,
                    canvasId'myQrcode',
                    canvas: canvas,
                    success(res) {
                        console.log(res);
                        console.log('二维码临时路径为:', res.tempFilePath);
                        that.setData({
                            QRcodeImg: res.tempFilePath
                        })
                    },
                    fail(res) {
                        console.error(res)
                    }
                })
    
            };
        })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReadyfunction () {


  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShowfunction (res{
    var obj = wx.getEnterOptionsSync();
    console.log(obj);
    console.log('启动小程序的路径:', obj.path);
    console.log('启动小程序的场景值:', obj.scene);
    console.log('启动小程序的 query 参数:', obj.query);
    console.log('来源信息:', obj.shareTicket);
    console.log('来源信息参数appId:', obj.referrerInfo.appId);
    console.log('来源信息传过来的数据:', obj.referrerInfo.extraData);
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHidefunction () {


  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnloadfunction () {


  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefreshfunction () {


  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottomfunction () {


  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessagefunction () {


  }
})
回答关注问题邀请回答
收藏

1 个回答

  • 小勇童学🍊
    小勇童学🍊
    2021-12-24
                        text: 'https://mp.weixin.qq.com/a/~-Hp6Aa6KoKu03rqbjSbfBQ~~'+that.data.visitor, // 二维码内容
    

    自定义参数为啥只能是文字呢 文字可以进入

    特殊字符或者字母数字扫描二维码进入不了小程序

    2021-12-24
    有用
    回复
登录 后发表内容