// mycomponent/yz_uploader/yz_uploader.js var app = getApp(); Component({ /** * 组件的属性列表 */ properties: { max_count:{ value:100, type:Number }, customStyle:{ value:'', type:String }, customImgStyle:{ value:'', type:String }, customIconStyle:{ value:'', type:String }, customBoxStyle:{ value:'', type:String }, isSlot:{ value:false, type:Boolean }, fileSrc:{ value:'', type:[String,Array] } }, /** * 组件的初始数据 */ data: { fileList1: [], fileImg:[] }, observers:{ 'fileSrc'(newVal,oldVal){ if(newVal!='' && !Array.isArray(newVal)){ this.setData({ fileList1:[newVal] }); }else if(newVal!='' && Array.isArray(newVal)){ console.log(newVal); this.setData({ fileList1:newVal }); } } }, created(){ console.log(this,this.slot); }, /** * 组件的方法列表 */ methods: { updateImages() { // console.log(that.data.fileList1); this.triggerEvent('updateImages', this.data.fileList1); }, //预览图片,放大预览 preview(e) { let currentUrl = e.currentTarget.dataset.src; wx.previewImage({ current: this.data.fileList1[currentUrl], // 当前显示图片的http链接 urls: this.data.fileList1 // 需要预览的图片http链接列表 }); }, delIntu_1(e) { let del = e.currentTarget.id; let url = this.data.fileList1.splice(del, 1); this.setData({ fileList1: this.data.fileList1, fileImg: this.data.fileImg.splice(del,1) }); this.triggerEvent("delete",{url,index:del}); this.updateImages(); this.triggerEvent('inputimg',this.data.fileImg) }, onRead_1() { let that = this; let ig = '1'; wx.chooseImage({ count: this.data.max_count-this.data.fileList1.length, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success(res) { // console.log(res.tempFilePaths); that.triggerEvent('beforeUp', res.tempFilePaths); that.unload({ tempFilePaths: res.tempFilePaths }, ig); } }); }, //多张上传方法 unload(data, ig) { let urlStr = app.getNetAddresss("upload.uploadPic"); var that = this, i = data.i ? data.i : 0, success = data.success ? data.success : 0, fail = data.fail ? data.fail : 0; wx.showLoading({ title: '正在上传', }); wx.uploadFile({ url: urlStr, filePath: data.tempFilePaths[i], name: 'file', formData: null, success(resdata) { var res = JSON.parse(resdata.data); // console.log(res.data.img_url); if (ig == '1') { that.data.fileList1.push(res.data.img_url); that.setData({ fileList1: that.data.fileList1, }); that.triggerEvent('successUp', {res,list:that.data.fileList1}); // console.log(that.data.fileList1); that.updateImages(); that.data.fileImg.push(res.data.img) that.triggerEvent('inputimg',that.data.fileImg) } }, fail(e) { fail++; }, complete(e) { wx.hideLoading(); i++; if (i == data.tempFilePaths.length) { //当图片传完时,停止调用 console.log('执行完毕'); console.log('成功:' + success + " 失败:" + fail); } else { //若图片还没有传完,则继续调用函数 data.i = i; data.success = success; data.fail = fail; that.unload(data, ig); //递归,回调自己 } } }); }, } });