| 1 |
- import{_ as e,o as t,w as o,i as r,j as l,k as n,l as a,L as c,bl as s,A as i,bm as d,bn as f,x as h}from"./index-C4aOhc7O.js";import{b as u}from"./browser.DEPKJqf2.js";/* empty css */const g=e({__name:"QRCode",props:{text:{type:String,required:!0},width:{type:Number,default:200},height:{type:Number,default:200},colorDark:{type:String,default:"#000000"},colorLight:{type:String,default:"#ffffff"},logo:{type:String,default:""},logoScale:{type:Number,default:.22}},setup(e,{expose:g}){"undefined"==typeof TextEncoder&&(globalThis.TextEncoder=class{encode(e){const t=new ArrayBuffer(e.length),o=new Uint8Array(t);for(let r=0,l=e.length;r<l;r++)o[r]=e.charCodeAt(r);return o}}),"undefined"==typeof TextDecoder&&(globalThis.TextDecoder=class{decode(e){let t=String.fromCharCode.apply(null,e);return decodeURIComponent(escape(t))}});const p=e,m=`qr_${Date.now()}_${Math.floor(1e10*Math.random()).toString().padStart(10,"0")}`;async function w(){if(console.log("开始绘制二维码:",p.text),p.text)try{await new Promise(e=>setTimeout(e,100));const e=await u.create(p.text,{errorCorrectionLevel:"H"}),t=s(m);if(!t)return void console.error("无法获取canvas上下文");const o=p.width,r=e.modules.size,l=o/r;t.setFillStyle(p.colorLight),t.fillRect(0,0,o,o);for(let n=0;n<r;n++)for(let o=0;o<r;o++)t.setFillStyle(e.modules.get(n,o)?p.colorDark:p.colorLight),t.fillRect(o*l,n*l,Math.ceil(l),Math.ceil(l));t.draw(!1,()=>{console.log("二维码绘制成功"),p.logo&&function(e){const t=p.width,o=t*p.logoScale,r=(t-o)/2,l=(t-o)/2;e.setFillStyle("#ffffff"),e.fillRect(r,l,o,o),e.drawImage(p.logo,r,l,o,o),e.draw(!0)}(t)})}catch(e){console.error("二维码绘制错误:"),console.error("错误类型:",e.constructor.name),console.error("错误信息:",e.message),console.error("错误堆栈:",e.stack),i({title:"二维码生成失败",icon:"none",duration:2e3})}else console.warn("二维码文本为空")}function y(){const e=s(m);e&&(e.clearRect(0,0,p.width,p.height),e.setFillStyle(p.colorLight),e.fillRect(0,0,p.width,p.height),e.draw(),console.log("二维码已清空"))}return t(()=>{console.log("QrCode组件已挂载,准备绘制二维码"),w()}),o(()=>p.text,e=>{console.log("二维码文本变化:",e),e?w():y()}),g({download:function(){setTimeout(()=>{d({canvasId:m,success(e){console.log("canvas转图片成功:",e.tempFilePath);const t=document.createElement("a");t.href=e.tempFilePath,t.download="qrcode.png",document.body.appendChild(t),t.click(),document.body.removeChild(t),i({title:"已保存",icon:"success"})},fail(e){console.error("canvas转图片失败:",e),i({title:"生成图片失败",icon:"error"})}})},200)},clear:y,drawQr:w}),(t,o)=>{const s=f,i=h;return r(),l(i,{class:"qr-container"},{default:n(()=>[a(s,{id:m,"canvas-id":m,class:"qr-canvas",width:e.width,height:e.height,style:c({width:e.width+"px",height:e.height+"px"})},null,8,["width","height","style"])]),_:1})}}},[["__scopeId","data-v-2bc638ff"]]);export{g as Q};
|