以下所述参考网上众多大佬略写,本人只是菜鸟一枚,记录博客主要怕自己记不住,方便以后参考,有何不足,欢迎补充…
方式方法和网上大多大佬有雷同,因为我也是参考网上各位大佬的…
引入一个js
var barcode = require('./barcode');
function convert_length(length) {
return Math.round(wx.getSystemInfoSync().windowWidth * length / 750);
}
function barc(id, code, width, height) {
barcode.code128(wx.createCanvasContext(id), code, convert_length(width), convert_length(height))
}
module.exports = {
barcode: barc,
}
在js页面引入上面的的 js 文件
const { barcode } = require('../../../../utils/payCode.js');
// 获取后台给的码
barcode('barcode' , this.data.goodsDetail.receiveCode, 520, 180);
// 获取二维码本地缓存
saveCanvas() {
// 用小程序官方给 wx.canvasToTempFilePath 方法
// 把当前画布指定区域的内容导出生成指定大小的图片。
wx.canvasToTempFilePath({
canvasId: 'barcode',
success: (res) => {
this.setData({
qrImgUrl: res.tempFilePath
})
}
})
},
// 如果条码为渲染出来,就一直请求,知道渲染出条码/二维码
getqrImgUrl() {
setTimeout(() => {
this.saveCanvas();
if (this.data.qrImgUrl == '') {
this.getqrImgUrl();
}
}, 300);
},