CircleMemberRepository.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\InterestCircleArticle;
  11. use App\Models\InterestCircleUser;
  12. use App\Models\Post;
  13. use Illuminate\Database\QueryException;
  14. use Dingo\Api\Http\Response;
  15. use Illuminate\Support\Carbon;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Redis;
  19. class CircleMemberRepository
  20. {
  21. public function __construct(InterestCircle $interestCircle,InterestCircleUser $interestCircleUser)
  22. {
  23. $this->interestCircleUser = $interestCircleUser;
  24. $this->interestCircle = $interestCircle;
  25. }
  26. /**
  27. * 用户列表
  28. */
  29. public function lists($request)
  30. {
  31. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  32. $where = [];
  33. if (isset($request['uid'])) {
  34. $where[] = ['uid', $request['uid']];
  35. }
  36. if (isset($request['user_type'])) {
  37. $where[] = ['is_black', $request['user_type']];
  38. }
  39. if (isset($request['circle_id'])) {
  40. $where[] = ['circle_id', $request['circle_id']];
  41. }
  42. $userModel = $this->interestCircleUser;
  43. return $userModel
  44. ->where($where)
  45. ->orderBy('created_at', 'desc')
  46. ->paginate($perPage);
  47. }
  48. /**
  49. * 设置该用户为当前圈子黑名单
  50. */
  51. public function setUserBlack($request)
  52. {
  53. $article = $this->interestCircleUser
  54. ->where('uid', $request['uid'])
  55. ->where('circle_id', $request['circle_id'])
  56. ->first();
  57. if (!$article) {
  58. return Response::create([
  59. 'message' => '当前用户不在该圈子',
  60. 'status_code' => 500
  61. ]);
  62. }
  63. if ($article->is_black == 1) {
  64. $article->is_black = 0;
  65. } else {
  66. $article->is_black = 1;
  67. }
  68. DB::beginTransaction();
  69. try {
  70. $article->save();
  71. DB::commit();
  72. return Response::create();
  73. } catch (QueryException $exception) {
  74. DB::rollBack();
  75. Log::debug('设置圈子黑名单:' . $request['uid'] . '-' . $request['circle_id'] . $exception->getMessage());
  76. return Response::create([
  77. 'message' => '操作失败,请重试',
  78. 'error' => $exception->getMessage(),
  79. 'status_code' => 500
  80. ]);
  81. }
  82. }
  83. }