ComplainController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Resources\ComplaintResource;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\User\UserModel;
  7. use App\Models\ComplaintModel;
  8. use App\Models\PartnerModel;
  9. use App\Models\Deed\InvitationCardModel;
  10. use App\Exceptions\AlertException;
  11. use App\Exceptions\NoticeException;
  12. use App\Services\Complain\ComplaintHandle;
  13. use App\Services\Complain\NoticeService;
  14. class ComplainController extends Controller
  15. {
  16. /**
  17. * 举报列表
  18. * @param Request $request
  19. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
  20. */
  21. public function index(Request $request)
  22. {
  23. $build = ComplaintModel::when($request->filled('state'), function ($query) use ($request) {
  24. return $query->where('state', $request->state);
  25. })->when($request->filled('search'), function ($query) use ($request) {
  26. return $query->where('search', 'like', "%" . $request->search . "%");
  27. })->when($request->filled('reply'), function ($query) use ($request) {
  28. if ($request->reply) {
  29. return $query->where('reply_at', '>', 0);
  30. } else {
  31. return $query->where('reply_at', '=', 0);
  32. }
  33. });
  34. $complaints = $build->orderBy(
  35. $request->input('sort_by', 'id'),
  36. $request->input('order', 'asc')
  37. )->paginate($request->input("per_page", 10));
  38. foreach ($complaints as &$complaint) {
  39. switch ($complaint->type) {
  40. case "partner":
  41. $partner = PartnerModel::find($complaint->value);
  42. $complaint->be_data = $partner;
  43. break;
  44. case "contact":
  45. $contact = InvitationCardModel::find($complaint->value);
  46. $complaint->be_data = $contact;
  47. break;
  48. case "pair":
  49. $pair = \App\Models\Fpdx\PairModel::find($complaint->value);
  50. $complaint->be_data = $pair;
  51. break;
  52. case "game":
  53. $game = \App\Models\Gteam\EnterModel::find($complaint->value);
  54. $complaint->be_data = $game;
  55. break;
  56. case "goodnight":
  57. $goodnight = \App\Models\Goodnight\EnterModel::find($complaint->value);
  58. $complaint->be_data = $goodnight;
  59. break;
  60. case "question":
  61. $question = \App\Models\User\LikeInviteQuestionModel::find($complaint->value);
  62. $question->question = json_decode($question->question, true);
  63. $complaint->be_data = $question;
  64. }
  65. $complaint->user = UserModel::find($complaint->uid);
  66. $complaint->be_user = UserModel::find($complaint->be_uid);
  67. }
  68. return ComplaintResource::collection($complaints);
  69. }
  70. /**
  71. * 删除某个字段
  72. * @param Request $request
  73. * @return array
  74. */
  75. public function erasing(Request $request, $id)
  76. {
  77. $this->validate($request, [
  78. 'fields' => 'required|array',
  79. ], [
  80. 'field.required' => '请选择一个字段'
  81. ]);
  82. $fields = $request->fields;
  83. try {
  84. $complaint = ComplaintHandle::make($id);
  85. foreach ($fields as $field) {
  86. $complaint->setField($field);
  87. }
  88. $complaint->erasing();
  89. } catch (\Exception $e) {
  90. throw new AlertException($e);
  91. }
  92. try {
  93. $notice = new NoticeService();
  94. $notice->erasingNotice($complaint->complaint);
  95. } catch (NoticeException $e) {
  96. app('sentry')->captureException($e);
  97. }
  98. return response()->json([
  99. 'code' => 200,
  100. 'message' => 'OK'
  101. ]);
  102. }
  103. /**
  104. * 回复
  105. * @param Request $request
  106. * @return array
  107. * @throws \Exception
  108. */
  109. public function reply(Request $request, $id)
  110. {
  111. $this->validate($request, [
  112. 'state' => 'required|in:1,2,3,4,5',
  113. 'content' => 'required|max:200',
  114. ], [
  115. 'state' => '请选择一种处理方式',
  116. 'content.required' => '请写回复内容',
  117. 'content.max' => '回复内容字数不得超出100字',
  118. ]);
  119. try {
  120. $complaint = ComplaintHandle::make($id);
  121. $complaint->state = $request->input('state');
  122. $complaint->reply_content = $request->input('content');
  123. $complaint->updateState();
  124. $complaint->reply();
  125. } catch (\Exception $e) {
  126. throw new AlertException($e);
  127. }
  128. $notice = new NoticeService();
  129. $notice->reply($complaint->complaint);
  130. return [
  131. 'code' => 200,
  132. 'message' => 'OK'
  133. ];
  134. }
  135. /**
  136. * 拉入黑名单
  137. * @param Request $request
  138. * @param int $id
  139. * @return AlertException|\Illuminate\Http\JsonResponse
  140. */
  141. public function defriend(Request $request, int $id)
  142. {
  143. $this->validate($request, [
  144. 'state' => 'required|in:1,2,3',
  145. ], [
  146. 'state.required' => '类型',
  147. ]);
  148. try {
  149. $state = $request->input('state');
  150. $complaint = ComplaintHandle::make($id);
  151. $complaint->setDefriend($state);
  152. $complaint->defriend();
  153. } catch (\Exception $e) {
  154. throw new AlertException($e);
  155. }
  156. $notice = new NoticeService();
  157. $notice->defriendNotice($complaint->complaint);
  158. $notice->defriendNoticeBeuser($complaint->complaint);
  159. return response([
  160. "code" => 200,
  161. "message" => "OK"
  162. ]);
  163. }
  164. }