@jecloud/utils
Version:
JECloud 工具库
1 lines • 2.8 kB
JavaScript
var e=this&&this.__assign||function(){return e=Object.assign||function(e){for(var o,i=1,t=arguments.length;i<t;i++)for(var n in o=arguments[i])Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n]);return e},e.apply(this,arguments)};import o from"../utils/flow/flow";import{uniqueId as i,upperFirst as t,toNumber as n}from"lodash-es";import{getAxiosBaseURL as l,transformAjaxData as r,decode as s}from"../utils";import{API_DOCUMENT_FILE as a,API_DOCUMENT_DOWN as p,API_DOCUMENT_PREVIEW as u}from"./api/urls";import{createDeferred as f}from"../utils";import{getSystemConfig as d,toParamsEncrypt as c}from".";import{JE_SYS_UPLOADFILESIZE as m}from"./constant";export function uploadFile(p){var u,v,h;void 0===p&&(p={});var w=p.url,y=p.params,g=p.multiple,x=void 0!==g&&g,O=p.inputFileAttrs,F=p.isDirectory,b=void 0!==F&&F,S=n(d(m,0)),j=i("upload-file-"),E=window.document.createElement("button");E.setAttribute("id",j),E.setAttribute("style",{visposition:"fixed",left:"-1000px",top:"-1000px",visibility:"hidden"});var z=c(y);z&&(p.flowOptions=null!==(u=p.flowOptions)&&void 0!==u?u:{},p.flowOptions.headers=null!==(v=p.flowOptions.headers)&&void 0!==v?v:{},Object.assign(p.flowOptions.headers,z));var A=new o(e({target:w||l()+a,testChunks:!1,fileParameterName:"files",chunkSize:524288e3,query:y},p.flowOptions));A.assignBrowse(E,b,!x,O),null===(h=p.onFlowInit)||void 0===h||h.call(p,A),["fileAdded","fileSuccess","fileError","fileProgress","uploadStart"].forEach((function(e){var o=p["on"+t(e)];o&&A.on(e,o)}));var B=p.includeSuffix,k=void 0===B?[]:B,K=p.excludeSuffix,P=void 0===K?[]:K,U=p.onValidate,_=p.onFileError,q=p.maxSize||S,C=q>0?1024*q*1024:0;A.on("filesSubmitted",(function(e){var o=[];e.forEach((function(e){var i=[],t=e.name.split(".").pop();C>0&&e.size>C&&i.push({file:e,type:"maxSize",message:"【"+e.name+"】 超过限制上传大小:"+q+"M"}),k.length?k.includes(t)||i.push({file:e,type:"include",message:"【"+e.name+"】 不在允许上传类型内:"+k.join(",")}):P.length&&P.includes(t)&&i.push({file:e,type:"exclude",message:"【"+e.name+"】 在禁止上传类型内:"+P.join(",")}),i.length&&(A.removeFile(e),o.push.apply(o,i))})),!1!==(null==U?void 0:U({files:e,errors:o,flowObj:A}))&&A.upload()}));var D=[],I=f();return A.on("fileSuccess",(function(e,o){r(s(o)).then((function(e){D.push.apply(D,e)})).catch((function(e){null==_||_(e)}))})),A.on("complete",(function(){I.resolve(D,A)})),A.on("error",(function(){for(var e=[],o=0;o<arguments.length;o++)e[o]=arguments[o];I.reject.apply(I,e)})),E.click(),I.promise}export function downloadFile(e){window.open(getFileUrlByKey(e,"download"))}export function previewFile(e){window.open(getFileUrlByKey(e))}export function getFileUrlByKey(e,o){return void 0===o&&(o="preview"),"download"===o?""+l()+p+"/"+e:""+l()+u+"/"+e}