WxCollectRepositores.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Http\Controllers\Api\Repositories;
  3. use App\Models\Posts\WxCollect 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\DB;
  10. use Illuminate\Support\Facades\Redis;
  11. class WxCollectRepositores
  12. {
  13. /**
  14. * Model.
  15. *
  16. * @var string
  17. */
  18. protected $eloquentClass = Model::class;
  19. /**
  20. * 点赞
  21. */
  22. public static function collect($uid, $posts_id)
  23. {
  24. DB::beginTransaction();
  25. try {
  26. $posts_user_id = WxPost::where('id', $posts_id)->pluck('user_id')->first();
  27. $isExists = Model::where('posts_id', $posts_id)
  28. ->where('user_id', $uid)
  29. ->exists();
  30. if (!$isExists) {
  31. // 收藏
  32. $wxLike = new Model();
  33. $wxLike->posts_user_id = $posts_user_id;
  34. $wxLike->posts_id = $posts_id;
  35. $wxLike->user_id = $uid;
  36. $wxLike->save();
  37. if($uid != $posts_user_id){
  38. $user = UserUtils::get_cached_user($uid);
  39. UserUtils::add_user_notice(2006, $posts_user_id, '收到了一个收藏', '您的笔记被「' . '<a href="/pages/user/user?id='.$user['id'].'">'.$user['user_name'].'</a>' . '」收藏啦!', 101, $posts_id);
  40. }
  41. }
  42. Redis::sadd('realtime:post:set', $posts_id);
  43. DB::commit();
  44. return true;
  45. } catch (\Exception $e) {
  46. DB::rollBack();
  47. _logger_(__file__, __line__, $e->getMessage());
  48. return false;
  49. }
  50. }
  51. public static function uncollect($uid, $posts_id)
  52. {
  53. DB::beginTransaction();
  54. try {
  55. $posts_user_id = WxPost::where('id', $posts_id)->pluck('user_id')->first();
  56. // 取消收藏
  57. (new Model())->where('posts_id', $posts_id)
  58. ->where('user_id', $uid)
  59. ->delete();
  60. (new WxNotice())->where('posts_id', $posts_id)
  61. ->where('user_id', $posts_user_id)
  62. ->where('notice_type', 3)
  63. ->delete();
  64. Redis::sadd('realtime:post:set', $posts_id);
  65. DB::commit();
  66. return true;
  67. } catch (\Exception $e) {
  68. DB::rollBack();
  69. _logger_(__file__, __line__, $e->getMessage());
  70. return false;
  71. }
  72. }
  73. }