acdr-ui/dist/build/mp-weixin/modules/mall/sheep/components/s-uploader/s-uploader.js
2024-10-01 09:15:35 +08:00

2 lines
7.6 KiB
JavaScript

"use strict";var e=Object.defineProperty,t=Object.defineProperties,i=Object.getOwnPropertyDescriptors,s=Object.getOwnPropertySymbols,l=Object.prototype.hasOwnProperty,r=Object.prototype.propertyIsEnumerable,o=(t,i,s)=>i in t?e(t,i,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[i]=s,a=(e,t)=>{for(var i in t||(t={}))l.call(t,i)&&o(e,i,t[i]);if(s)for(var i of s(t))r.call(t,i)&&o(e,i,t[i]);return e},h=(e,t,i)=>new Promise(((s,l)=>{var r=e=>{try{a(i.next(e))}catch(t){l(t)}},o=e=>{try{a(i.throw(e))}catch(t){l(t)}},a=e=>e.done?s(e.value):Promise.resolve(e.value).then(r,o);a((i=i.apply(e,t)).next())}));const n=require("../../../../../common/vendor.js"),u=require("./choose-and-upload-file.js"),d=require("./utils.js"),p=require("../../index.js"),c={name:"sUploader",components:{uploadImage:()=>"./upload-image.js",uploadFile:()=>"./upload-file.js"},options:{virtualHost:!0},emits:["select","success","fail","progress","delete","update:modelValue","update:url"],props:{modelValue:{type:[Array,Object],default:()=>[]},url:{type:[Array,String],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},sizeType:{type:Array,default:()=>["original","compressed"]},driver:{type:String,default:"local"},subtitle:{type:String,default:""}},data:()=>({files:[],localValue:[],imgsrc:p.sheep.$url.static("/static/img/shop/upload-camera.png")}),watch:{modelValue:{handler(e,t){this.setValue(e,t)},immediate:!0}},computed:{returnType(){return this.limit>1?"array":"object"},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(){"local"===this.driver&&(n.Ws.chooseAndUploadFile=u.chooseAndUploadFile),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)},setValue(e,t){return h(this,null,(function*(){const t=e=>h(this,null,(function*(){let t="";return t=e.fileID?e.fileID:e.url,/cloud:\/\/([\w.]+\/?)\S*/.test(t)&&(e.fileID=t,e.url=yield this.getTempFileURL(t)),e.url&&(e.path=e.url),e}));if("object"===this.returnType)e?yield t(e):e={};else{e||(e=[]);for(let i=0;i<e.length;i++){let s=e[i];yield t(s)}}this.localValue=e,this.form&&this.formItem&&!this.is_reset&&(this.is_reset=!1,this.formItem.setValue(this.localValue));let i=Object.keys(e).length>0?e:[];this.files=[].concat(i)}))},choose(){this.disabled||(this.files.length>=Number(this.limitLength)&&"grid"!==this.showType&&"array"===this.returnType?n.index.showToast({title:`您最多选择 ${this.limitLength} 个文件`,icon:"none"}):this.chooseFiles())},chooseFiles(){const e=d.get_extname(this.fileExtname);n.Ws.chooseAndUploadFile({type:this.fileMediatype,compressed:!1,sizeType:this.sizeType,extension:e.length>0?e: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=>{}))},chooseFileCallback(e){return h(this,null,(function*(){const s=d.get_extname(this.fileExtname);(1===Number(this.limitLength)&&this.disablePreview&&!this.disabled||"object"===this.returnType)&&(this.files=[]);let{filePaths:l,files:r}=d.get_files_and_is_max(e,s);s&&s.length>0||(l=e.tempFilePaths,r=e.tempFiles);let o=[];for(let e=0;e<r.length&&!(this.limitLength-this.files.length<=0);e++){r[e].uuid=Date.now();let s=yield d.get_file_data(r[e],this.fileMediatype);s.progress=0,s.status="ready",this.files.push(s),o.push((h=a({},s),n={file:r[e]},t(h,i(n))))}var h,n;this.$emit("select",{tempFiles:o,tempFilePaths:l}),e.tempFiles=r,this.autoUpload||(e.tempFiles=[])}))},uploadFiles(e){return e=[].concat(e),u.uploadCloudFiles.call(this,e,5,(e=>{this.setProgress(e,e.index,!0)})).then((e=>(this.setSuccessAndError(e),e))).catch((e=>{}))},setSuccessAndError(e,t){return h(this,null,(function*(){let t=[],i=[],s=[],l=[];for(let r=0;r<e.length;r++){const o=e[r],a=o.uuid?this.files.findIndex((e=>e.uuid===o.uuid)):o.index;if(-1===a||!this.files)break;if("request:fail"===o.errMsg)this.files[a].url=o.path,this.files[a].status="error",this.files[a].errMsg=o.errMsg,i.push(this.files[a]),l.push(this.files[a].url);else{this.files[a].errMsg="",this.files[a].fileID=o.url;/cloud:\/\/([\w.]+\/?)\S*/.test(o.url)?this.files[a].url=yield this.getTempFileURL(o.url):this.files[a].url=o.url,this.files[a].status="success",this.files[a].progress+=1,t.push(this.files[a]),s.push(this.files[a].fileID)}}t.length>0&&(this.setEmit(),this.$emit("success",{tempFiles:this.backObject(t),tempFilePaths:s})),i.length>0&&this.$emit("fail",{tempFiles:this.backObject(i),tempFilePaths:l})}))},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",{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=[],t=[];"object"===this.returnType?(e=this.backObject(this.files)[0],this.localValue=e||null,t=e?e.url:""):(e=this.backObject(this.files),this.localValue||(this.localValue=[]),this.localValue=[...e],this.localValue.length>0&&this.localValue.forEach((e=>{t.push(e.url)}))),this.$emit("update:modelValue",this.localValue),this.$emit("update:url",t)},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})})),t},getTempFileURL(e){return h(this,null,(function*(){e={fileList:[].concat(e)};return(yield n.Ws.getTempFileURL(e)).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}}};if(!Array){(n.resolveComponent("upload-image")+n.resolveComponent("upload-file"))()}const f=n._export_sfc(c,[["render",function(e,t,i,s,l,r){return n.e({a:i.title},i.title?{b:n.t(i.title),c:n.t(r.filesList.length),d:n.t(r.limitLength)}:{},{e:i.subtitle},i.subtitle?{f:n.t(i.subtitle)}:{},{g:"image"===i.fileMediatype&&"grid"===r.showType},"image"===i.fileMediatype&&"grid"===r.showType?{h:l.imgsrc,i:n.o(r.uploadFiles),j:n.o(r.choose),k:n.o(r.delFile),l:n.p({readonly:i.readonly,"image-styles":i.imageStyles,"files-list":i.url,limit:r.limitLength,disablePreview:i.disablePreview,delIcon:i.delIcon})}:{},{m:"image"!==i.fileMediatype||"grid"!==r.showType},"image"!==i.fileMediatype||"grid"!==r.showType?{n:n.o(r.uploadFiles),o:n.o(r.choose),p:n.o(r.delFile),q:n.p({readonly:i.readonly,"list-styles":i.listStyles,"files-list":r.filesList,showType:r.showType,delIcon:i.delIcon})}:{})}],["__scopeId","data-v-20e5088a"]]);wx.createComponent(f);