jhwxapp/miniprogram/pages/components/file-uploader-all/index.js

91 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-08-10 01:21:29 +08:00
// components/filling/index.js
const app = getApp();
Component({
/**
* 组件的属性列表
*/
properties: {
2024-03-17 16:19:31 +08:00
fileType:{
type:String,
value:"all",
},
2023-08-29 21:48:51 +08:00
fileUrlArray:{
type:Array
},
limit:{
type:Number
2023-08-10 01:21:29 +08:00
}
},
2023-08-29 21:48:51 +08:00
/**数据监听 */
observers: {
fileUrlArray:function(val){
this.setData({
uploaderList:val
})
}
},
2023-08-10 01:21:29 +08:00
/**
* 组件的初始数据
*/
data: {
2023-08-29 21:48:51 +08:00
quantity:5,
2023-08-10 01:21:29 +08:00
uploaderList: [],
uploaderNum:0,
showUpload:true
},
/**
* 组件的方法列表
*/
methods: {
// 删除图片
clearImg:function(e){
var nowList = [];//新数据
var uploaderList = this.data.uploaderList;//原数据
for (let i = 0; i < uploaderList.length;i++){
if (i == e.currentTarget.dataset.index){
continue;
}else{
nowList.push(uploaderList[i])
}
}
this.setData({
uploaderNum: this.data.uploaderNum - 1,
uploaderList: nowList,
showUpload: true
})
console.log(this.data.uploaderList)
2023-08-29 21:48:51 +08:00
this.triggerEvent("files", this.data.uploaderList);
2023-08-10 01:21:29 +08:00
},
//上传图片
upload: function(e) {
2023-08-29 21:48:51 +08:00
if(this.data.limit){
this.data.quantity=this.data.limit;
}
2023-08-10 01:21:29 +08:00
var that = this;
2023-08-29 21:48:51 +08:00
wx.chooseMessageFile({
2024-03-17 16:19:31 +08:00
type:that.data.fileType,
2023-08-10 01:21:29 +08:00
count: that.data.quantity - that.data.uploaderNum, // 默认9
success: function(res) {
// 返回选定照片的本地文件路径列表tempFilePath可以作为img标签的src属性显示图片
2023-08-29 21:48:51 +08:00
let tempFilePaths = res.tempFiles;
2023-08-10 01:21:29 +08:00
let uploaderList = that.data.uploaderList.concat(tempFilePaths);
if (uploaderList.length==that.data.quantity){
that.setData({
showUpload:false
})
}
that.setData({
uploaderList: uploaderList,
uploaderNum: uploaderList.length,
})
2023-08-29 21:48:51 +08:00
that.triggerEvent("files", that.data.uploaderList);
2023-08-10 01:21:29 +08:00
}
})
},
}
})