ActivityReward.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 App\Services\ActivityUserService;
  8. use App\Services\UserService;
  9. use Illuminate\Http\JsonResponse;
  10. use Illuminate\Support\Facades\Cache;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Validation\ValidationException;
  13. use Exception;
  14. class ActivityReward extends BaseController
  15. {
  16. //发送手机验证码
  17. public function sendPhoneCode()
  18. {
  19. try {
  20. $params = request()->validate([
  21. 'member_id' => ['required', 'integer'],
  22. 'phone' => ['required', 'string'],
  23. ]);
  24. $params['action'] = request()->action;
  25. $key = 'api_request_' . md5(json_encode($params));
  26. if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
  27. Cache::put($key, true, 60);
  28. } catch (ValidationException $e) {
  29. return $this->error($e->validator->errors()->first());
  30. } catch (Exception $e) {
  31. return $this->error($e->getMessage(), [], $e->getCode());
  32. }
  33. }
  34. public function participate(): JsonResponse
  35. {
  36. DB::beginTransaction();
  37. try {
  38. request()->validate([
  39. 'member_id' => ['required', 'integer', 'exists:users,member_id'],
  40. 'activity_id' => ['required', 'integer'],
  41. ]);
  42. $memberId = request()->input('member_id');
  43. $user = UserService::findOne(['member_id' => $memberId]);
  44. if (empty($user->phone)) throw new Exception('请先绑定手机号', HttpStatus::NOT_BIND_PHONE);
  45. if (ActivityUser::where('member_id', $memberId)->where('status', 0)->exists()) {
  46. throw new Exception('完成活动才可以参与新活动', HttpStatus::CUSTOM_ERROR);
  47. }
  48. $activityId = request()->input('activity_id');
  49. if (ActivityUser::where('member_id', $memberId)->where('activity_id', $activityId)->exists()) {
  50. throw new Exception('已参与过此活动,每个活动仅可参加一次', HttpStatus::CUSTOM_ERROR);
  51. }
  52. $time = time();
  53. $activity = ActivityRewardService::findOne([
  54. 'start_time' => ['<=', $time],
  55. 'end_time' => ['>=', $time],
  56. 'status' => ActivityRewardModel::STATUS_UP,
  57. 'id' => $activityId
  58. ]);
  59. if (!$activity) throw new Exception('活动不存在', HttpStatus::CUSTOM_ERROR);
  60. ActivityUserService::submit([
  61. 'activity_id' => $activityId,
  62. 'title' => $activity->title,
  63. 'sub_title' => $activity->sub_title,
  64. 'start_time' => $activity->start_time,
  65. 'end_time' => $activity->end_time,
  66. 'detail_image' => $activity->detail_image,
  67. 'part_in_time' => $time,
  68. 'member_id' => $memberId,
  69. 'status' => 0
  70. ]);
  71. DB::commit();
  72. } catch (ValidationException $e) {
  73. DB::rollBack();
  74. return $this->error($e->validator->errors()->first());
  75. } catch (Exception $e) {
  76. DB::rollBack();
  77. return $this->error($e->getMessage(), [], $e->getCode());
  78. }
  79. return $this->success();
  80. }
  81. public function index(): JsonResponse
  82. {
  83. try {
  84. request()->validate(['member_id' => ['nullable', 'integer']]);
  85. $memberId = request()->input('member_id');
  86. $time = time();
  87. $query = ActivityRewardModel::where(ActivityRewardService::getWhere([
  88. 'start_time' => ['<=', $time],
  89. 'end_time' => ['>=', $time],
  90. 'status' => ActivityRewardModel::STATUS_UP
  91. ]));
  92. if (!empty($memberId)) {
  93. $query->with(['activityUser' => function ($query1) use ($memberId) {
  94. $query1->where('member_id', $memberId);
  95. }]);
  96. }
  97. $list = $query->orderByDesc('id')->get();
  98. $list->transform(function ($activity) use ($memberId) {
  99. if (!empty($memberId)) {
  100. $activity->is_claimed = $activity->activityUser->isNotEmpty();
  101. } else {
  102. $activity->is_claimed = false;
  103. }
  104. unset($activity->activityUser);
  105. return $activity;
  106. })->toArray();
  107. } catch (ValidationException $e) {
  108. return $this->error($e->validator->errors()->first());
  109. } catch (Exception $e) {
  110. return $this->error($e->getMessage());
  111. }
  112. return $this->success($list);
  113. }
  114. }