EnterController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Controllers\Goodnight;
  3. use App\Http\Resources\Goodnight\EnterCollection;
  4. use App\Http\Resources\Goodnight\EnterResource;
  5. use App\Models\Goodnight\ActivityModel;
  6. use App\Models\Goodnight\EnterModel;
  7. use App\Models\Goodnight\RoomModel;
  8. use App\Services\Goodnight\EnterService;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Controller;
  11. use App\Http\Controllers\Miniprogram\Auth;
  12. class EnterController extends Controller
  13. {
  14. // 创建报名
  15. public function store(Request $request)
  16. {
  17. $type = $request->input('type', '报名');
  18. $uid = Auth::auth();
  19. \DB::beginTransaction();
  20. try {
  21. $enterService = new EnterService();
  22. $enter_id = $enterService->enter($uid, $type);
  23. \DB::commit();
  24. return response()->json([
  25. 'code' => 200,
  26. 'message' => 'OK',
  27. 'data' => [
  28. 'enter' => $enter_id
  29. ]
  30. ]);
  31. } catch (\Exception $exception) {
  32. \DB::rollBack();
  33. return array(
  34. 'code' => $exception->getCode(),
  35. 'message' => $exception->getMessage()
  36. );
  37. }
  38. }
  39. // 报名列表
  40. public function index(Request $request)
  41. {
  42. $uid = Auth::auth();
  43. $builder = EnterModel::where('uid', $uid);
  44. $enters = $builder->orderBy('activity_id', 'desc')->paginate($request->get('per_page', 20));
  45. foreach ($enters as $enter) {
  46. $enter->activity;
  47. }
  48. return new EnterCollection($enters);
  49. }
  50. // 查看报名信息
  51. public function show(Request $request, $id)
  52. {
  53. $uid = Auth::auth();
  54. $enter = EnterModel::findOrFail($id);
  55. $enter->activity;
  56. return new EnterResource($enter);
  57. }
  58. public function today(Request $request)
  59. {
  60. $activity = ActivityModel::whereRaw("from_unixtime(`showed_at`, '%Y-%m-%d') = ?", [date('Y-m-d')])->first();
  61. $uid = Auth::auth();
  62. $enter = EnterModel::where('uid', $uid)->where('type', '报名')->where('activity_id', $activity->id)->first();
  63. $room = null;
  64. if ($enter) {
  65. $room = RoomModel::find($enter->room_id);
  66. } else {
  67. $activity = ActivityModel::where([
  68. ['opened_at', '<', time()],
  69. ['closed_at', '>', time()]
  70. ])->firstOrFail();
  71. $enter = EnterModel::where('uid', $uid)->where('type', '报名')->where('activity_id', $activity->id)->first();
  72. if ($enter) {
  73. $room = RoomModel::find($enter->room_id);
  74. }
  75. }
  76. return response()->json([
  77. 'code' => 200,
  78. 'message' => 'OK',
  79. 'data' => [
  80. 'activity' => $activity,
  81. 'enter' => $enter,
  82. 'room' => $room
  83. ]
  84. ]);
  85. }
  86. }