添加:二维码下载功能

This commit is contained in:
wuhui_zzw 2024-03-03 13:30:48 +08:00
parent 8d1b812ec8
commit 16694b3e82
2 changed files with 61 additions and 41 deletions

View File

@ -70,6 +70,7 @@
"nprogress": "0.2.0", "nprogress": "0.2.0",
"path-to-regexp": "2.4.0", "path-to-regexp": "2.4.0",
"qiniu-js": "^3.4.1", "qiniu-js": "^3.4.1",
"qrcode": "^1.5.1",
"qrcodejs2": "0.0.2", "qrcodejs2": "0.0.2",
"screenfull": "4.2.0", "screenfull": "4.2.0",
"script-loader": "0.7.2", "script-loader": "0.7.2",

View File

@ -2,15 +2,6 @@
<div class="divBox"> <div class="divBox">
<!--主要内容--> <!--主要内容-->
<el-card class="box-card"> <el-card class="box-card">
<div>
<canvas
id="QRCode_header"
ref="canvas"
title="扫描二维码"
></canvas>
</div>
<!--顶部搜索栏--> <!--顶部搜索栏-->
<div slot="header" class="clearfix"> <div slot="header" class="clearfix">
<div class="container"> <div class="container">
@ -106,10 +97,6 @@
@current-change="pageUserChange" @current-change="pageUserChange"
/> />
</div> </div>
</el-card> </el-card>
</div> </div>
</template> </template>
@ -117,7 +104,10 @@
<script> <script>
import {exchangeCodeActivateForm, exchangeCodeBatchList, exchangeCodeEditForm, exchangeCodeList} from "@/api/user"; import {exchangeCodeActivateForm, exchangeCodeBatchList, exchangeCodeEditForm, exchangeCodeList} from "@/api/user";
import createWorkBook from "@/utils/newToExcel"; import createWorkBook from "@/utils/newToExcel";
import QRCode from "qrcodejs2"; import QRCodeOld from "qrcodejs2";
import QRCode from "qrcode";
import JSZip from "jszip";
import FileSaver from "file-saver";
export default { export default {
name: "preSaleProductList", name: "preSaleProductList",
@ -159,13 +149,13 @@ export default {
_this.$nextTick(function () { _this.$nextTick(function () {
Object.values(res.data.list).forEach(item => { Object.values(res.data.list).forEach(item => {
let refName = 'qrCodeImg'+item.id; let refName = 'qrCodeImg'+item.id;
new QRCode(_this.$refs[refName], { new QRCodeOld(_this.$refs[refName], {
text: item.exchange_code, // text: item.exchange_code, //
width: 80, width: 80,
height: 80, height: 80,
colorDark: '#000000', colorDark: '#000000',
colorLight: '#ffffff', colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H, correctLevel: QRCodeOld.CorrectLevel.H,
}); });
}) })
}); });
@ -245,39 +235,68 @@ export default {
}) })
}, },
// //
downloadQrCode(exportData){ async downloadQrCode(exportData){
let _this = this; let _this = this;
let list = exportData.list || {}; let list = exportData.list || {};
// //
let opts = { const data = [];
errorCorrectionLevel: "H",//, for (const item of list) {
type: "image/png",// const base64 = await this.textQrcodeToBase64(item[2]);
quality: 0.3,// data.push({
margin: 5,// name: item[2],
width: 200,// value: base64,
height: 200,// });
text: "",// }
//
this.dataUrlZip(data);
},
/**
* 将字符串生成二维码并且转成base64
* @param {要生成二维码的字符串} text
* @returns
*/
textQrcodeToBase64(text) {
return new Promise((res, rej) => {
QRCode.toDataURL(
text, //
{
type: "image/png", // dataurl
width: 200, //
quality: 0.8, //
margin: 1, //
color: { color: {
light: "#eaeaea"// dark: "#000", //
} light: "#fff", //
}; },
Object.values(list).forEach( item => { },
opts.text = item[2]; (err, dataUrl)=> {
QRCode.Drawing(_this.$refs.canvas, opts, function (error) { if (err) rej(err);
console.log(error) res(dataUrl.substring(22)); // substring(22)base64
if(error){
console.log('加载失败!');
} }
);
}); });
},
/**
* 将base64字符串以png格式装进jszip, 然后下载保存到本地
* @param {Array} data {value: base64字符串, name: 二维码的名字}
*/
dataUrlZip(data) {
const zip = new JSZip();
for (const item of data) {
const { value, name } = item;
zip.file(name + ".png", value, {
base64: true,
}); });
console.log(exportData)
} }
// file-saver
zip.generateAsync({ type: "blob" }).then( (content) =>{
FileSaver.saveAs(content, "qrcodes.zip");
});
},
}, },
}; };