UNPKG

my-uniapp-tools

Version:

一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能

2 lines (1 loc) 4.52 kB
import{MESSAGES as e,DEFAULTS as t,PERFORMANCE as o}from"./constants.js";import{validateSelectedFiles as s,normalizeHeaders as n,normalizeFormData as r,normalizeExtensions as i,revokeObjectURLSafely as a,normalizeConcurrency as c}from"./utils.js";import{throttle as u}from"../utils.js";import{useToast as f}from"../ui.js";function l(e){return{toast:"function"==typeof e.toast?e.toast:f,shouldShowToast:void 0===e.showToast?t.SHOW_TOAST:Boolean(e.showToast)}}function g(e){return Boolean(e?.aborted)}function m(e){return[{file:null,success:!1,message:e}]}function p(){return m(e.UPLOAD_CANCELED)}function y(e,t){t.autoRevokeObjectURL&&e.forEach(e=>{a(e.path)})}async function d(a,f,d){const E=function(e){const o=e&&"object"==typeof e?e:{},s="image"===o.type||"file"===o.type||"any"===o.type?o.type:t.FILE_TYPE,a="number"==typeof o.count&&Number.isFinite(o.count)&&o.count>0?Math.floor(o.count):t.COUNT,c="number"==typeof o.timeoutMs&&Number.isFinite(o.timeoutMs)&&o.timeoutMs>0?o.timeoutMs:void 0,u=Array.isArray(o.files)?o.files.filter(e=>Boolean(e)&&"object"==typeof e&&"string"==typeof e.path):void 0;return{...o,url:"string"==typeof o.url?o.url:"",files:u,type:s,count:a,extensions:i(o.extensions),fieldName:"string"==typeof o.fieldName&&o.fieldName.trim()?o.fieldName:t.FIELD_NAME,formData:r(o.formData),headers:n(o.headers),timeoutMs:c,autoRevokeObjectURL:Boolean(o.autoRevokeObjectURL),signal:(f=o.signal,Boolean(f&&"object"==typeof f&&"function"==typeof f.addEventListener)?o.signal:void 0),beforeUpload:"function"==typeof o.beforeUpload?o.beforeUpload:void 0,onProgress:"function"==typeof o.onProgress?o.onProgress:void 0,toast:"function"==typeof o.toast?o.toast:void 0,progressToastFormatter:"function"==typeof o.progressToastFormatter?o.progressToastFormatter:void 0,successMessage:"string"==typeof o.successMessage?o.successMessage:void 0,failMessage:"string"==typeof o.failMessage?o.failMessage:void 0};var f}(a),{url:L,files:h,type:A=t.FILE_TYPE,count:T=t.COUNT,maxSizeMB:U,extensions:D,signal:C,successMessage:O}=E,P=L,{toast:_,shouldShowToast:w}=l(E);if(!P.trim()){const t=e.UPLOAD_URL_REQUIRED;return w&&_(t,!1,"none"),m(t)}if(g(C))return w&&_(e.UPLOAD_CANCELED,!1,"none"),p();const M=Array.isArray(h);let v=Array.isArray(h)?h:[];if(0===v.length&&!M)try{const e=await f({type:A,count:T,extensions:D});if(!e.success)return w&&e.message&&_(e.message,!1,"none"),[{file:null,success:!1,message:e.message}];v=e.files}catch(t){const o=t instanceof Error?t.message:e.SELECT_FAILED;return w&&_(o,!1,"none"),m(o)}if(0===v.length)return m(e.NO_FILES_SELECTED);const N=s(v,{maxSizeMB:U,extensions:D});if(!N.success){const t=N.message||e.SELECT_FAILED;return w&&_(t,!1,"none"),y(v,E),m(t)}if(g(C))return w&&_(e.UPLOAD_CANCELED,!1,"none"),y(v,E),p();const S=await async function(t,s,n,r){const{concurrency:i,signal:a,beforeUpload:f,onProgress:m,showProgressToast:p,progressToastFormatter:d}=s,{toast:E,shouldShowToast:L}=l(s),h=new Array(t.length);let A=0;const T=t.length,U=c(i,T),D=Boolean(p&&T>0),C="undefined"!=typeof uni?uni:null,O=Boolean(D&&C&&"function"==typeof C.showLoading&&"function"==typeof C.hideLoading),P=O,_=u((o,s)=>{if(!O)return;const n=function(t){const{files:o,index:s,pct:n,finishedCount:r,totalCount:i,progressToastFormatter:a}=t;if("function"==typeof a)try{return a(o[s],n,s+1,i)}catch{}return 1===i?`${e.UPLOADING} ${n}%`:`${e.UPLOADING} (${r}/${i})`}({files:t,index:o,pct:s,finishedCount:A,totalCount:T,progressToastFormatter:d});C.showLoading({title:n,mask:!0})},o.THROTTLE_MS);const w=async o=>{const i=t[o];let c=null;try{if(g(a))return c={file:i,success:!1,message:e.UPLOAD_CANCELED},y([i],s),void(O&&_(o,0));const t=await async function(t){if(!f)return null;try{return await f(t)?null:{file:t,success:!1,message:e.UPLOAD_CANCELED}}catch(o){const s=e.BEFORE_UPLOAD_FAILED(o instanceof Error?o.message:String(o??"unknown"));return L&&E(s,!1,"error"),{file:t,success:!1,message:s}}}(i);if(t)return c=t,void y([i],s);const u=function(e){return P?{...s,onProgress:(t,o)=>{_(e,o),m&&m(t,o)}}:s}(o);c=await r(i,u,n)}catch(e){c={file:i,success:!1,message:e instanceof Error?e.message:String(e??"Unknown error")}}finally{c||(c={file:i,success:!1,message:"Unknown error"}),h[o]=c,A++,c.success&&_(o,100)}};for(let e=0;e<T;e+=U){const t=Math.min(e+U,T),o=[];for(let s=e;s<t;s+=1)o.push(w(s));await Promise.all(o)}return O&&(_.cancel?.(),C.hideLoading()),h}(v,E,P,d);return S.filter(e=>e.success).length===S.length&&1===S.length&&w&&_(O||e.UPLOAD_SUCCESS,!1,"none"),S}export{d as processUpload};