1.在小程序中引入weapp-qrcode.js到utils文件夹下:
2.在需要转化二维码的页面引入
const QRCode = require('../../utils/weapp-qrcode.js')
3.canvas的宽高需要自适应,那么需要进行尺寸转换
const W = wx.getSystemInfoSync().windowWidth; const rate = 750.0 / W; // 300rpx 在6s上为 150px const qrcodeWidth = 300 / rate;
4.生成二维码
getqrcode(item) { qrcode = new QRCode(`${item.couponUserId}`, { usingIn: this, image: '../../images/connection.png', width: qrcodeWidth, height: qrcodeWidth, colorLight: "white", correctLevel: QRCode.CorrectLevel.L }); let _urls = config.previewHost qrcode.makeCode(`${_urls}/coupon-use?id=${item.couponUserId}`); },
`${item.couponUserId}` 是wxml中canvas的id
text
为需要转化为二维码的字符串;
usingIn
为可选参数,组件中要使用,传this
image为默认生成二维码的图片
width
和height
为绘制出的二维码长宽,这里设置为跟canvas
同样的长宽;
colorDark
和colorLight
为二维码交替的两种颜色;
correctLevel二维码纠错级别,默认值为高级
如果需要再次生成二维码,调用qrcode.makeCode('text you want convert')