2 lines
1.6 KiB
JavaScript
2 lines
1.6 KiB
JavaScript
import{p as t}from"./api.B1J5tFT_.js";async function e(e,a=".jpg"){const n=await t("/business/CosUpload/presignUrl",{count:e,ext:a},{timeout:12e4});if(200!==n.code)throw new Error(n.msg||"获取上传地址失败");return n.data}function a(t,e){return new Promise(((a,n)=>{(async function(t,e){let a;if(e.startsWith("data:")){const t=await fetch(e);a=await t.blob()}else{const t=await fetch(e);a=await t.blob()}const n=await fetch(t,{method:"PUT",headers:{"Content-Type":"image/jpeg"},body:a});if(!n.ok)throw new Error(`COS上传失败: ${n.status}`)})(t,e).then(a).catch(n)}))}function n(t,e,a){return"string"==typeof e&&(e=[e]),new Promise((a=>{const n=setTimeout((()=>{console.warn("[watermark] timeout"),a(t)}),1e4);(function(t,e,a){return function(t,e){return new Promise(((a,n)=>{const o=new Image;o.crossOrigin="anonymous",o.onload=()=>{try{const t=o.naturalWidth,n=o.naturalHeight,r=document.createElement("canvas");r.width=t,r.height=n;const i=r.getContext("2d");i.drawImage(o,0,0,t,n),function(t,e,a,n,o){const r=Math.max(Math.floor(.03*e),14),i=Math.floor(.8*r),s=r+i,c=s*n.length+i;o?(t.setFillStyle("rgba(0,0,0,0.4)"),t.fillRect(0,a-c,e,c),t.setFillStyle("#ffffff"),t.setFontSize(r),n.forEach(((e,n)=>{t.fillText(e,i,a-c+i+s*n)}))):(t.fillStyle="rgba(0,0,0,0.4)",t.fillRect(0,a-c,e,c),t.fillStyle="#ffffff",t.font=`${r}px sans-serif`,t.textBaseline="middle",n.forEach(((e,n)=>{t.fillText(e,i,a-c+i/2+s*n+r/2)})))}(i,t,n,e,!1),a(r.toDataURL("image/jpeg",.9))}catch(t){n(t)}},o.onerror=n,o.src=t}))}(t,e)})(t,e).then((e=>{clearTimeout(n),a(e||t)})).catch((e=>{clearTimeout(n),console.warn("[watermark]",e),a(t)}))}))}export{n as a,e as g,a as u};
|