2024年09月21日 建站教程
前端如何利用nodejs生成二维码,最简单方法就是利用第三方库来实现,下面web建站小编给大家介绍一个qrcode的实现方法。
1、生成二维码代码如下:
const qrCode = require('qrcode')
class creatQRCode {
async create (ctx) {
const { text = 'Luban', options } = ctx.request.body
const qrOptions = {
type: 'image/png', //类型:image/png、image/jpeg、 image/web
width: 180, //宽度
margin: 0, //内边距
scale: 1, //放缩的倍数
color: { //颜色
dark: '#000000',
light: '#ffffff'
},
errorCorrectionLevel: 'M',
quality: 1
}
Object.assign(qrOptions, options)
const imgData = await qrCode.toDataURL(text, qrOptions)
return ctx.success({ imgData })
}
}
module.exports = new creatQRCode()
2、下载二维码代码如下:
const a = document.createElement('a')
const event = new MouseEvent('click')
a.download = '我的二维码'
a.href = this.imgSrc
a.dispatchEvent(event)
本文链接:http://so.lmcjl.com/news/13565/