initital commit
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
// components/imageUpload/imageUpload.js
|
||||
Component({
|
||||
/**
|
||||
* 组件的属性列表
|
||||
*/
|
||||
properties: {
|
||||
// 最大上传个数
|
||||
maxLength: {
|
||||
type: Number,
|
||||
value: 9
|
||||
},
|
||||
// 单个图片限制 单位MB
|
||||
maxSize: {
|
||||
type: Number,
|
||||
value: 1
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 组件的初始数据
|
||||
*/
|
||||
data: {
|
||||
// 图片路径数组
|
||||
tempFilePaths: []
|
||||
},
|
||||
|
||||
/**
|
||||
* 组件的方法列表
|
||||
*/
|
||||
methods: {
|
||||
// 选择图片
|
||||
upload() {
|
||||
let that = this;
|
||||
let maxSize = this.data.maxSize * 1024 * 1024
|
||||
let flag = true
|
||||
let maxLength = this.data.maxLength
|
||||
console.log("maxLength",maxLength);
|
||||
wx.chooseImage({
|
||||
count: maxLength, // 默认9
|
||||
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
|
||||
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
|
||||
success: res => {
|
||||
wx.showToast({
|
||||
title: '正在上传...',
|
||||
icon: 'loading',
|
||||
mask: true,
|
||||
duration: 1000
|
||||
})
|
||||
// 判断数量
|
||||
if (res.tempFiles.length > maxLength) {
|
||||
wx.hideToast()
|
||||
flag = false
|
||||
wx.showToast({
|
||||
title: `最多上传${maxLength}张`,
|
||||
mask: true,
|
||||
duration: 1000
|
||||
})
|
||||
return false
|
||||
}
|
||||
res.tempFiles.forEach(item => {
|
||||
if (item.size > maxSize) {
|
||||
wx.hideToast()
|
||||
flag = false
|
||||
wx.showModal({
|
||||
title: '错误提示',
|
||||
content: `图片大于${that.data.maxSize}M`,
|
||||
showCancel: false,
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
console.log('用户点击确定')
|
||||
}
|
||||
}
|
||||
})
|
||||
return false
|
||||
}
|
||||
})
|
||||
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
|
||||
let tempFilePaths = res.tempFilePaths;
|
||||
if (flag) {
|
||||
that.setData({
|
||||
tempFilePaths: tempFilePaths
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 上传完成后把文件上传到服务器
|
||||
*/
|
||||
// if (flag) {
|
||||
// let count = 0;
|
||||
// for (let i = 0; i < tempFilePaths.length; i++) {
|
||||
// //上传文件
|
||||
// wx.uploadFile({
|
||||
// url: 'imgUploadUrl',
|
||||
// filePath: tempFilePaths[i],
|
||||
// name: 'uploadfile_ant',
|
||||
// header: {
|
||||
// "Content-Type": "multipart/form-data"
|
||||
// },
|
||||
// success: function (res) {
|
||||
// console.log("上传图片", res);
|
||||
// count++;
|
||||
// //如果是最后一张,则隐藏等待中
|
||||
// if (count == tempFilePaths.length) {
|
||||
// wx.hideToast();
|
||||
// }
|
||||
// },
|
||||
// fail: function (res) {
|
||||
// wx.hideToast();
|
||||
// wx.showModal({
|
||||
// title: '错误提示',
|
||||
// content: '上传图片失败',
|
||||
// showCancel: false,
|
||||
// success: function (res) {}
|
||||
// })
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
},
|
||||
|
||||
fail(err) {
|
||||
console.log("失败", err);
|
||||
}
|
||||
})
|
||||
},
|
||||
// 预览图片
|
||||
previewImage(e) {
|
||||
console.log("e", e);
|
||||
let index = e.target.dataset.index;
|
||||
let that = this;
|
||||
wx.previewImage({
|
||||
current: that.data.tempFilePaths[index],
|
||||
urls: that.data.tempFilePaths,
|
||||
success: function (res) {
|
||||
console.log("res", res);
|
||||
},
|
||||
fail: function (fail) {
|
||||
//console.log('fail')
|
||||
}
|
||||
})
|
||||
},
|
||||
// 删除图片
|
||||
deleteImage(e) {
|
||||
let that = this;
|
||||
let tempFilePaths = that.data.tempFilePaths;
|
||||
let index = e.currentTarget.dataset.index; //获取当前长按图片下标
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: '确定要删除此图片吗?',
|
||||
success: function (res) {
|
||||
if (res.confirm) {
|
||||
console.log('确定');
|
||||
tempFilePaths.splice(index, 1);
|
||||
} else if (res.cancel) {
|
||||
console.log('取消');
|
||||
return false;
|
||||
}
|
||||
that.setData({
|
||||
tempFilePaths
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user