-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgerarQrCode.min.js
1 lines (1 loc) · 1.92 KB
/
gerarQrCode.min.js
1
import e from"https://cdn.jsdelivr.net/npm/qrcode@1.5.4/+esm";const t=async({elementoId:t,valor:r,tipo:o,opcoes:n={}})=>{try{if(!t||!r||!o)throw new Error("Parâmetros obrigatórios não fornecidos");const a=document.getElementById(t);if(!a)throw new Error(`Elemento com ID "${t}" não encontrado`);const c={width:256,height:256,margin:1,color:{dark:"#000000",light:"#FFFFFF"},errorCorrectionLevel:"H",version:void 0,maskPattern:void 0};"pix"===o.toLowerCase()&&(c.errorCorrectionLevel="Q",c.margin=0,c.width=350,c.height=350);const i={...c,...n},s=function(e,t){switch(t.toLowerCase()){case"pix":return"string"==typeof e&&(e.startsWith("00020126")||e.includes("BR.GOV.BCB.PIX"))?e:"object"==typeof e?function(e){if("string"==typeof e)return e;if(e.dadosEmv||e.payload)return e.dadosEmv||e.payload;const{chave:t,valor:r,beneficiario:o,cidade:n,identificador:a}=e;return""}(e):e;case"url":return e.startsWith("http")?e:`https://${e}`;case"email":return`mailto:${e}`;case"tel":return`tel:${e.replace(/\D/g,"")}`;case"sms":return`sms:${e.replace(/\D/g,"")}`;case"wifi":const{nomeRede:t,senha:r,criptografia:o="WPA"}="object"==typeof e?e:{};return`WIFI:T:${o};S:${t};P:${r};;`;case"contato":const{nome:n,telefone:a,email:c}="object"==typeof e?e:{};return`BEGIN:VCARD\nVERSION:3.0\nN:${n}\nTEL:${a}\nEMAIL:${c}\nEND:VCARD`;default:return String(e)}}(r,o);let l=a.querySelector("canvas");return l||(l=document.createElement("canvas"),a.innerHTML="",a.appendChild(l)),await e.toCanvas(l,s,i),l.className=`qrcode-${o.toLowerCase()}`,function(e){const t=new ResizeObserver(function(e,t){let r;return function(...o){const n=()=>{clearTimeout(r),e(...o)};clearTimeout(r),r=setTimeout(n,t)}}((e=>{for(const t of e){const e=t.target.querySelector("canvas");if(e){const{width:r,height:o}=t.contentRect,n=Math.min(r,o);e.style.width=`${n}px`,e.style.height=`${n}px`}}}),250));t.observe(e)}(a),!0}catch(e){return console.error("Erro ao gerar QR Code:",e),!1}};export{t as gerarQRCode};