WxLikeRepositores.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Http\Controllers\Api\Repositories;
  3. use App\Models\Posts\WxLike as Model;
  4. use App\Models\Posts\WxPost;
  5. use App\Models\WxNotice;
  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 WxLikeRepositores
  13. {
  14. /**
  15. * Model.
  16. *
  17. * @var string
  18. */
  19. protected $eloquentClass = Model::class;
  20. /**
  21. * 点赞
  22. */
  23. public static function admin_like($uid, $posts_id)
  24. {
  25. DB::beginTransaction();
  26. try {
  27. $posts_user_id = WxPost::where('id', $posts_id)->value('user_id');
  28. if(_empty_($posts_user_id)){
  29. DB::rollBack();
  30. return false;
  31. }
  32. $isExists = Model::where('posts_id', $posts_id)
  33. ->where('user_id', $uid)
  34. ->exists();
  35. if (!$isExists) {
  36. // 点赞
  37. $wxLike = new Model();
  38. $wxLike->posts_user_id = $posts_user_id;
  39. $wxLike->posts_id = $posts_id;
  40. $wxLike->user_id = $uid;
  41. $wxLike->created_at = time();
  42. $wxLike->updated_at = time();
  43. $wxLike->save();
  44. $user = UserUtils::get_cached_user($uid);
  45. if($posts_user_id != $user['id']) {
  46. UserUtils::add_user_notice(2003, $posts_user_id, '收到了一个喜欢', '您的笔记收到了「' . '<a href="/pages/user/user?id=' . $user['id'] . '">' . $user['user_name'] . '</a>' . '」的喜欢。', 101, $posts_id);
  47. }
  48. }
  49. Redis::sadd('realtime:post:set', $posts_id);
  50. Cache::forget('posts:likeCont:'.$posts_id);
  51. DB::commit();
  52. return true;
  53. } catch (\Exception $e) {
  54. DB::rollBack();
  55. _logger_(__file__, __line__, $e->getMessage());
  56. return false;
  57. }
  58. }
  59. public static function unlike($uid, $posts_id)
  60. {
  61. DB::beginTransaction();
  62. try {
  63. $posts_user_id = WxPost::where('id', $posts_id)->pluck('user_id')->first();
  64. // 取消点赞
  65. (new Model())->where('posts_id', $posts_id)
  66. ->where('user_id', $uid)
  67. ->delete();
  68. (new WxNotice())->where('posts_id', $posts_id)
  69. ->where('user_id', $posts_user_id)
  70. ->where('notice_type', 2)
  71. ->delete();
  72. Redis::sadd('realtime:post:set', $posts_id);
  73. DB::commit();
  74. return true;
  75. } catch (\Exception $e) {
  76. DB::rollBack();
  77. _logger_(__file__, __line__, $e->getMessage());
  78. return false;
  79. }
  80. }
  81. }