2 lines
17 KiB
JavaScript
2 lines
17 KiB
JavaScript
import{Z as e}from"./uni-cloud.es.t_RKYAwE.js";import{o as t,c as i,w as s,a as l,F as o,m as r,A as a,b as n,K as d,s as u,e as h,N as c,f as p,i as f,O as m,p as y,t as g,M as b,v as F,d as x}from"./index-CZsuXyof.js";import{_}from"./_plugin-vue_export-helper.BCo6x5W8.js";const k="chooseAndUploadFile:fail";function w(e,t){return e.tempFiles.forEach(((e,i)=>{e.name||(e.name=e.path.substring(e.path.lastIndexOf("/")+1)),t&&(e.fileType=t),e.cloudPath=Date.now()+"_"+i+e.name.substring(e.name.lastIndexOf("."))})),e.tempFilePaths||(e.tempFilePaths=e.tempFiles.map((e=>e.path))),e}function v(t,i=5,s){const l=(t=JSON.parse(JSON.stringify(t))).length;let o=0,r=this;return new Promise((a=>{for(;o<i;)n();function n(){let i=o++;if(i>=l)return void(!t.find((e=>!e.url&&!e.errMsg))&&a(t));const d=t[i],u=r.files.findIndex((e=>e.uuid===d.uuid));d.url="",delete d.errMsg,e.uploadFile({filePath:d.path,cloudPath:d.cloudPath,fileType:d.fileType,onUploadProgress:e=>{e.index=u,s&&s(e)}}).then((e=>{d.url=e.fileID,d.index=u,i<l&&n()})).catch((e=>{d.errMsg=e.errMsg||e.message,d.index=u,i<l&&n()}))}}))}function I(e,{onChooseFile:t,onUploadProgress:i}){return e.then((e=>{if(t){const i=t(e);if(void 0!==i)return Promise.resolve(i).then((t=>void 0===t?e:t))}return e})).then((e=>!1===e?{errMsg:"chooseAndUploadFile:ok",tempFilePaths:[],tempFiles:[]}:e))}function P(e={type:"all"}){return"image"===e.type?I(function(e){const{count:t,sizeType:i=["original","compressed"],sourceType:s,extension:l}=e;return new Promise(((e,o)=>{uni.chooseImage({count:t,sizeType:i,sourceType:s,extension:l,success(t){e(w(t,"image"))},fail(e){o({errMsg:e.errMsg.replace("chooseImage:fail",k)})}})}))}(e),e):"video"===e.type?I(function(e){const{count:t,camera:i,compressed:s,maxDuration:l,sourceType:o,extension:r}=e;return new Promise(((e,t)=>{uni.chooseVideo({camera:i,compressed:s,maxDuration:l,sourceType:o,extension:r,success(t){const{tempFilePath:i,duration:s,size:l,height:o,width:r}=t;e(w({errMsg:"chooseVideo:ok",tempFilePaths:[i],tempFiles:[{name:t.tempFile&&t.tempFile.name||"",path:i,size:l,type:t.tempFile&&t.tempFile.type||"",width:r,height:o,duration:s,fileType:"video",cloudPath:""}]},"video"))},fail(e){t({errMsg:e.errMsg.replace("chooseVideo:fail",k)})}})}))}(e),e):I(function(e){const{count:t,extension:i}=e;return new Promise(((e,s)=>{let l=uni.chooseFile;if("undefined"!=typeof wx&&"function"==typeof wx.chooseMessageFile&&(l=wx.chooseMessageFile),"function"!=typeof l)return s({errMsg:k+" 请指定 type 类型,该平台仅支持选择 image 或 video。"});l({type:"all",count:t,extension:i,success(t){e(w(t))},fail(e){s({errMsg:e.errMsg.replace("chooseFile:fail",k)})}})}))}(e),e)}const T=e=>{const t=e.lastIndexOf("."),i=e.length;return{name:e.substring(0,t),ext:e.substring(t+1,i)}},S=e=>{if(Array.isArray(e))return e;return e.replace(/(\[|\])/g,"").split(",")},$=async(e,t="image")=>{const i=T(e.name).ext.toLowerCase();let s={name:e.name,uuid:e.uuid,extname:i||"",cloudPath:e.cloudPath,fileType:e.fileType,thumbTempFilePath:e.thumbTempFilePath,url:e.path||e.path,size:e.size,image:{},path:e.path,video:{}};if("image"===t){const t=await(l=e.path,new Promise(((e,t)=>{uni.getImageInfo({src:l,success(t){e(t)},fail(e){t(e)}})})));delete s.video,s.image.width=t.width,s.image.height=t.height,s.image.location=t.path}else delete s.image;var l;return s};const M=_({name:"uniFilePicker",components:{uploadImage:_({name:"uploadImage",emits:["uploadFiles","choose","delFile"],props:{filesList:{type:Array,default:()=>[]},disabled:{type:Boolean,default:!1},disablePreview:{type:Boolean,default:!1},limit:{type:[Number,String],default:9},imageStyles:{type:Object,default:()=>({width:"auto",height:"auto",border:{}})},delIcon:{type:Boolean,default:!0},readonly:{type:Boolean,default:!1}},computed:{styles(){return Object.assign({width:"auto",height:"auto",border:{}},this.imageStyles)},boxStyle(){const{width:e="auto",height:t="auto"}=this.styles;let i={};"auto"===t?"auto"!==e?(i.height=this.value2px(e),i["padding-top"]=0):i.height=0:(i.height=this.value2px(t),i["padding-top"]=0),i.width="auto"===e?"auto"!==t?this.value2px(t):"33.3%":this.value2px(e);let s="";for(let l in i)s+=`${l}:${i[l]};`;return s},borderStyle(){let{border:e}=this.styles,t={};if("boolean"==typeof e)t.border=e?"1px #eee solid":"none";else{let i=e&&e.width||1;i=this.value2px(i);let s=e&&e.radius||3;s=this.value2px(s),t={"border-width":i,"border-style":e&&e.style||"solid","border-color":e&&e.color||"#eee","border-radius":s}}let i="";for(let s in t)i+=`${s}:${t[s]};`;return i}},methods:{uploadFiles(e,t){this.$emit("uploadFiles",e)},choose(){this.$emit("choose")},delFile(e){this.$emit("delFile",e)},prviewImage(e,t){let i=[];1===Number(this.limit)&&this.disablePreview&&!this.disabled&&this.$emit("choose"),this.disablePreview||(this.filesList.forEach((e=>{i.push(e.url)})),uni.previewImage({urls:i,current:t}))},value2px:e=>("number"==typeof e?e+="px":-1===e.indexOf("%")&&(e=-1!==e.indexOf("px")?e:e+"px"),e)}},[["render",function(e,y,g,b,F,x){const _=p,k=f,w=m;return t(),i(k,{class:"uni-file-picker__container"},{default:s((()=>[(t(!0),l(o,null,r(g.filesList,((e,l)=>(t(),i(k,{class:"file-picker__box",key:l,style:a(x.boxStyle)},{default:s((()=>[n(k,{class:"file-picker__box-content",style:a(x.borderStyle)},{default:s((()=>[n(_,{class:"file-image",src:e.url,mode:"aspectFill",onClick:d((t=>x.prviewImage(e,l)),["stop"])},null,8,["src","onClick"]),g.delIcon&&!g.readonly?(t(),i(k,{key:0,class:"icon-del-box",onClick:d((e=>x.delFile(l)),["stop"])},{default:s((()=>[n(k,{class:"icon-del"}),n(k,{class:"icon-del rotate"})])),_:2},1032,["onClick"])):u("",!0),e.progress&&100!==e.progress||0===e.progress?(t(),i(k,{key:1,class:"file-picker__progress"},{default:s((()=>[n(w,{class:"file-picker__progress-item",percent:-1===e.progress?0:e.progress,"stroke-width":"4",backgroundColor:e.errMsg?"#ff5a5f":"#EBEBEB"},null,8,["percent","backgroundColor"])])),_:2},1024)):u("",!0),e.errMsg?(t(),i(k,{key:2,class:"file-picker__mask",onClick:d((t=>x.uploadFiles(e,l)),["stop"])},{default:s((()=>[h(" 点击重试 ")])),_:2},1032,["onClick"])):u("",!0)])),_:2},1032,["style"])])),_:2},1032,["style"])))),128)),g.filesList.length<g.limit&&!g.readonly?(t(),i(k,{key:0,class:"file-picker__box",style:a(x.boxStyle)},{default:s((()=>[n(k,{class:"file-picker__box-content is-add",style:a(x.borderStyle),onClick:x.choose},{default:s((()=>[c(e.$slots,"default",{},(()=>[n(k,{class:"icon-add"}),n(k,{class:"icon-add rotate"})]),!0)])),_:3},8,["style","onClick"])])),_:3},8,["style"])):u("",!0)])),_:3})}],["__scopeId","data-v-86b162f5"]]),uploadFile:_({name:"uploadFile",emits:["uploadFiles","choose","delFile"],props:{filesList:{type:Array,default:()=>[]},delIcon:{type:Boolean,default:!0},limit:{type:[Number,String],default:9},showType:{type:String,default:""},listStyles:{type:Object,default:()=>({border:!0,dividline:!0,borderStyle:{}})},readonly:{type:Boolean,default:!1}},computed:{list(){let e=[];return this.filesList.forEach((t=>{e.push(t)})),e},styles(){return Object.assign({border:!0,dividline:!0,"border-style":{}},this.listStyles)},borderStyle(){let{borderStyle:e,border:t}=this.styles,i={};if(t){let t=e&&e.width||1;t=this.value2px(t);let s=e&&e.radius||5;s=this.value2px(s),i={"border-width":t,"border-style":e&&e.style||"solid","border-color":e&&e.color||"#eee","border-radius":s}}else i.border="none";let s="";for(let l in i)s+=`${l}:${i[l]};`;return s},borderLineStyle(){let e={},{borderStyle:t}=this.styles;if(t&&t.color&&(e["border-color"]=t.color),t&&t.width){let i=t&&t.width||1,s=t&&t.style||0;"number"==typeof i?i+="px":i=i.indexOf("px")?i:i+"px",e["border-width"]=i,"number"==typeof s?s+="px":s=s.indexOf("px")?s:s+"px",e["border-top-style"]=s}let i="";for(let s in e)i+=`${s}:${e[s]};`;return i}},methods:{uploadFiles(e,t){this.$emit("uploadFiles",{item:e,index:t})},choose(){this.$emit("choose")},delFile(e){this.$emit("delFile",e)},value2px:e=>("number"==typeof e?e+="px":e=-1!==e.indexOf("px")?e:e+"px",e)}},[["render",function(e,p,b,F,x,_){const k=f,w=m;return t(),i(k,{class:"uni-file-picker__files"},{default:s((()=>[b.readonly?u("",!0):(t(),i(k,{key:0,class:"files-button",onClick:_.choose},{default:s((()=>[c(e.$slots,"default",{},void 0,!0)])),_:3},8,["onClick"])),_.list.length>0?(t(),i(k,{key:1,class:"uni-file-picker__lists is-text-box",style:a(_.borderStyle)},{default:s((()=>[(t(!0),l(o,null,r(_.list,((e,l)=>(t(),i(k,{class:y(["uni-file-picker__lists-box",{"files-border":0!==l&&_.styles.dividline}]),key:l,style:a(0!==l&&_.styles.dividline&&_.borderLineStyle)},{default:s((()=>[n(k,{class:"uni-file-picker__item"},{default:s((()=>[n(k,{class:"files__name"},{default:s((()=>[h(g(e.name),1)])),_:2},1024),b.delIcon&&!b.readonly?(t(),i(k,{key:0,class:"icon-del-box icon-files",onClick:e=>_.delFile(l)},{default:s((()=>[n(k,{class:"icon-del icon-files"}),n(k,{class:"icon-del rotate"})])),_:2},1032,["onClick"])):u("",!0)])),_:2},1024),e.progress&&100!==e.progress||0===e.progress?(t(),i(k,{key:0,class:"file-picker__progress"},{default:s((()=>[n(w,{class:"file-picker__progress-item",percent:-1===e.progress?0:e.progress,"stroke-width":"4",backgroundColor:e.errMsg?"#ff5a5f":"#EBEBEB"},null,8,["percent","backgroundColor"])])),_:2},1024)):u("",!0),"error"===e.status?(t(),i(k,{key:1,class:"file-picker__mask",onClick:d((t=>_.uploadFiles(e,l)),["stop"])},{default:s((()=>[h(" 点击重试 ")])),_:2},1032,["onClick"])):u("",!0)])),_:2},1032,["class","style"])))),128))])),_:1},8,["style"])):u("",!0)])),_:3})}],["__scopeId","data-v-5d376bd5"]])},options:{virtualHost:!0},emits:["select","success","fail","progress","delete","update:modelValue","input"],props:{modelValue:{type:[Array,Object],default:()=>[]},value:{type:[Array,Object],default:()=>[]},disabled:{type:Boolean,default:!1},disablePreview:{type:Boolean,default:!1},delIcon:{type:Boolean,default:!0},autoUpload:{type:Boolean,default:!0},limit:{type:[Number,String],default:9},mode:{type:String,default:"grid"},fileMediatype:{type:String,default:"image"},fileExtname:{type:[Array,String],default:()=>[]},title:{type:String,default:""},listStyles:{type:Object,default:()=>({border:!0,dividline:!0,borderStyle:{}})},imageStyles:{type:Object,default:()=>({width:"auto",height:"auto"})},readonly:{type:Boolean,default:!1},returnType:{type:String,default:"array"},sizeType:{type:Array,default:()=>["original","compressed"]},sourceType:{type:Array,default:()=>["album","camera"]},provider:{type:String,default:""}},data:()=>({files:[],localValue:[]}),watch:{value:{handler(e,t){this.setValue(e,t)},immediate:!0},modelValue:{handler(e,t){this.setValue(e,t)},immediate:!0}},computed:{filesList(){let e=[];return this.files.forEach((t=>{e.push(t)})),e},showType(){return"image"===this.fileMediatype?this.mode:"list"},limitLength(){return"object"===this.returnType?1:this.limit?this.limit>=9?9:this.limit:1}},created(){e.config&&e.config.provider||(this.noSpace=!0,e.chooseAndUploadFile=P),this.form=this.getForm("uniForms"),this.formItem=this.getForm("uniFormsItem"),this.form&&this.formItem&&this.formItem.name&&(this.rename=this.formItem.name,this.form.inputChildrens.push(this))},methods:{clearFiles(e){0===e||e?this.files.splice(e,1):(this.files=[],this.$nextTick((()=>{this.setEmit()}))),this.$nextTick((()=>{this.setEmit()}))},upload(){let e=[];return this.files.forEach(((t,i)=>{"ready"!==t.status&&"error"!==t.status||e.push(Object.assign({},t))})),this.uploadFiles(e)},async setValue(e,t){const i=async e=>{let t="";return t=e.fileID?e.fileID:e.url,/cloud:\/\/([\w.]+\/?)\S*/.test(t)&&(e.fileID=t,e.url=await this.getTempFileURL(t)),e.url&&(e.path=e.url),e};if("object"===this.returnType)e?await i(e):e={};else{e||(e=[]);for(let t=0;t<e.length;t++){let s=e[t];await i(s)}}this.localValue=e,this.form&&this.formItem&&!this.is_reset&&(this.is_reset=!1,this.formItem.setValue(this.localValue));let s=Object.keys(e).length>0?e:[];this.files=[].concat(s)},choose(){this.disabled||(this.files.length>=Number(this.limitLength)&&"grid"!==this.showType&&"array"===this.returnType?uni.showToast({title:`您最多选择 ${this.limitLength} 个文件`,icon:"none"}):this.chooseFiles())},chooseFiles(){const t=S(this.fileExtname);e.chooseAndUploadFile({type:this.fileMediatype,compressed:!1,sizeType:this.sizeType,sourceType:this.sourceType,extension:t.length>0?t:void 0,count:this.limitLength-this.files.length,onChooseFile:this.chooseFileCallback,onUploadProgress:e=>{this.setProgress(e,e.index)}}).then((e=>{this.setSuccessAndError(e.tempFiles)})).catch((e=>{console.log("选择失败",e)}))},async chooseFileCallback(e){const t=S(this.fileExtname);(1===Number(this.limitLength)&&this.disablePreview&&!this.disabled||"object"===this.returnType)&&(this.files=[]);let{filePaths:i,files:s}=((e,t)=>{let i=[],s=[];return t&&0!==t.length?(e.tempFiles.forEach((e=>{const l=T(e.name).ext.toLowerCase();-1!==t.indexOf(l)&&(s.push(e),i.push(e.path))})),s.length!==e.tempFiles.length&&uni.showToast({title:`当前选择了${e.tempFiles.length}个文件 ,${e.tempFiles.length-s.length} 个文件格式不正确`,icon:"none",duration:5e3}),{filePaths:i,files:s}):{filePaths:i,files:s}})(e,t);t&&t.length>0||(i=e.tempFilePaths,s=e.tempFiles);let l=[];for(let o=0;o<s.length&&!(this.limitLength-this.files.length<=0);o++){s[o].uuid=Date.now();let e=await $(s[o],this.fileMediatype);e.progress=0,e.status="ready",this.files.push(e),l.push({...e,file:s[o]})}this.$emit("select",{tempFiles:l,tempFilePaths:i}),e.tempFiles=s,this.autoUpload&&!this.noSpace||(e.tempFiles=[]),e.tempFiles.forEach(((e,t)=>{this.provider&&(e.provider=this.provider);const i=e.name.split("."),s=i.pop(),l=i.join(".").replace(/[\s\/\?<>\\:\*\|":]/g,"_");e.cloudPath=l+"_"+Date.now()+"_"+t+"."+s}))},uploadFiles(e){return e=[].concat(e),v.call(this,e,5,(e=>{this.setProgress(e,e.index,!0)})).then((e=>(this.setSuccessAndError(e),e))).catch((e=>{console.log(e)}))},async setSuccessAndError(e,t){let i=[],s=[],l=[],o=[];for(let r=0;r<e.length;r++){const t=e[r],a=t.uuid?this.files.findIndex((e=>e.uuid===t.uuid)):t.index;if(-1===a||!this.files)break;if("request:fail"===t.errMsg)this.files[a].url=t.path,this.files[a].status="error",this.files[a].errMsg=t.errMsg,s.push(this.files[a]),o.push(this.files[a].url);else{this.files[a].errMsg="",this.files[a].fileID=t.url;/cloud:\/\/([\w.]+\/?)\S*/.test(t.url)?this.files[a].url=await this.getTempFileURL(t.url):this.files[a].url=t.url,this.files[a].status="success",this.files[a].progress+=1,i.push(this.files[a]),l.push(this.files[a].fileID)}}i.length>0&&(this.setEmit(),this.$emit("success",{tempFiles:this.backObject(i),tempFilePaths:l})),s.length>0&&this.$emit("fail",{tempFiles:this.backObject(s),tempFilePaths:o})},setProgress(e,t,i){this.files.length;const s=Math.round(100*e.loaded/e.total);let l=t;i||(l=this.files.findIndex((t=>t.uuid===e.tempFile.uuid))),-1!==l&&this.files[l]&&(this.files[l].progress=s-1,this.$emit("progress",{index:l,progress:parseInt(s),tempFile:this.files[l]}))},delFile(e){this.$emit("delete",{index:e,tempFile:this.files[e],tempFilePath:this.files[e].url}),this.files.splice(e,1),this.$nextTick((()=>{this.setEmit()}))},getFileExt(e){const t=e.lastIndexOf("."),i=e.length;return{name:e.substring(0,t),ext:e.substring(t+1,i)}},setEmit(){let e=[];"object"===this.returnType?(e=this.backObject(this.files)[0],this.localValue=e||null):(e=this.backObject(this.files),this.localValue||(this.localValue=[]),this.localValue=[...e]),this.$emit("update:modelValue",this.localValue)},backObject(e){let t=[];return e.forEach((e=>{t.push({extname:e.extname,fileType:e.fileType,image:e.image,name:e.name,path:e.path,size:e.size,fileID:e.fileID,url:e.url,uuid:e.uuid,status:e.status,cloudPath:e.cloudPath})})),t},async getTempFileURL(t){t={fileList:[].concat(t)};return(await e.getTempFileURL(t)).fileList[0].tempFileURL||""},getForm(e="uniForms"){let t=this.$parent,i=t.$options.name;for(;i!==e;){if(t=t.$parent,!t)return!1;i=t.$options.name}return t}}},[["render",function(e,l,o,r,a,d){const p=F,m=f,y=b("upload-image"),_=x,k=b("upload-file");return t(),i(m,{class:"uni-file-picker"},{default:s((()=>[o.title?(t(),i(m,{key:0,class:"uni-file-picker__header"},{default:s((()=>[n(p,{class:"file-title"},{default:s((()=>[h(g(o.title),1)])),_:1}),n(p,{class:"file-count"},{default:s((()=>[h(g(d.filesList.length)+"/"+g(d.limitLength),1)])),_:1})])),_:1})):u("",!0),"image"===o.fileMediatype&&"grid"===d.showType?(t(),i(y,{key:1,readonly:o.readonly,"image-styles":o.imageStyles,"files-list":d.filesList,limit:d.limitLength,disablePreview:o.disablePreview,delIcon:o.delIcon,onUploadFiles:d.uploadFiles,onChoose:d.choose,onDelFile:d.delFile},{default:s((()=>[c(e.$slots,"default",{},(()=>[n(m,{class:"is-add"},{default:s((()=>[n(m,{class:"icon-add"}),n(m,{class:"icon-add rotate"})])),_:1})]),!0)])),_:3},8,["readonly","image-styles","files-list","limit","disablePreview","delIcon","onUploadFiles","onChoose","onDelFile"])):u("",!0),"image"!==o.fileMediatype||"grid"!==d.showType?(t(),i(k,{key:2,readonly:o.readonly,"list-styles":o.listStyles,"files-list":d.filesList,showType:d.showType,delIcon:o.delIcon,onUploadFiles:d.uploadFiles,onChoose:d.choose,onDelFile:d.delFile},{default:s((()=>[c(e.$slots,"default",{},(()=>[n(_,{type:"primary",size:"mini"},{default:s((()=>[h("选择文件")])),_:1})]),!0)])),_:3},8,["readonly","list-styles","files-list","showType","delIcon","onUploadFiles","onChoose","onDelFile"])):u("",!0)])),_:3})}],["__scopeId","data-v-5e9fcb2e"]]);export{M as _};
|