点赞 小程序端wxml:
<view>
<image wx:if="{{item.like==1}}" bindtap="like" data-id="{{item.id}}" src="/img/sheart.png" style="width:80rpx;height:80rpx">
</image>
<image wx:else bindtap="like" data-id="{{item.id}}" src="/img/heart.png" style="width:80rpx;height:80rpx">
</image>
</view>
js:
like:function(e){
let tid = e.target.dataset.id
let token = wx.getStorageSync('token')
let that = this
wx.request({
url: 'http://www.xxx.com/api/giveLike',
header:{token},
data:{token,tid},
success:function(res){
that.onLoad()
}
})
},
后端php:
public function giveLike(Request $request){
// 接收数据 并解密id
$token = $request->input('token');
$uid = common::checkToken($token);
// 动态id
$tid = $request->input('tid');
// 查询是否点赞
$data = Like::where(['uid'=>$uid,'tid'=>$tid])->first();
if (!$data){
// 创建点赞信息
$res = Like::create(['uid'=>$uid,'tid'=>$tid]);
return ['code'=>200,'msg'=>'点赞成功','data'=>''];
}
// 删除点赞信息
$res = Like::where(['uid'=>$uid,'tid'=>$tid])->delete();
return ['code'=>200,'msg'=>'取消点赞成功','data'=>''];
}
评论 小程序端wxml:
<view>
评论区: <text>\n\n</text>
<view wx:for="{{item.comment}}" wx:key="comment">
{{item.user.nickname}} : {{item.content}}
<text>\n\n</text>
</view>
<form catchsubmit="commit">
<l-input label="评论" name="content" value="{{content}}"></l-input>
<input type="text" hidden="true" name="tid" value="{{item.id}}"/>
<button form-type="submit">评论</button>
</form>
</view>
js:
// 评论
commit:function(e){
let content = e.detail.value.content
if(content.length < 4){
wx.showToast({
title: '不得少于4个字符',
icon:'error'
})
}
let tid = e.detail.value.tid
let token = wx.getStorageSync('token')
let that = this
wx.request({
url: 'http://www.xxx.com/api/comment',
header:{token},
data:{token,content,tid},
success:function(res){
if(res.data.code != 200 ){
wx.showToast({
title: res.data.msg,
icon:'error'
})
}
that.onLoad()
}
})
},
后端php:
// 评论
public function comment(Request $request){
// 接收数据 并解密id
$token = $request->input('token');
$uid = common::checkToken($token);
// 动态id
$tid = $request->input('tid');
// 评论内容
$content = $request->input('content');
try {
Comment::create(['uid'=>$uid,'tid'=>$tid,'content'=>$content]);
return ['code'=>200,'msg'=>'评论成功','data'=>''];
}catch (\Exception $e){
return ['code'=>400,'msg'=>$e->getMessage(),'data'=>''];
}
}
|