微信小程序~评论加图片上传、预览、删除
先看看效果图,有木有自己要用的
wxml:
<view class="conts">
<textarea class="areas" placeholder='留下点评,帮助更多人' minlength="{{min}}" maxlength="{{max}}" bindinput="inputs"></textarea>
<text class="hint">{{texts}}</text>
<text class="currentWordNumber">{{currentWordNumber|0}}/{{max}}</text>
</view>
<view class="img_box">
<view class="imgs" wx:for="{{tempFilePaths}}" wx:key="index">
<image src='{{item}}' bindlongpress="deleteImage" bindtap="listenerButtonPreviewImage" data-index="{{index}}" mode='widthFix' />
</view>
<view class="imgs">
<view class="images" bindtap="upload">
<image src='../../image/tianjia.png' mode='widthFix' />
</view>
</view>
</view>
wxss:
/* 评论 */
.conts{
width:90%;
height: auto;
position:relative;
padding-bottom:50rpx;
margin:0 auto;
}
.areas{
width:100%;
height:152rpx;
font-size: 30rpx;
text-indent: 28rpx;
border: 1rpx solid gainsboro;
padding-top: 30rpx;
margin: 0 auto;
overflow: hidden;
position: relative;
}
.currentWordNumber{
font-size: 28rpx;
color: gray;
position: absolute;
right:10rpx;
bottom:10rpx;
}
.hint{
font-size: 28rpx;
position: absolute;
left:20rpx;
bottom:10rpx;
color: #FF6600;
}
/* 图片 */
.img_box{
width:690rpx;
position:relative;
display: flex;
flex-wrap: wrap;
margin:0 auto;
}
.imgs{
width:33.33333333%;
display: flex;
justify-content: center;
margin-bottom:20rpx;
}
.imgs image{
width:90%;
max-height:212rpx;
border:1px solid rgba(214, 212, 212, 0.1);
/* box-shadow: 5rpx 5rpx 1rpx 3rpx #e2e0e0; */
}
.imgs .images{
position:relative;
}
.images button{
width:100%;
height:100%;
position:absolute;
top:0;
left:0;
}
.img_box .images{
width:90%;
height: 212rpx;
border:1px solid #E8E8E8;
border-radius:4rpx;
display: flex;
align-items: center;
justify-content: center;
}
.img_box .images>image{
width:60rpx;
height:60rpx;
}
js:
Page({
/**
* 页面的初始数据
*/
data: {
texts: "",
min: 5,//最少字数
max: 22, //最多字数 (根据自己需求改变)
tempFilePaths: []
},
/**
* 字数限制
*/
inputs: function (e) {
// 获取输入框的内容
var value = e.detail.value;
// 获取输入框内容的长度
var len = parseInt(value.length);
//最少字数限制
if (len <this.data.min){
this.setData({
texts: "加油,至少要输入5个字哦"
})
}else if (len >= this.data.min){
this.setData({
texts: " "
})
}
//最多字数限制
if (len > this.data.max) return;
// 当输入框内容的长度大于最大长度限制(max)时,终止setData()的执行
this.setData({
currentWordNumber: len //当前字数
});
},
/**
* 上传图片方法
*/
upload: function () {
let that = this;
wx.chooseImage({
count: 9, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: res => {
wx.showToast({
title: '正在上传...',
icon: 'loading',
mask: true,
duration: 1000
})
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
let tempFilePaths = res.tempFilePaths;
that.setData({
tempFilePaths: tempFilePaths
})
/**
* 上传完成后把文件上传到服务器
*/
var count = 0;
for (var i = 0, h = tempFilePaths.length; i < h; i++) {
//上传文件
/* wx.uploadFile({
url: HOST + '地址路径',
filePath: tempFilePaths[i],
name: 'uploadfile_ant',
header: {
"Content-Type": "multipart/form-data"
},
success: function (res) {
count++;
//如果是最后一张,则隐藏等待中
if (count == tempFilePaths.length) {
wx.hideToast();
}
},
fail: function (res) {
wx.hideToast();
wx.showModal({
title: '错误提示',
content: '上传图片失败',
showCancel: false,
success: function (res) { }
})
}
});*/
}
}
})
},
/**
* 预览图片方法
*/
listenerButtonPreviewImage: function (e) {
let index = e.target.dataset.index;
let that = this;
console.log(that.data.tempFilePaths[index]);
console.log(that.data.tempFilePaths);
wx.previewImage({
current: that.data.tempFilePaths[index],
urls: that.data.tempFilePaths,
//这根本就不走
success: function (res) {
//console.log(res);
},
//也根本不走
fail: function () {
//console.log('fail')
}
})
},
/**
* 长按删除图片
*/
deleteImage: function (e) {
var that = this;
var tempFilePaths = that.data.tempFilePaths;
var 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
});
}
})
}
})