<template>
<div>
<div class="con" v-if="numList.length">
<div class="qr-section" v-for="(item, index) in numList" :key="index">
<vue-qr ref="qrCode" :text="item" width="300" height="300"></vue-qr>
<span class="num">{{ item }}</span>
</div>
</div>
</div>
</template>
<script>
// 組件
import VueQr from 'vue-qr'
import html2canvas from 'html2canvas'
import JSZip from 'jszip'
import FileSaver from 'file-saver'
export default {
name: 'Qrcode',
components: {
VueQr
},
props: {
isBatch: {
type: Boolean,
default: false
},
numList: {
type: Array,
default: () => []
}
},
data () {
return { imgZipList: '', imgList: [] }
},
watch: {
isBatch: {
handler (val) {
if (val) {
this.$nextTick(() => {
this.compressedDownload()
})
}
},
deep: true
}
},
created () {},
mounted () {},
methods: {
// 壓縮下載
compressedDownload () {
// // 創(chuàng)建一個code文件夾巾陕,文件里里創(chuàng)建一個images文件菜循,文件內(nèi)容為空
this.zip = new JSZip()
this.imgZipList = this.zip.folder('images')
// 獲取每個二維碼的dom
const canvasBoxList = document.querySelectorAll('.qr-section')
canvasBoxList.forEach((item, index) => {
// 處理生成待編碼的二維碼圖片
html2canvas(item).then(canvas => {
// 獲取圖片在線地址
const dataURL = canvas.toDataURL('image/jpg')
const that = this
const total = canvasBoxList.length
// ↓第一個參數(shù)是單張二維碼圖片的命名,第二個參數(shù)是二維碼的base64涡上,這里用replace把URL的前綴截掉僅保留純base64編碼
that.imgZipList.file('二維碼_' + index + '.png', dataURL.replace(/^data:image\/(png|jpg);base64,/, ''), {
base64: true
})
if (index === total - 1) {
// 添加完下載
this.zip.generateAsync({ type: 'blob' }).then(function (content) {
// 使用file-saver保存下載zip文件浑玛,第二個參數(shù)是壓縮包命名
FileSaver.saveAs(content, `二維碼.zip`)
that.$emit('handleCancel')
})
}
})
})
}
}
}
</script>
<style lang="less" scoped>
.con {
position: absolute;
top: 0;
left: 0;
z-index: -10;
display: flex;
font-size: 15px;
font-family: PingFangSC-Regular, PingFang SC;
img {
width: 300px;
height: 300px;
}
.num {
color: #2a3030;
line-height: 21px;
text-align: center;
}
}
.qr-section {
display: flex;
flex-direction: column;
align-items: center;
width: 320px;
height: 350px;
margin: 10px auto;
span {
margin: 0 !important;
}
}
</style>
生成多張二維碼圖片桐款,并使用jszip和file-saver打包成壓縮包下載
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門拆又,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人栏账,你說我怎么就攤上這事帖族。” “怎么了挡爵?”我有些...
- 文/不壞的土叔 我叫張陵竖般,是天一觀的道長。 經(jīng)常有香客問我茶鹃,道長涣雕,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任闭翩,我火速辦了婚禮挣郭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘疗韵。我一直安慰自己兑障,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著流译,像睡著了一般逞怨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上先蒋,一...
- 文/蒼蘭香墨 我猛地睜開眼寇蚊,長吁一口氣:“原來是場噩夢啊……” “哼笔时!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起仗岸,我...
- 正文 年R本政府宣布,位于F島的核電站朦蕴,受9級特大地震影響篮条,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜梦重,卻給世界環(huán)境...
- 文/蒙蒙 一兑燥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧琴拧,春花似錦降瞳、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽除师。三九已至,卻和暖如春扔枫,著一層夾襖步出監(jiān)牢的瞬間汛聚,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 生成二維碼圖片舵稠,查看該文章:http://www.reibang.com/p/fb9a3a095d03[http...
- 1.template 利用el-table創(chuàng)建一個表格,其中表格的第三列為二維碼組件qrcode-vue 基本效果...
- 簡述:最近有接到一個需求入宦,生成二維碼哺徊,然后打包成zip ,最后下載乾闰,且不用文件服務(wù)器落追!1、spring boot ...