| 1 |
- import{c as t,a9 as e,a2 as s,a5 as i,az as o,t as l,p as a,w as n,v as h,Q as r,ag as p,x as d,y as u,q as c}from"./index-P0fyQ2Em.js";import{_ as g}from"./_plugin-vue_export-helper.BCo6x5W8.js";class f{constructor(t){this.dom=null,this.files=new Map,this.debug=t.debug||!1,this.id=t.id,this.width=t.width,this.height=t.height,this.option=t.option,this.instantly=t.instantly,this.prohibited=t.prohibited,this.onchange=t.onchange,this.onprogress=t.onprogress,this.uploadHandle=this._uploadHandle}create(t){if(!this.dom){let t=document.createElement("input");return t.type="file",t.value="",t.style.height=this.height,t.style.width=this.width,t.style.position="absolute",t.style.top=0,t.style.left=0,t.style.right=0,t.style.bottom=0,t.style.opacity=0,t.style.zIndex=999,t.accept=this.prohibited.accept,this.prohibited.multiple&&(t.multiple="multiple"),t.onchange=t=>{for(let e of t.target.files){if(this.files.size>=this.prohibited.count){this.toast(`只允许上传${this.prohibited.count}个文件`),this.dom.value="";break}this.addFile(e)}this._uploadAfter(),this.dom.value=""},this.dom=t,this.dom}}setData(){let[t,e=""]=arguments;"object"==typeof t?Object.assign(this.option,t):this._setValue(this.option,t,e),this.debug&&console.log(JSON.stringify(this.option))}async upload(t=""){if(!this.option.url)throw Error("未设置上传地址");if(t&&this.files.has(t))await this.uploadHandle(this.files.get(t));else for(let e of this.files.values())"waiting"!==e.type&&"fail"!==e.type||await this.uploadHandle(e)}addFile(t,e){let s=t.name;this.debug&&console.log("文件名称",s,"大小",t.size);const i={KB:1024,MB:1048576,GB:1073741824};if(t){let e="",o=s.substring(s.lastIndexOf(".")+1).toLowerCase(),l=this.prohibited.formats.toLowerCase();return e=URL.createObjectURL(t),l&&!l.includes(o)?(this.toast(`不支持上传${o.toUpperCase()}格式文件`),!1):t.size>i[this.prohibited.sizeUnit]*Math.abs(this.prohibited.size)?(this.toast(`文件大小超过${this.prohibited.size}${this.prohibited.sizeUnit}`),!1):(this.files.set(t.name,{file:t,path:e,name:t.name,fullName:t.name,size:t.size,progress:0,type:"waiting"}),!0)}}clear(t=""){return t?this.files.delete(t):this.files.clear(),this.onchange(this.files)}toast(e){t({title:e,icon:"none"})}chooseMessageFile(t,e){wx.chooseMessageFile({count:e,type:t,success:({tempFiles:t})=>{for(let e of t)this.addFile(e);this._uploadAfter()},fail:()=>{this.toast("打开失败")}})}_copyObject(t){return void 0!==t?JSON.parse(JSON.stringify(t)):t}_setValue(t,e,s){let i;i="object"==typeof s?this._copyObject(s):s;let o=new RegExp("([\\w$]+)|\\[(:\\d)\\]","g");const l=e.match(o);for(let a=0;a<l.length-1;a++){let e=l[a];"object"!=typeof t[e]&&(t[e]={}),t=t[e]}t[l[l.length-1]]=i,this.debug&&console.log("参数更新后",JSON.stringify(this.option))}_uploadAfter(){this.onchange(this.files),setTimeout((()=>{this.instantly&&this.upload()}),1e3)}_overrideUrlLoading(){this.dom.overrideUrlLoading({mode:"reject"},(t=>{let{retype:e,item:s,files:i,end:o}=this._getRequest(t.url),l=this;switch(e){case"updateOption":this.dom.evalJS(`vm.setData('${JSON.stringify(l.option)}')`);break;case"change":try{l.files=new Map([...l.files,...JSON.parse(unescape(i))])}catch(a){return console.error("出错了,请检查代码")}l.onchange(l.files);break;case"progress":try{s=JSON.parse(unescape(s))}catch(a){return console.error("出错了,请检查代码")}l._changeFilesItem(s,o)}}))}_getRequest(t){let e=new Object,s=t.indexOf("?");if(-1!=s){let i=t.substring(s+1).split("&");for(let t=0;t<i.length;t++)e[i[t].split("=")[0]]=unescape(i[t].split("=")[1])}return e}_changeFilesItem(t,e=!1){this.debug&&console.log("onprogress",JSON.stringify(t)),this.onprogress(t,e),this.files.set(t.name,t)}_uploadHandle(t){return t.type="loading",delete t.responseText,new Promise(((e,s)=>{this.debug&&console.log("option",JSON.stringify(this.option));let{url:i,name:o,method:l="POST",header:a,formData:n,data:h,parentId:r}=this.option,p=new FormData;for(let t in n)p.append(t,n[t]);for(let t in h)p.append(t,h[t]);p.append(o,t.file);let d=new XMLHttpRequest;d.open(l,`${i}?parentId=${r}`,!0);for(let t in a)d.setRequestHeader(t,a[t]);d.upload.addEventListener("progress",(e=>{if(e.lengthComputable){let s=Math.ceil(100*e.loaded/e.total);s<=100&&(t.progress=s,this._changeFilesItem(t))}}),!1),d.ontimeout=()=>(console.error("请求超时"),t.type="fail",this._changeFilesItem(t,!0),e(!1)),d.onreadystatechange=s=>{if(4==d.readyState)return 200==d.status?(this.debug&&console.log("上传完成:"+d.responseText),t.responseText=d.responseText,t.type="success",this._changeFilesItem(t,!0),e(!0)):(0==d.status&&console.error("status = 0 :请检查请求头Content-Type与服务端是否匹配,服务端已正确开启跨域,并且nginx未拦截阻止请求"),console.error("--ERROR--:status = "+d.status),t.type="fail",this._changeFilesItem(t,!0),e(!1))},d.send(p)}))}_uploadHandleWX(t){return t.type="loading",delete t.responseText,new Promise(((s,i)=>{this.debug&&console.log("option",JSON.stringify(this.option));let o={filePath:t.file.path,...this.option,url:this.option.url+"?parentId="+this.option.parentId+"&fileName="+t.file.name,formData:this.option.data||{}};o.fail=({errMsg:e=""})=>(console.error("--ERROR--:"+e),t.type="fail",this._changeFilesItem(t,!0),s(!1)),o.success=e=>200==e.statusCode?(this.debug&&console.log("上传完成,微信端返回不一定是字符串,根据接口返回格式判断是否需要JSON.parse:"+e.data),t.responseText=e.data,t.type="success",this._changeFilesItem(t,!0),s(!0)):(t.type="fail",this._changeFilesItem(t,!0),s(!1)),e(o).onProgressUpdate((({progress:e=0})=>{e<=100&&(t.progress=e,this._changeFilesItem(t))}))}))}}const y=g({name:"Lsj-upload",props:{debug:{type:Boolean,default:!1},instantly:{type:Boolean,default:!1},option:{type:Object,default:()=>{}},sizeUnit:{type:String,default:"MB"},size:{type:Number,default:10},count:{type:Number,default:9},multiple:{type:Boolean,default:!0},formats:{type:String,default:""},accept:{type:String,default:""},wxFileType:{type:String,default:"all"},childId:{type:String,default:"lsjUpload"},width:{type:String,default:"100%"},height:{type:String,default:"80rpx"},top:{type:[String,Number],default:""},left:{type:[String,Number],default:""},bottom:{type:[String,Number],default:""},right:{type:[String,Number],default:""},position:{type:String,default:"static"},buttonText:{type:String,default:"点击上传"}},data:()=>({}),watch:{option(t){}},updated(){},computed:{getStyles(){let t={width:this.width,height:this.height};return"absolute"==this.position&&(t.top=this.top,t.bottom=this.bottom,t.left=this.left,t.right=this.right,t.position="fixed"),t}},mounted(){this._size=0;let t=this.childId+(new Date).getTime();this.lsjFile=new f({id:t,debug:this.debug,width:this.width,height:this.height,option:this.option,instantly:this.instantly,prohibited:{size:this.size,sizeUnit:this.sizeUnit,formats:this.formats,accept:this.accept,count:this.count,multiple:this.multiple},onchange:this.onchange,onprogress:this.onprogress}),this.create(),s("lsjShow",this.show)},beforeDestroy(){i("lsjShow",this.show)},methods:{setFiles(t){if(t instanceof Map)for(let[e,s]of t)s.progress=100,s.type="success",this.lsjFile.files.set(e,s);else Array.isArray(t)&&t.forEach((t=>{t.name&&(t.progress=100,t.type="success",this.lsjFile.files.set(t.name,t))}));this.onchange(this.lsjFile.files)},setData(){this.lsjFile&&this.lsjFile.setData(...arguments)},getDomStyles(t){o().in(this).select(".lsj-file").fields({size:!0,rect:!0},(({height:e,width:s,top:i,left:l,right:a,bottom:n})=>{o().selectViewport().scrollOffset((({scrollTop:o})=>t({top:parseInt(i)+parseInt(o)+"px",left:parseInt(l)+"px",width:parseInt(s)+"px",height:parseInt(e)+"px"}))).exec()})).exec()},show(){this._size&&this._size>=this.count||(this.isShow=!0,this.lsjFile.dom.style.display="inline")},hide(){this.isShow=!1,this.lsjFile.dom.style.display="none"},upload(t){this.lsjFile&&this.lsjFile.upload(t)},onchange(t){return this._size=t.size,t.size>=this.count?this.hide():this.show()},onprogress(t,e=!1){this.$emit("progress",t),e&&setTimeout((()=>{this.$emit("uploadEnd",t)}),0)},clear(t){this.lsjFile.clear(t)},create(){let t=this.lsjFile.create("/uni_modules/lsj-upload/hybrid/html/uploadFile.html");this.$refs.lsj.$el.appendChild(t),this.show()},onClick(){this._size>=this.count&&this.toast(`只允许上传${this.count}个文件`)},toast(e){t({title:e,icon:"none"})}}},[["render",function(t,e,s,i,o,g){const f=c;return l(),a(f,{class:"lsj-file",style:r([g.getStyles])},{default:n((()=>[h(f,{ref:"lsj",class:"hFile",style:r([g.getStyles]),onClick:g.onClick},{default:n((()=>[p(t.$slots,"default",{},(()=>[h(f,{class:"defview",style:r([g.getStyles])},{default:n((()=>[d(u(s.buttonText),1)])),_:1},8,["style"])]),!0)])),_:3},8,["style","onClick"])])),_:3},8,["style"])}],["__scopeId","data-v-daa3316b"]]);export{y as _};
|