//后端接口
public function collect()
{
$lou_id = \request()->get('lou_id');
$uid = \request()->get('uid');
$data = collectModel::where('lou_id',$lou_id)->first();
$lou = realModel::where('id',$lou_id)->first();
if (!$data){
collectModel::create([
'lou_id'=>$lou_id,
'uid'=>$uid,
'yong_price'=>$lou['yong_price'],
'price'=>$lou['price'],
'area'=>$lou['area'],
'type'=>$lou['type'],
'share'=>$lou['share'],
'pageviews'=>$lou['pageviews'],
'file'=>$lou['file'],
]);
return json_encode(['code'=>200,'msg'=>'收藏成功','data'=>'']);
}else{
$delete = collectModel::where('lou_id',$lou_id)->first();
if ($delete['deleted_at'] != ''){
collectModel::where('lou_id',$lou_id)->update(['deleted_at'=>null]);
return json_encode(['code'=>200,'msg'=>'收藏成功','data'=>'']);
}
collectModel::where('lou_id',$lou_id)->update(['deleted_at'=>time()]);
return json_encode(['code'=>2001,'msg'=>'取消收藏','data'=>'']);
}
}
//小程序端
//wxml
<image src="{{src}}" bindtap="add_collect" style="width: 50px; height: 50px;" data-id="{{data.id}}"></image>
//js
data: {
src:"/pages/image/shoucang1.png",
},
add_collect:function(res){
var lou_id = (res.currentTarget.dataset.id)
var uid = 100;
var _that = this;
wx.request({
url: 'http://www.exam.com/index.php/api/collect',
data:{
lou_id:lou_id,
uid:uid
},
method:'POST',
success:function(res){
var code = (res.data.code)
if(code == 200){
_that.setData({
src:"/pages/image/shoucang.png"
})
}
if(code == 2001){
_that.setData({
src:"/pages/image/shoucang1.png"
})
}
}
})
},