WxCommentLikeRepositores.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Controllers\Api\Repositories;
  3. use App\Models\Posts\WxComment;
  4. use App\Models\Posts\WxPost;
  5. use App\Models\WxCommentLike as Model;
  6. use App\Models\User\WxUser;
  7. use App\Wen\Utils\FieldUtils;
  8. use App\Wen\Utils\UserUtils;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Redis;
  12. class WxCommentLikeRepositores
  13. {
  14. /**
  15. * Model.
  16. *
  17. * @var string
  18. */
  19. protected $eloquentClass = Model::class;
  20. /**
  21. * 点赞
  22. */
  23. public static function like($uid, $comment_id)
  24. {
  25. DB::beginTransaction();
  26. try {
  27. $comment_modal = WxComment::where('id', $comment_id)->first();
  28. $comment_user_id = $comment_modal->user_id;
  29. $likeIsExists = Model::where('user_id',$uid)
  30. ->where('comment_id',$comment_id)->exists();
  31. // 判断是否已经点过赞
  32. if($likeIsExists){
  33. return true;
  34. }
  35. $post_id = $comment_modal->posts_id;
  36. $CommentLikeModel = new Model();
  37. $CommentLikeModel->user_id = $uid;
  38. $CommentLikeModel->comment_id = $comment_id;
  39. $CommentLikeModel->comment_user_id = $comment_user_id;
  40. $CommentLikeModel->save();
  41. $user = WxUser::where('id', $uid)->first(FieldUtils::userInfoColums());
  42. $comment = WxComment::where('id', $comment_id)->first();
  43. if($comment_user_id != $user['id']){
  44. UserUtils::add_user_notice(3003, $comment_user_id, '您的评论收到了一个喜欢', '「<a href="/pages/user/user?id='.$user['id'].'">'.$user['user_name'].'</a>」' . '喜欢了您的评论。', 101, $comment['posts_id']);
  45. }
  46. Cache::forget($uid.':islike:comment:'.$comment->id);
  47. Cache::forget('comment:likecount:'.$comment->id);
  48. Redis::sadd('realtime:post:set', $post_id);
  49. DB::commit();
  50. return true;
  51. } catch (\Exception $e) {
  52. DB::rollBack();
  53. _logger_(__file__, __line__, $e->getMessage());
  54. return false;
  55. }
  56. }
  57. public static function unlike($uid, $comment_id)
  58. {
  59. DB::beginTransaction();
  60. try {
  61. Model::where('user_id',$uid)
  62. ->where('comment_id',$comment_id)->delete();
  63. $post_id = WxComment::where('id', $comment_id)->value('posts_id');
  64. Redis::sadd('realtime:post:set', $post_id);
  65. Cache::forget($uid.':islike:comment:'.$comment_id);
  66. Cache::forget('comment:likecount:'.$comment_id);
  67. DB::commit();
  68. return true;
  69. } catch (\Exception $e) {
  70. DB::rollBack();
  71. _logger_(__file__, __line__, $e->getMessage());
  72. return false;
  73. }
  74. }
  75. }