ActivityReward.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Constants\HttpStatus;
  4. use App\Models\ActivityReward as ActivityRewardModel;
  5. use App\Models\ActivityUser;
  6. use App\Services\ActivityRewardService;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Validation\ValidationException;
  9. use Exception;
  10. class ActivityReward extends BaseController
  11. {
  12. public function participate(): JsonResponse
  13. {
  14. try {
  15. request()->validate([
  16. 'member_id' => ['required', 'integer', 'exists:users,member_id'],
  17. 'activity_id' => ['required', 'integer'],
  18. ]);
  19. $memberId = request()->input('member_id');
  20. if (ActivityUser::where('member_id', $memberId)->where('status', 0)->exists()) {
  21. throw new Exception('完成活动才可以参与新活动', HttpStatus::CUSTOM_ERROR);
  22. }
  23. $activityId = request()->input('activity_id');
  24. if (ActivityUser::where('member_id', $memberId)->where('activity_id', $activityId)->exists()) {
  25. throw new Exception('已参与过此活动,每个活动仅可参加一次', HttpStatus::CUSTOM_ERROR);
  26. }
  27. $time = time();
  28. $activity = ActivityRewardService::findOne([
  29. 'start_time' => ['<=', $time],
  30. 'end_time' => ['>=', $time],
  31. 'status' => ActivityRewardModel::STATUS_UP,
  32. 'id' => $activityId
  33. ]);
  34. if (!$activity) throw new Exception('活动不存在', HttpStatus::CUSTOM_ERROR);
  35. ActivityUser::create([
  36. 'activity_id' => $activityId,
  37. 'title' => $activity->title,
  38. 'sub_title' => $activity->sub_title,
  39. 'start_time' => $activity->start_time,
  40. 'end_time' => $activity->end_time,
  41. 'detail_image' => $activity->detail_image,
  42. 'part_in_time' => $time,
  43. 'member_id' => $memberId,
  44. 'status' => 0
  45. ]);
  46. } catch (ValidationException $e) {
  47. return $this->error($e->validator->errors()->first());
  48. } catch (Exception $e) {
  49. return $this->error($e->getMessage());
  50. }
  51. return $this->success($activity);
  52. }
  53. public function index(): JsonResponse
  54. {
  55. try {
  56. request()->validate(['member_id' => ['nullable', 'integer']]);
  57. $memberId = request()->input('member_id');
  58. $time = time();
  59. $query = ActivityRewardModel::where(ActivityRewardService::getWhere([
  60. 'start_time' => ['<=', $time],
  61. 'end_time' => ['>=', $time],
  62. 'status' => ActivityRewardModel::STATUS_UP
  63. ]));
  64. if (!empty($memberId)) {
  65. $query->with(['activityUser' => function ($query1) use ($memberId) {
  66. $query1->where('member_id', $memberId);
  67. }]);
  68. }
  69. $list = $query->orderByDesc('id')->get();
  70. $list->transform(function ($activity) use ($memberId) {
  71. if (!empty($memberId)) {
  72. $activity->is_claimed = $activity->activityUser->isNotEmpty();
  73. } else {
  74. $activity->is_claimed = false;
  75. }
  76. unset($activity->activityUser);
  77. return $activity;
  78. })->toArray();
  79. } catch (ValidationException $e) {
  80. return $this->error($e->validator->errors()->first());
  81. } catch (Exception $e) {
  82. return $this->error($e->getMessage());
  83. }
  84. return $this->success($list);
  85. }
  86. }