微信小程序随机生成二维码

本文介绍了如何在微信小程序中使用weapp.qrcode.js库来生成二维码。首先引入模块,然后在data中定义二维码图片路径。接着展示WXML和WXSS代码,说明布局和样式。在JS代码部分,详细解释了如何生成二维码并将其转换为临时文件路径,以及如何结合用户信息和背景图生成分享图片。
摘要由CSDN通过智能技术生成

微信小程序随机生成二维码思路
利用weapp.qrcode.esm随机生成二维码工具
本文章使用的是weapp.qrcode.js v1.1.2版本
首先引入模块

import drawQrcode from '../../utils/weapp.qrcode.esm'

在data数据里面添加一个二维码图片的路径

 // 二维码图片路径
        QRcodeUrl: ''

wxml代码

<view class="bg" bindtap='hideview' style='display:{{display}}'></view>
<view class="show" bindtap='hideview' style='display:{{display}}'>
  <view class='containera'>
    <image class="imagesa" src="{{shareImgSrc}}"></image>
    <view class='fourlayoutstylea'>
      <button size="{{primarySize}}" bindtap="showLocal"> 保存分享图片 </button>
    </view>
    <view class="canvas-box">
      <canvas canvas-id="myCanvas" style="width:100%;height:{{windowHeight}}px;"></canvas>
    </view>
  </view>
</view>
//画布里面存放的是二维码
<canvas type="2d" style="width: 260px; height: 260px;" id="myQrcode" style="display: none;"></canvas>

wxss代码,样式可以自己调整

.bg {
	display: none;
	position: absolute;
	top: 0%;
	left: 0%;
	width: 100%;
	height: 100%;
	background-color: black;
	z-index: 1001;
	-moz-opacity: 0.7;
	opacity: 0.70;
	filter: alpha(opacity=70);
  }
  
  .show {
	text-align: center;
	position: absolute;
	top: 5%;
	left: 10%;
	width: 80%;
	height: 90%;
	z-index: 1002;
  }
  
  .canvas-box {
	height: 100%;
	width: 100%;
	position: fixed;
	left: 0;
	top: 999999rpx;
  }
  
  .containera {
	display: flex;
	flex-direction: column;
	width: 100%;
	height: 100%;
	position: relative;
  }
  
  image {
	width: 100%;
	height: 75%;
  }
  
  .fourlayoutstylea {
	width: 50%;
	height: 200rpx;
	align-items: center;
	position: absolute;
	bottom: 0;
	left: 50%;
	margin-left: -25%;
  }
.share image{
	width: 60rpx;
	height: 60rpx;
}
.share{
	float: right;
}
.content{
	padding: 30rpx;
}

js代码

async onLoad() {
        const result = await request({
            url: '/common/notice/getAll'
        })
        console.log(result)
        this.setData({
            noteList: result
        })
        /**
         * 生成二维码
         */
        const that = this
        const query = wx.createSelectorQuery()
        query.select('#myQrcode')
            .fields({
                node: true,
                size: true
            })
            .exec((res) => {
                var canvas = res[0].node
                console.log('这是公告内容', that.data.noteList[0].noticeDes)

                // 调用方法drawQrcode生成二维码
                drawQrcode({
                    canvas: canvas,
                    canvasId: 'myQrcode',
                    width: 260,
                    padding: 30,
                    background: '#ffffff',
                    foreground: '#000000',
                    text: that.data.noteList[0].noticeDes,
                })

                // 获取临时路径(得到之后,想干嘛就干嘛了)
                wx.canvasToTempFilePath({
                    canvasId: 'myQrcode',
                    canvas: canvas,
                    x: 0,
                    y: 0,
                    width: 260,
                    height: 260,
                    destWidth: 260,
                    destHeight: 260,
                    success(res) {
                        console.log('二维码临时路径:', res.tempFilePath)
                        that.setData({
                            QRcodeUrl: res.tempFilePath
                        })
                        console.log(that.data.QRcodeUrl)
                    },
                    fail(res) {
                        console.error(res)
                    }
                })
            })
    },
    onShareAppMessage: function () {
        return {
            title: '筑梦回收',
            desc: '最好用的回收利器!',
            path: 'pages/notice/notice'
        }
    },
    shareOther: function (event) {
        wx.showLoading({
            title: '生成中',
        })
        this.setData({
            display: "block",
        })
        var that = this;
        // 传入头像和名称
        const user = wx.getStorageSync('user')
        const avatarUrl = user.avatarUrl
        const name = user.nickName
        that.onshow(name, avatarUrl)
    },
    //   传入
    onshow: function (name, img) {

        var that = this;
        //1. 请求后端API生成小程序码
        //2. canvas绘制文字和图片
        const ctx = wx.createCanvasContext('myCanvas');
        //背景图
        var bgImgPath = '../../image/公告.jfif'
        var imgPath = this.data.QRcodeUrl; //二维码
        ctx.drawImage(bgImgPath, 0, 0, this.data.windowWidth, 280);

        ctx.setFillStyle('white')
        ctx.fillRect(0, 280, 300, 120);
        ctx.drawImage(imgPath, 190, 290, 100, 100);
        ctx.drawImage(img, 10, 290, 40, 40)
        //绘制字体
        ctx.setFontSize(14)
        ctx.setFillStyle('#6F6F6F')
        ctx.fillText(name, 63, 314)

        ctx.setFontSize(15)
        ctx.setFillStyle('#111111')
        ctx.fillText('最新公告已发布,快来看看吧!', 10, 350)
        ctx.fillText(name + '在智慧社区', 10, 370)

        ctx.setFontSize(10)
        ctx.fillText('长按扫码查看详情', 10, 390)
        ctx.draw(false, setTimeout(function () {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: that.data.windowWidth,
                height: that.data.windowHeight,
                destWidth: that.data.windowWidth * 2,
                destHeight: that.data.windowHeight * 2,
                canvasId: 'myCanvas',
                success: function (res) {
                    console.log(res.tempFilePath);
                    wx.hideLoading();
                    that.setData({
                        shareImgSrc: res.tempFilePath
                    })

                },
                fail: function (res) {
                    wx.hideLoading();
                    wx.showToast({
                        title: '生成失败',
                        icon: "none"
                    })
                }
            })
        }, 200));
    }

实现的效果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值