EnterController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Http\Controllers\Gteam;
  3. use App\Http\Controllers\Miniprogram\Auth;
  4. use App\Http\Resources\Gteam\EnterResource;
  5. use App\Models\Gteam\EnterModel;
  6. use App\Services\Gteam\EnterService;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. class EnterController extends Controller
  10. {
  11. private $type = [
  12. "wz" => ["wz_wx", "wz_qq"],
  13. "cj" => ["cj_wx", "cj_qq"],
  14. ];
  15. public function store(Request $request)
  16. {
  17. $this->validate($request, [
  18. 'activity_id' => 'required',
  19. 'type' => 'required',
  20. ]);
  21. $uid = Auth::auth();
  22. $activity_id = $request->activity_id;
  23. $rank_type = $request->input('rank_type', 4);
  24. $game_type = $request->type;
  25. $sex = $request->input('sex', 0);
  26. \DB::beginTransaction();
  27. try {
  28. $enterService = new EnterService();
  29. $ack = $enterService->enter($uid, $activity_id, $game_type, $sex, $rank_type);
  30. $enterService->push($ack);
  31. \DB::commit();
  32. return response()->json([
  33. 'code' => 200,
  34. 'message' => 'OK',
  35. 'data' => [
  36. 'id' => $ack
  37. ]
  38. ]);
  39. } catch (\Exception $e) {
  40. app('sentry')->captureException($e);
  41. \DB::rollBack();
  42. throw $e;
  43. }
  44. }
  45. /**
  46. * 预约的列表
  47. * @param Request $request
  48. * @return \Illuminate\Http\JsonResponse
  49. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  50. */
  51. public function index(Request $request)
  52. {
  53. $uid = Auth::auth();
  54. $this->validate($request, [
  55. 'type' => 'in:wz,cj',
  56. ]);
  57. $builder = EnterModel::where('uid', $uid)->when($request->filled('type'), function ($query) use ($request) {
  58. return $query->whereIn('type', $this->type[$request->type]);
  59. })->where('activity_id', '>', 0);
  60. $enters = $builder->orderBy('created_at', 'desc')->paginate($request->get('per_page', 20));
  61. foreach ($enters as $enter) {
  62. $enter->activity;
  63. }
  64. return response()->json([
  65. 'code' => 200,
  66. 'data' => $enters
  67. ]);
  68. }
  69. public function show(Request $request, $id)
  70. {
  71. $uid = Auth::auth();
  72. $enter = EnterModel::findOrFail($id);
  73. $enter->room;
  74. $enter->activity;
  75. return new EnterResource($enter);
  76. }
  77. /**
  78. * 活动男女数量
  79. * @param int $activity_id
  80. * @return array
  81. */
  82. public function sexproportion(int $activity_id)
  83. {
  84. $es = new EnterService();
  85. $data = $es->sexproportion($activity_id);
  86. return array(
  87. 'code' => 200,
  88. 'message' => 'success',
  89. 'data' => $data
  90. );
  91. }
  92. /**
  93. * 取消报名
  94. * @param Request $request
  95. * @param $enter_id
  96. * @return \Illuminate\Http\JsonResponse
  97. */
  98. public function destroy(Request $request, $enter_id)
  99. {
  100. $service = new EnterService();
  101. $service->unenter($enter_id);
  102. return response()->json([
  103. "code" => 200,
  104. "message" => "OK"
  105. ]);
  106. }
  107. }