ActivityReward.php 5.0 KB

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