微信小程序-评论业务的实现
一、效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/04c6a957c40e14e4626c7daa9e655288.gif)
二、实现
1.wxml
<!--
wx:index = "index":列表循环后所有位置都可以访问索引
-->
<view class="item" wx:for="{{list}}" wx:index = "index">
<view class="left">
<image class="avatar"></image>
</view>
<view class="right">
<view class="nickname">{{item.nickName}}</view>
<view class="content">{{item.content}}</view>
<view class="image-list">
<image class="image" wx:for="{{item.imageList}}"></image>
</view>
<view class="time-area">
<view class="time">{{item.time}}</view>
<view>
<!--
data-index="{{index}}"
1.设置后在回调函数中currentTarget.dataset中显示
-->
<image class="operation-button" src="../../images/caozuo.png" catchtap="showOperationPannel" data-index="{{index}}"></image>
<!-- 判断当前索引和面盘索引是否一致 -->
<view class="operation-pannel" wx:if="{{showOperationPannelIndex == index}}">
<!-- 设置索引和点击函数 -->
<view class="tab" catchtap="clickLove" data-index="{{index}}">
<image class="image" src="../../images/love-white.png"></image>
<text>赞</text>
</view>
<view class="tab" catchtap="clickComment" data-index="{{index}}">
<image class="image" src="../../images/comment-white.png"></image>
<text>评论</text>
</view>
</view>
</view>
</view>
<view class="love-comment">
<!--
item.loveList=重复
item.loveList
<view class="love" wx:if="{{item.loveList.length > 0}}">
<image class="love-icon" src="../../images/love-blue.png"></image>
<text class="love-nickname" wx:for="{{item.loveList}}">老夫子 兰陵王</text>
</view>
-->
<view class="love" wx:if="{{item.loveList.length > 0}}">
<image class="love-icon" src="../../images/love-blue.png"></image>
<!-- love和整个循环的item不冲突 -->
<text class="love-nickname" wx:for-items="{{item.loveList}}"
wx:for-item = "love"
>{{love.nickName}}</text>
</view>
<view class="comment" wx:if="{{item.commentList.length > 0}}">
<view wx:for-items="{{item.commentList}}"
wx:for-item = "comment">
<text class="comment-nickname">{{comment.nickName}}</text>
<text class="comment-content">{{comment.content}}</text>
</view>
</view>
</view>
</view>
</view>
<view class="comment-add" style="bottom:{{heightBottom != '' ? heightBottom : 0}}rpx;"
wx:if="{{showCommentAdd}}"
>
<input
placeholder="请输入评论"
focus="{{showCommentAdd}}"
bindinput="bindInput"
bindfocus="bindFocus"
bindblur="bindBlur"
class="comment-input">
</input>
<button class="comment-send" catchtap="clickSend" type="primary" disabled="{{commentContent.length > 0 ? false : true}}" size="mini">发 送</button>
</view>
2.js
var that;
Page({
data: {
currentCircleIndex:-1,
showCommentAdd:false,
commentContent:'',
heightBottom:'',
userInfo:null,
list:[],
showOperationPannelIndex:-1,
},
onLoad: function (options) {
that = this;
for (var i = 1; i < 10; i++) {
var circleData = {};
circleData.nickName = "朋友-" + i;
circleData.content = "朋友发布内容-" + i;
circleData.time = "2020-05-0" + i;
var imageList = [];
var loveList = [];
var commentList = [];
circleData.imageList = imageList;
circleData.loveList = loveList;
circleData.commentList = commentList;
for (var j = 1; j < i; j++) {
imageList.push(j);
var loveData = {};
loveData.nickName = '点赞-' + j;
loveList.push(loveData);
var commentData = {};
commentData.nickName = "兰陵王-" + j + ":";
commentData.content = "评论内容-" + j;
commentList.push(commentData);
}
that.data.list.push(circleData);
}
that.setData({
list: that.data.list
})
wx.getStorage({
key: 'userInfo',
success(res){
console.log("getStorage success:",JSON.parse(res.data));
that.setData({
userInfo:JSON.parse(res.data)
})
}
})
},
showOperationPannel(e){
console.log("showOperationPannel",e);
var index = e.currentTarget.dataset.index;
if(that.data.showOperationPannelIndex == index){
that.setData({
showOperationPannelIndex:-1
})
}
else{
that.setData({
showOperationPannelIndex:index
})
}
},
clickLove(e){
console.log(e);
var index = e.currentTarget.dataset.index;
var circleData = that.data.list[index];
var loveList = circleData.loveList;
var isHaveLove = false;
for(var i = 0; i < loveList.length; i++){
if(that.data.userInfo.nickName == loveList[i].nickName){
isHaveLove = true;
loveList.splice(i,1);
break;
}
}
if(!isHaveLove){
loveList.push({nickName:that.data.userInfo.nickName});
}
that.setData({
list:that.data.list,
showOperationPannelIndex:-1
})
},
bindInput(e){
console.log(e);
that.setData({
commentContent:e.detail.value
})
},
bindFocus(e){
console.log(e);
that.setData({
heightBottom:e.detail.height
})
},
bindBlur(e){
that.setData({
showCommentAdd:false,
})
},
clickComment(e){
that.setData({
currentCircleIndex:e.currentTarget.dataset.index,
showCommentAdd:true,
showOperationPannelIndex:-1
})
},
clickSend(e){
console.log(e);
var circleData = that.data.list[that.data.currentCircleIndex];
var commentList = circleData.commentList;
var commentData = {};
commentData.nickName = that.data.userInfo.nickName + ":";
commentData.content = that.data.commentContent;
commentList.push(commentData);
that.setData({
list:that.data.list,
showCommentAdd:false,
commentContent:''
})
},
})
3.wxss
page{
background-color: #fff;
}
.item{
display: flex;
width: 100%;
padding: 20rpx 20rpx;
border-bottom: 1px solid #f0f0f0;
}
.left{
width: 80rpx;
margin-right: 10rpx;
}
.avatar{
width: 80rpx;
height: 80rpx;
margin-right: 10rpx;
background: #f3f3f3;
}
.right{
width: 620rpx;
display: flex;
flex-direction: column;
}
.nickname{
font-size: 32rpx;
line-height: 50rpx;
color: #3b4977;
}
.content{
font-size: 32rpx;
line-height: 40rpx;
color: #181818;
margin-bottom: 10rpx;
}
.image-list{
display: flex;
flex-wrap: wrap;
}
.image-list .image{
width: 160rpx;
height: 160rpx;
background: #f3f3f3;
margin-right: 10rpx;
margin-bottom: 10rpx;
}
.time-area{
display: flex;
height: 72rpx;
position: relative;
align-items: center;
}
.time-area .time{
font-size: 26rpx;
line-height: 70rpx;
color: #696969;
}
.time-area .operation-button{
height: 40rpx;
width: 70rpx;
position: absolute;
right: 0rpx;
top: 15rpx;
}
.time-area .operation-pannel{
position: absolute;
top: 0rpx;
right: 80rpx;
width: 300rpx;
line-height: 70rpx;
background-color: #4c5154;
border-radius: 10rpx;
font-size: 24rpx;
color: white;
text-align: center;
}
.time-area .operation-pannel .tab{
display: inline-block;
width: 150rpx;
}
.time-area .operation-pannel .tab .image{
width: 26rpx;
height: 26rpx;
vertical-align: middle;
background-color: transparent;
}
.love-comment{
margin-top: 10rpx;
background-color: #f7f7f7;
width: 100%;
}
.love{
padding: 6rpx 16rpx;
border-bottom: 1rpx solid #f0f0f0;
}
.love .love-icon{
width: 26rpx;
height: 26rpx;
}
.love .love-nickname{
font-size: 30rpx;
line-height: 40rpx;
color: #3b4977;
}
.comment{
padding: 6rpx 16rpx;
padding-bottom: 12rpx;
font-size: 30rpx;
}
.comment .comment-nickname{
color: #3b4977;
}
.comment .comment-content{
color: #181818;
line-height: 40rpx;
}
.comment-add{
background-color: white;
position: fixed;
bottom: 0rpx;
width: 100%;
height: 100rpx;
display: flex;
align-items: center;
border-top: 1px solid #f3f3f3;
}
.comment-add .comment-input{
height: 52rpx;
line-height: 52rpx;
vertical-align: middle;
color: #181818;
margin-left: 20rpx;
margin-right: 20rpx;
padding: 5rpx 20rpx;
background-color: #f3f3f3;
flex-grow: 1;
}
.comment-add .comment-send{
margin-right: 20rpx;
font-size: 22rpx;
}