Vue+Bootstrap收藏(点赞)功能逻辑与具体实现
love : function(cid){
var target = this;
//获取点击收藏数据的id
var myChar = document.getElementById(cid);
//如果没登录,提示,this.formData.uid是当前登陆者id
if(this.formData.uid==undefined){
alert("请先登录");
}else{
//该数组存储了已经收藏的数据
if(this.lvs[cid]==1){
//由红心变为空心
myChar.className = "glyphicon glyphicon-heart-empty";
//通过数据id和用户id获取点赞表的id
axios.post('/bac/love/selectByCidAndUid?cid='+cid+'&uid='+target.formData.uid)
.then(function (response) {
var id = response.data.result.data.id;
//通过点赞表的id删除取消收藏的数据
axios.post('/bac/love/delete?objectId='+id)
.then(function (response) {
var success = response.data.success;
if(success==false){
alert(response.data.errorName);
}else{
console.log("删除成功");
}
})
.catch(function (error) {
console.log(error);
});
})
.catch(function (error) {
console.log(error);
});
//把数组中某数据id等2,使下次点击由空心变红心,相当于开关
target.lvs[cid]=2;
for(var i=0;i if(target.datas[i].cid==cid){ target.datas[i].count = target.datas[i].count-1; target.updateDatas.cid = target.datas[i].cid; target.updateDatas.count = target.datas[i].count; //更新数据表 axios.post('/bac/culture/updateByPrimaryKeySelective',target.updateDatas) .then(function (response) { var success = response.data.success; if(success==false){ alert(response.data.errorName); }else{ } }) .catch(function (error) { console.log(error); }); } } }else{ //变为红心 myChar.className = "glyphicon glyphicon-heart"; //获取数据id、用户id、喜欢的状态,插入点赞表 target.loveDatas.cid = cid; target.loveDatas.uid = target.formData.uid; target.loveDatas.type = 1; //插入点赞表 axios.post('/bac/love/insert',target.loveDatas) .then(function (response) { var success = response.data.success; if(success==false){ alert(response.data.errorName); }else{ console.log("插入成功"); } }) .catch(function (error) { console.log(error); }); //使下次点击由红心变空心 target.lvs[cid]=1; for(var i=0;i if(target.datas[i].cid==cid){ //使数值加1 target.datas[i].count = target.datas[i].count+1; //获取需要更新的数据表的id和count target.updateDatas.cid = target.datas[i].cid; target.updateDatas.count = target.datas[i].count; //更新数据表 axios.post('/bac/culture/updateByPrimaryKeySelective',target.updateDatas) .then(function (response) { var success = response.data.success; if(success==false){ alert(response.data.errorName); }else{ } }) .catch(function (error) { console.log(error); }); } } } } }