思路:
1.先在data里面声明id和index索引,是否点赞isLike;
2.绑定事件设置三个data-的值,获取当前的id,index,isLike。
3.点击事件,判断是否为1,是的话弹窗提示并不执行下面的代码,否则,请求接口且成功的话,把isLike的值,通过setData赋给变量isLike.
注意,具体操作那个需要利用index.
<view class='' bindtap='onLike' data-index='{{index}}' data-isLike='{{item.isLike}}' data-schemeId='{{item.schemeId}}'>
<image mode='widthFix' src='{{item.isLike==1?2.png:1.png}}'></image>
</view>
data: {
follow_info:[],
//isLike: 0,
schemeId:null,
index:null
},
onLike(e) {
var self = this;
var isLike = e.currentTarget.dataset.islike;
var index = e.currentTarget.dataset.index;
if(isLike==1){
wx.showToast({
title: '已点赞',
icon: 'none',
duration: 1000
});
return false;
}
var schemeId = e.currentTarget.dataset.schemeid;
self.setData({
schemeId: schemeId,
index: index
})
app.checkSessionKey(this.onLike_fn)
},
onLike_fn(obj) {
var self = this;
var openId = obj.openId;
var sessionKey = obj.sessionKey;
var schemeId = self.data.schemeId;
wx.request({
url: app.globalData.http + '/a/b',
method: 'POST',
data: {
openid: openId,
session_key: sessionKey,
schemeId: schemeId
},
success: function (res) {
if (res.data.code == 200) {
var follow_info = self.data.follow_info;
var index = self.data.index;
follow_info[index].isLike = 1;
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1000
});
self.setData({
follow_info: follow_info
})
}
}
})
},
网友评论