安装依赖
npm install --save qrcodejs2
vue页面引用
<script>
import QRCode from 'qrcodejs2'
import axios from 'axios'
import JSZip from 'jszip'
import FileSaver from 'file-saver'
export default{
components:{
QRCode
},
}
</sscript>
HTML vue语法循环生成二维码 并动态生成装二维码盒子
用后台抛的数据循环生成二维码
async returnCode(row){
this.CodeDialog = true
this.title = '导出二维码'
try {
let res = await this.$operationRequest.operationRequest({parkId:row.ParkinglotID},'GetParkinglotQRCode','GET');
let jsonData = JSON.parse(res.datas)
this.CodeList = JSON.parse(jsonData.list)
this.CodeList.forEach((item,index)=>{
this.$nextTick(()=>{
let dom = document.getElementById('qrcode'+index)
dom.innerHTML = ''
this.qrcode(dom,item)
})
})
} finally {
}
},
qrcode (dom,val) {
let qrcode = new QRCode(dom, {
width: 200,
height: 200,
text: 'https://parks.borchaw.com/sp_mb/'+val.QRCode
})
},
批量下载(处理为一个压缩包)或当下载二维码
getFile(url) {
return new Promise((resolve, reject) => {
axios({method: 'get', url, responseType: 'arraybuffer',}).then((data) => {
resolve(data.data)
}).catch((error) => {
reject(error.toString())
})
})
},
DownCodeImg(val){
if(val){
let dom = document.getElementById(val)
let url = dom.childNodes[1].src
let link = document.createElement('a')
fetch(url).then(res => res.blob()).then(blob => {
link.href = URL.createObjectURL(blob)
link.download = 'pic'
document.body.appendChild(link)
link.click()
})
} else {
const zip = new JSZip();
const promises = [];
this.CodeList.forEach((item,index) => {
let dom = document.getElementById('qrcode'+index)
let url = dom.childNodes[1].src
const promise = this.getFile(url).then((data) => {
let nameList = url.split('.')
let fileName = item.devName+index+'.png'
zip.file(fileName, data, { binary: true })
});
promises.push(promise)
});
Promise.all(promises).then(() => {
zip.generateAsync({ type: 'blob' }).then((content) => {
FileSaver.saveAs(content, this.Down_pack_name +'.zip')
})
}).catch((err) => {
this.$message.error(err || '文件压缩失败')
})
}
},
|