微信小程序 图片打开相册选择图片,图片预览,图片删除效果

微信小程序 打开图片选择图片,图片预览,图片删除效果
1openPhoto:function(e){
var me = this ;
// 设置点击图片的信息 用来更改事件
if(e.currentTarget.dataset.item != me.data.photo){
var urlArr = new Array();
urlArr.push(e.currentTarget.dataset.item)
console.log(urlArr,’*’)
wx.previewImage({
urls: urlArr //需要预览的图片链接列表,
});
me.setData({
checkPhoto: e.currentTarget.dataset.item
})
}else{
wx.chooseImage({
count: 3,
success (res) {
// debugger;
if(me.data.photoNum.length == 4){
console.log(me.data.photoNum,‘添加照片的个数’)
return
}else{
const tempFilePaths = res.tempFilePaths;
console.log(tempFilePaths)
for(var i = 0 ;i < tempFilePaths.length ;i++){
var arr = new Array();
arr = me.data.photoNum
if(arr.length > 1){
arr[arr.length -1] = tempFilePaths[i]
arr[arr.length] = ‘http://xxx.png’
}else{
arr.unshift(tempFilePaths[i])
}
}
me.setData({
photoNum: arr,
})
console.log(arr,“照片数组”)
var deleteImgArr = new Array();
deleteImgArr = me.data.deleteImg
switch(me.data.photoNum.length){
case 2:
deleteImgArr = [1]
me.setData({
deleteImg: deleteImgArr
})
break;
case 3:
deleteImgArr = [1,2]
me.setData({
deleteImg: deleteImgArr
})
break;
case 4:
deleteImgArr = [1,2,3]
me.setData({
deleteImg: deleteImgArr
})
break;
}
}
}
})
}
},
// 删除图片
deleteImg: function(e){
var me = this ;
console.log(e.currentTarget.dataset.imgindex,‘删除图片的下标识’)
switch(e.currentTarget.dataset.imgindex){
case 0:
if(me.data.photoNum.length == 4){
var deleteIMgArr = new Array();
deleteIMgArr = me.data.photoNum
deleteIMgArr.splice(2,1)
me.setData({
photoNum: deleteIMgArr,
deleteImg:[1,2]
})
}else if(me.data.photoNum.length == 3){
var deleteIMgArr = new Array();
deleteIMgArr = me.data.photoNum
deleteIMgArr.splice(1,1)
me.setData({
photoNum: deleteIMgArr,
deleteImg:[1]
})
}else if(me.data.photoNum.length == 2){
var deleteIMgArr = new Array();
deleteIMgArr = me.data.photoNum
deleteIMgArr.splice(0,1)
me.setData({
photoNum: deleteIMgArr,
deleteImg:[]
})
}
break;
case 1:
if(me.data.photoNum.length == 4){
var deleteIMgArr = new Array();
deleteIMgArr = me.data.photoNum
deleteIMgArr.splice(1,1)
me.setData({
photoNum: deleteIMgArr,
deleteImg:[1,2]
})
}else if(me.data.photoNum.length == 3){
var deleteIMgArr = new Array();
deleteIMgArr = me.data.photoNum
deleteIMgArr.splice(0,1)
me.setData({
photoNum: deleteIMgArr,
deleteImg:[1]
})
}
break;
case 2:
var deleteIMgArr = new Array();
deleteIMgArr = me.data.photoNum
deleteIMgArr.splice(0,1)
me.setData({
photoNum: deleteIMgArr,
})
if(me.data.photoNum.length == 3){
me.setData({
deleteImg:[1,2]
})
}
break;
}

你可能感兴趣的:(微信小程序)