downQRCode: function(scope) {
_self.download = true; //解決canvas突然閃現的問題
let _self = this;
const smImage = 'https://goss.vcg.com/creative/vcg/800/version23/VCG41187144006.jpg';
Vue.nextTick(() => {
var c = document.getElementById("myCanvas");
var cxt = c.getContext("2d");
var img = new Image();
img.onload = function() {
var text = "http://www.baidu.com";
cxt.drawImage(img, 0, 0);//新增背景圖片
//門店名稱
cxt.textAlign="center";
cxt.fillStyle='#dcae69';
cxt.font="20px Arial";
cxt.fillText(scope.row.shopName,255, 568);
//客服電話
cxt.fillStyle='#594a53';
cxt.font="18px Arial";
cxt.fillText('客服電話:400-1888-699',255, 720);
//商戶名稱
cxt.fillStyle='#000';
cxt.fillText(`No: ${scope.row.localMctNo}`,255, 541);
$('.smImage').find('canvas').remove();
$('.smImage').qrcode({
render: "canvas",
width: 260,
height: 260,
typeNumber: -1,//計算模式
correctLevel: 2,//二維碼糾錯級別
text: text + scope.row.id,
});
var canvas = $('.smImage').find('canvas')[0];
var image = new Image();
image.src = canvas.toDataURL("image/png"); //"image/png"
//將canvas轉化為圖片新增到大的canvas中
image.onload = function() {
cxt.drawImage(image, 122, 255);
//將門店圖片劃到大的canvas中
var smimg = new Image();
smimg.setAttribute('crossOrigin', 'anonymous');//設定伺服器圖片跨域
//小圖片固定寬高60px
smimg.onload=function() {
cxt.drawImage(smimg, 215, 355, 70,70); //(smimg,posX,posY,width,height,)
_self.downloadFile(`${scope.row.shopName}.png`, c.toDataURL("image/png"));
_self.download = false;
};
smimg.src= bgimg;
}
};
img.src = bgimg;
})
},
複製程式碼
將base64轉化為blob型別
base64Img2Blob(code) {
var parts = code.split(';base64,');
var contentType = parts[0].split(':')[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
for(var i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
return new Blob([uInt8Array], {
type: contentType
});
},
複製程式碼