0
0

PostLike.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. class PostLike extends Base
  7. {
  8. public function like(){
  9. $postId = input('postId');
  10. $type = input('type'); // 0=点赞 1=取消赞
  11. $info = Db::name('post_like')
  12. ->where('post_id',$postId)
  13. ->where('user_id',$this->userId)
  14. ->find();
  15. if($type == 0){
  16. if($info){
  17. HelpHander::error('已赞');
  18. }else{
  19. $data=[
  20. 'post_id'=>$postId,
  21. 'user_id'=>$this->userId,
  22. ];
  23. $ret=Db::name('post_like')->insert($data);
  24. if($ret){
  25. HelpHander::success([],'点赞成功');
  26. }else{
  27. HelpHander::error('点赞失败');
  28. }
  29. }
  30. }else{
  31. if(!$info){
  32. HelpHander::error('已取消');
  33. }
  34. $ret=Db::name('post_like')
  35. ->where('post_id',$postId)
  36. ->where('user_id',$this->userId)
  37. ->delete();
  38. if($ret){
  39. HelpHander::success([],'取消点赞成功');
  40. }else{
  41. HelpHander::error('取消点赞失败');
  42. }
  43. }
  44. }
  45. }