wd-upload.js 7.2 KB

1
  1. "use strict";const e=require("../../../../common/vendor.js");Math||(o+t+i)();const o=()=>"../wd-icon/wd-icon.js",i=()=>"../wd-video-preview/wd-video-preview.js",t=()=>"../wd-loading/wd-loading.js",s=e.defineComponent({name:"wd-upload",options:{addGlobalClass:!0,virtualHost:!0,styleIsolation:"shared"},props:e.uploadProps,emits:["fail","change","success","progress","oversize","chooseerror","remove","update:fileList"],setup(o,{expose:i,emit:t}){const s=o,n=t;i({submit:()=>v(),abort:()=>d()});const{translate:a}=e.useTranslate("upload"),l=e.ref([]),r=e.computed((()=>!s.limit||l.value.length<s.limit)),u=e.ref(),{startUpload:c,abort:d,chooseFile:f,UPLOAD_STATUS:m}=e.useUpload();function p(){n("update:fileList",l.value)}function v(){const{buildFormData:o,formData:i={},statusKey:t}=s,{action:n,name:a,header:r={},accept:u,successStatus:d,uploadMethod:f}=s,p=e.isDef(d)?d:200;for(const e of l.value)e[t]===m.PENDING&&(o?o({file:e,formData:i,resolve:o=>{o&&c(e,{action:n,header:r,name:a,formData:o,fileType:u,statusCode:p,statusKey:t,uploadMethod:f,onSuccess:g,onError:w,onProgress:y})}}):c(e,{action:n,header:r,name:a,formData:i,fileType:u,statusCode:p,statusKey:t,uploadMethod:f,onSuccess:g,onError:w,onProgress:y}))}function h(o){return new Promise(((i,t)=>{e.index.getImageInfo({src:o,success:e=>{i(e)},fail:e=>{t(e)}})}))}function b(o,i){const{statusKey:t}=s,n={uid:e.context.id++,name:o.name||"",thumb:o.thumb||"",[t]:"pending",size:o.size||0,url:o.path,percent:0};"number"==typeof i?l.value.splice(i,1,n):l.value.push(n),s.autoUpload&&v()}function w(e,o,i){const{statusKey:t}=s,a=l.value.findIndex((e=>e.uid===o.uid));a>-1&&(l.value[a][t]="fail",l.value[a].error=e.message,l.value[a].response=e,n("fail",{error:e,file:o,formData:i}),p())}function g(e,o,i){const{statusKey:t}=s,a=l.value.findIndex((e=>e.uid===o.uid));a>-1&&(l.value[a][t]="success",l.value[a].response=e.data,n("change",{fileList:l.value}),n("success",{file:o,fileList:l.value,formData:i}),p())}function y(e,o){const i=l.value.findIndex((e=>e.uid===o.uid));i>-1&&(l.value[i].percent=e.progress,n("progress",{response:e,file:o}))}function x(e){const{multiple:o,maxSize:i,accept:t,sizeType:a,limit:r,sourceType:u,compressed:c,maxDuration:d,camera:m,beforeUpload:p,extension:v}=s;f({multiple:o,sizeType:a,sourceType:u,maxCount:r?r-l.value.length:9,accept:t,compressed:c,maxDuration:d,camera:m,extension:v}).then((t=>{let s=t;o||(s=s.slice(0,1));const a=async o=>{for(let t=0;t<o.length;t++){const s=o[t];if("image"===s.type&&!s.size){const e=await h(s.path);s.size=e.width*e.height}Number(s.size)<=i?b(s,e):n("oversize",{file:s})}};p?p({files:s,fileList:l.value,resolve:e=>{e&&a(s)}}):a(s)})).catch((e=>{n("chooseerror",{error:e})}))}function L(e){if(s.disabled)return;const{beforeChoose:o}=s;o?o({fileList:l.value,resolve:o=>{o&&x(e)}}):x(e)}function F(e){l.value.splice(l.value.findIndex((o=>o.uid===e.uid)),1),n("change",{fileList:l.value}),n("remove",{file:e}),p()}function C(o){e.index.openDocument({filePath:o.url,showMenu:!0})}function I(o,i){const{onPreviewFail:t}=s;e.index.previewImage({urls:i,current:i[o],fail(){t?t({index:o,imgList:i}):e.index.showToast({title:"预览图片失败",icon:"none"})}})}function P(o,i){const{onPreviewFail:t}=s;e.index.previewMedia({current:o,sources:i.map((e=>({url:e.url,type:"video",poster:e.thumb}))),fail(){t?t({index:o,imgList:[]}):e.index.showToast({title:"预览视频失败",icon:"none"})}})}function T(o){const{beforePreview:i,reupload:t}=s,n=e.deepClone(l.value),a=n.findIndex((e=>e.url===o.url)),r=n.filter((e=>D(e))),u=r.findIndex((e=>e.url===o.url));t?L(a):i?i({file:o,index:a,imgList:[],fileList:n,resolve:e=>{e&&P(u,r)}}):P(u,r)}function D(o){return o.name&&e.isVideoUrl(o.name)||e.isVideoUrl(o.url)}function K(o){return o.name&&e.isImageUrl(o.name)||e.isImageUrl(o.url)}return e.watch((()=>s.fileList),(o=>{const{statusKey:i}=s;if(e.isEqual(o,l.value))return;const t=o.map((o=>(o[i]=o[i]||"success",o.response=o.response||"",{...o,uid:e.context.id++})));l.value=t}),{deep:!0,immediate:!0}),e.watch((()=>s.limit),(e=>{e&&e<l.value.length&&console.error("[wot-design]Error: props limit must less than fileList.length")}),{deep:!0,immediate:!0}),e.watch((()=>s.beforePreview),(o=>{o&&!e.isFunction(o)&&console.error("The type of beforePreview must be Function")}),{deep:!0,immediate:!0}),e.watch((()=>s.onPreviewFail),(o=>{o&&!e.isFunction(o)&&console.error("The type of onPreviewFail must be Function")}),{deep:!0,immediate:!0}),e.watch((()=>s.beforeRemove),(o=>{o&&!e.isFunction(o)&&console.error("The type of beforeRemove must be Function")}),{deep:!0,immediate:!0}),e.watch((()=>s.beforeUpload),(o=>{o&&!e.isFunction(o)&&console.error("The type of beforeUpload must be Function")}),{deep:!0,immediate:!0}),e.watch((()=>s.beforeChoose),(o=>{o&&!e.isFunction(o)&&console.error("The type of beforeChoose must be Function")}),{deep:!0,immediate:!0}),e.watch((()=>s.buildFormData),(o=>{o&&!e.isFunction(o)&&console.error("The type of buildFormData must be Function")}),{deep:!0,immediate:!0}),(o,i)=>e.e({a:e.f(l.value,((i,t,n)=>e.e({a:K(i)},K(i)?{b:i.url,c:o.imageMode,d:e.o((o=>function(o){const{beforePreview:i,reupload:t}=s,n=e.deepClone(l.value),a=n.findIndex((e=>e.url===o.url)),r=n.filter((e=>K(e))).map((e=>e.url)),u=r.findIndex((e=>e===o.url));t?L(a):i?i({file:o,index:a,fileList:n,imgList:r,resolve:e=>{e&&I(u,r)}}):I(u,r)}(i)),t)}:D(i)?e.e({f:i.thumb},i.thumb?{g:i.thumb,h:o.imageMode,i:"95b8bfe2-0-"+n,j:e.p({name:"play-circle-filled","custom-class":"wd-upload__video-paly"}),k:e.o((e=>T(i)),t)}:{l:i.url,m:i.name||"视频"+t,n:i.thumb,o:"95b8bfe2-1-"+n,p:e.p({name:"play-circle-filled","custom-class":"wd-upload__video-paly"}),q:e.o((e=>T(i)),t)}):{r:"95b8bfe2-2-"+n,s:e.p({name:"file","custom-class":"wd-upload__file-icon"}),t:e.t(i.name||i.url),v:e.o((o=>function(o){const{beforePreview:i,reupload:t}=s,n=e.deepClone(l.value),a=n.findIndex((e=>e.url===o.url));t?L(a):i?i({file:o,index:a,imgList:[],fileList:n,resolve:e=>{e&&C(o)}}):C(o)}(i)),t)},{e:D(i),w:"success"!==i[s.statusKey]},"success"!==i[s.statusKey]?e.e({x:"loading"===i[s.statusKey]},"loading"===i[s.statusKey]?{y:"95b8bfe2-3-"+n,z:e.p({type:o.loadingType,size:o.loadingSize,color:o.loadingColor}),A:e.t(i.percent)}:{},{B:"fail"===i[s.statusKey]},"fail"===i[s.statusKey]?{C:"95b8bfe2-4-"+n,D:e.p({name:"close-outline","custom-class":"wd-upload__icon"}),E:e.t(i.error||e.unref(a)("error"))}:{}):{},{F:"loading"!==i[s.statusKey]&&!o.disabled},"loading"===i[s.statusKey]||o.disabled?{}:{G:e.o((e=>function(e){const{beforeRemove:o}=s,i=e,t=l.value[i];o?o({file:t,index:i,fileList:l.value,resolve:e=>{e&&F(t)}}):F(t)}(t)),t),H:"95b8bfe2-5-"+n,I:e.p({name:"error-fill","custom-class":"wd-upload__close"})},o.$slots["preview-cover"]?{J:"preview-cover-"+n,K:e.r("preview-cover",{file:i,index:t},n)}:{},{L:t}))),b:o.$slots["preview-cover"],c:e.n(o.customPreviewClass),d:r.value},r.value?e.e({e:o.$slots.default},o.$slots.default?{f:e.n(o.customEvokeClass),g:e.o(L)}:e.e({h:e.p({name:"fill-camera"}),i:o.limit&&o.showLimitNum},o.limit&&o.showLimitNum?{j:e.t(l.value.length),k:e.t(o.limit)}:{},{l:e.o(L),m:e.n(o.disabled?"is-disabled":""),n:e.n(o.customEvokeClass)})):{},{o:e.n(o.customClass),p:e.s(o.customStyle),q:e.sr(u,"95b8bfe2-7",{k:"videoPreview"})})}}),n=e._export_sfc(s,[["__scopeId","data-v-95b8bfe2"]]);wx.createComponent(n);