UNPKG

my-uniapp-tools

Version:

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

2 lines (1 loc) 3.15 kB
"use strict";var e=require("./constants.cjs"),s=require("./utils.cjs");exports.pickFilesInH5=function(t){const{type:n="image",count:o=1,extensions:i}=t;return new Promise(t=>{if("undefined"==typeof document||"undefined"==typeof window)return void t({success:!1,files:[],message:e.MESSAGES.ENV_NOT_SUPPORT_SELECT});const a=document.createElement("input");a.type="file",a.multiple=o>1,a.style.display="none";const r=s.normalizeExtensions(i);"image"===n?a.accept="image/*":r&&(a.accept=r.map(e=>`.${e}`).join(","));let c=!1,l=null;const E=e=>{c||(c=!0,(()=>{l&&(clearTimeout(l),l=null),window.removeEventListener("focus",f);try{a.remove()}catch{}})(),t(e))},u=t=>{if(!t||0===t.length)return void E({success:!1,files:[],message:e.MESSAGES.USER_CANCELED});const n=Array.from(t).slice(0,o).map((e,t)=>({id:s.createFileId(t),name:e.name,size:e.size,path:URL.createObjectURL(e),mimeType:e.type,ext:s.getExtension(e.name),source:"file",platform:"h5",raw:e}));E({success:!0,files:n})};a.addEventListener("change",()=>u(a.files)),a.addEventListener("cancel",()=>E({success:!1,files:[],message:e.MESSAGES.USER_CANCELED}));const f=()=>{c||(l=window.setTimeout(()=>{c||(a.files&&0!==a.files.length?u(a.files):E({success:!1,files:[],message:e.MESSAGES.USER_CANCELED}))},300))};window.addEventListener("focus",f),document.body.appendChild(a),a.click()})},exports.uploadFileInH5=function(t,n,o){const{fieldName:i="file",formData:a,headers:r,timeoutMs:c,autoRevokeObjectURL:l}=n,{signal:E,onProgress:u}=n,f="string"==typeof i&&i.trim()?i:"file",m="number"==typeof c&&Number.isFinite(c)&&c>0?c:void 0,d=s.normalizeHeaders(r),S=s.normalizeFormData(a),p="function"==typeof u?u:void 0;return new Promise(n=>{const i=t.raw;if(!("undefined"!=typeof File&&i&&i instanceof File))return void n({file:t,success:!1,message:e.MESSAGES.H5_FILE_MISSING});const a=new XMLHttpRequest;let r=!1,c=null;const u=e=>{r||(r=!0,E&&c&&"function"==typeof E.removeEventListener&&(E.removeEventListener("abort",c),c=null),l&&s.isBlobUrl(t.path)&&s.revokeObjectURLSafely(t.path),n(e))};if(E&&"function"==typeof E.addEventListener){if(E.aborted)return void u({file:t,success:!1,message:e.MESSAGES.UPLOAD_CANCELED});c=()=>{a.abort(),u({file:t,success:!1,message:e.MESSAGES.UPLOAD_CANCELED})},E.addEventListener("abort",c,{once:!0})}a.open("POST",o),m&&m>0&&(a.timeout=m),d&&Object.keys(d).forEach(e=>{a.setRequestHeader(e,d[e])});const L=new FormData;L.append(f,i,t.name),S&&Object.keys(S).forEach(e=>{L.append(e,s.serializeFormDataValue(S[e]))}),a.upload.onprogress=e=>{if(e.lengthComputable&&p){const s=Math.round(e.loaded/e.total*100);p(t,s)}},a.onload=()=>{const n=a.status,o=n>=200&&n<300;let i=s.safeJsonParse(a.responseText);u({file:t,success:o,statusCode:n,data:i,message:o?void 0:e.MESSAGES.STATUS_CODE_ERROR(n)})},a.onerror=()=>u({file:t,success:!1,message:e.MESSAGES.UPLOAD_FAILED}),a.ontimeout=()=>u({file:t,success:!1,message:e.MESSAGES.UPLOAD_TIMEOUT(m||0)}),a.onabort=()=>u({file:t,success:!1,message:e.MESSAGES.UPLOAD_CANCELED});try{a.send(L)}catch(s){const n=s instanceof Error?s.message:"Unknown error";u({file:t,success:!1,message:e.MESSAGES.UPLOAD_REQUEST_FAILED(n)})}})};