/* 针对系统中众多svg的图表/沙盘想要复制粘到微信中 */ import bus from '@/api/bus.js'; export const copyBlob = (url,callback=null,ratio=1,urlType='svg') => { const copyImg = new Image() copyImg.crossOrigin = 'anonymous'; copyImg.src = urlType==='svg'?svgToBase64(url):url; copyImg.onload = ()=> { const canvas = document.createElement('canvas'); canvas.width = copyImg.width * ratio; canvas.height = copyImg.height * ratio; const ctx = canvas.getContext("2d"); ctx.drawImage(copyImg,0,0,canvas.width,canvas.height) ctx.scale(ratio,ratio) callback && callback(); if(window.ClipboardItem) { canvas.toBlob(async(blob) => { console.log('toblob') console.log(blob) const data = [new ClipboardItem({ [blob.type]: blob })]; await navigator.clipboard.write(data).then( () => { bus.$message.success('复制成功!') }, () => { bus.$message.warning('浏览器暂不支持') } ); }) }else { bus.$message.warning('当前协议暂不支持,仅支持https协议') } } } /* 转base64 */ export const svgToBase64 = (svg) => { const base64img = `data:image/svg+xml;base64,${window.btoa( unescape(encodeURI(svg)) )}`; return base64img; } /* 不受粘贴板对象影响的粘贴 */ export function copyFit(value) { if(navigator.clipboard) { navigator.clipboard.writeText(value) }else { setTimeout(() => { const input = document.createElement('input') input.setAttribute('readonly','readonly') input.setAttribute('id',new Date().getTime()) input.value = value document.body.appendChild(input) input.select(); document.execCommand('copy'); document.body.removeChild(input); }) } }