CircleRepository.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Circle;
  9. use App\Models\InterestCircle;
  10. use App\Models\InterestCircleUser;
  11. use Illuminate\Database\QueryException;
  12. use Dingo\Api\Http\Response;
  13. use Illuminate\Support\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. class CircleRepository
  18. {
  19. public function __construct(InterestCircle $interestCircle,
  20. InterestCircleUser $interestCircleUser)
  21. {
  22. $this->interestCircle = $interestCircle;
  23. $this->interestCircleUser = $interestCircleUser;
  24. }
  25. /**
  26. * 圈子列表
  27. */
  28. public function circleLists($request, $limit = 3)
  29. {
  30. $where[] = ['is_open', 1];
  31. if (isset($request['is_recommend'])) {
  32. $where[] = ['is_recommend', 1];
  33. }
  34. return $this->interestCircle
  35. ->where($where)
  36. ->orderBy('id', 'desc')
  37. ->take($limit)
  38. ->get();
  39. }
  40. /**
  41. * 圈子详情
  42. */
  43. public function detail($request, $isTrashed = false)
  44. {
  45. $model = $this->interestCircle;
  46. if ($isTrashed) {
  47. $model->withTrashed();
  48. }
  49. return $model->find($request['id']);
  50. }
  51. /**
  52. * 相册列表
  53. */
  54. public function pictureLists($request)
  55. {
  56. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  57. $where = [];
  58. if (isset($request['circle_id'])) {
  59. $where[] = ['circle_id', $request['circle_id']];
  60. }
  61. if (isset($request['uid'])) {
  62. $where[] = ['uid', $request['uid']];
  63. }
  64. return $this->interestCirclePicture
  65. ->where($where)
  66. ->orderBy('created_at', 'desc')
  67. ->paginate($perPage);
  68. }
  69. /**
  70. * 删除相册图片
  71. */
  72. public function deletePicture($request)
  73. {
  74. $circle = $this->interestCirclePicture->where('id', $request['id'])->first();
  75. if (!$circle) {
  76. return Response::create([
  77. 'message' => '获取图片信息失败',
  78. 'status_code' => 500
  79. ]);
  80. }
  81. DB::beginTransaction();
  82. try {
  83. $circle->delete();
  84. DB::commit();
  85. return Response::create();
  86. } catch (QueryException $exception) {
  87. DB::rollBack();
  88. Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
  89. return Response::create([
  90. 'message' => '操作失败,请重试',
  91. 'error' => $exception->getMessage(),
  92. 'status_code' => 500
  93. ]);
  94. }
  95. }
  96. /**
  97. * 圈子用户列表
  98. */
  99. public function memberLists($request)
  100. {
  101. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  102. $where[] = ['is_black', 0];
  103. $where[] = ['circle_id', $request['circle_id']];
  104. $userModel = $this->interestCircleUser;
  105. return $userModel
  106. ->where($where)
  107. ->orderBy('created_at', 'desc')
  108. ->paginate($perPage);
  109. }
  110. }