ActivityReward.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Models\Config;
  7. use App\Services\ActivityRewardService;
  8. use App\Services\ActivityUserService;
  9. use App\Services\ConfigService;
  10. use App\Services\PhoneCodeService;
  11. use App\Services\SmsService;
  12. use App\Services\UserService;
  13. use Illuminate\Http\JsonResponse;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Validation\ValidationException;
  17. use Exception;
  18. class ActivityReward extends BaseController
  19. {
  20. //活动规则
  21. public function rule(): JsonResponse
  22. {
  23. try {
  24. $activityRole = ConfigService::getVal("activity_rule");
  25. } catch (Exception $e) {
  26. return $this->error($e->getMessage(), [], $e->getCode());
  27. }
  28. return $this->success($activityRole);
  29. }
  30. //验证手机号
  31. public function verifyPhone(): JsonResponse
  32. {
  33. DB::beginTransaction();
  34. try {
  35. $params = request()->validate([
  36. 'member_id' => ['required', 'integer'],
  37. 'phone' => ['required', 'string'],
  38. 'code' => ['required', 'string'],
  39. 'visitor_id' => ['required', 'string', 'min:1', 'max:32'],
  40. ]);
  41. PhoneCodeService::verify($params['phone'], $params['code'], $params['member_id'], $params['visitor_id']);
  42. DB::commit();
  43. } catch (ValidationException $e) {
  44. DB::rollBack();
  45. return $this->error($e->validator->errors()->first());
  46. } catch (Exception $e) {
  47. DB::rollBack();
  48. return $this->error($e->getMessage(), [], $e->getCode());
  49. }
  50. return $this->success();
  51. }
  52. //发送手机验证码
  53. public function sendPhoneCode(): JsonResponse
  54. {
  55. try {
  56. $params = request()->validate([
  57. 'phone' => ['required', 'string'],
  58. ]);
  59. $keys = [
  60. 'phone' => $params['phone'],
  61. 'ip' => request()->ip(),
  62. 'action' => request()->route()->getActionName(),
  63. ];
  64. $key = 'api_request_' . md5(json_encode($keys));
  65. if (!Cache::has($key)) {
  66. Cache::put($key, true, 60);
  67. $code = SmsService::sendPhoneCode($params['phone']);
  68. PhoneCodeService::add($params['phone'], $code);
  69. }
  70. } catch (ValidationException $e) {
  71. return $this->error($e->validator->errors()->first());
  72. } catch (Exception $e) {
  73. return $this->error($e->getMessage(), [], $e->getCode());
  74. }
  75. return $this->success();
  76. }
  77. //参与活动
  78. public function participate(): JsonResponse
  79. {
  80. DB::beginTransaction();
  81. try {
  82. request()->validate([
  83. 'member_id' => ['required', 'integer', 'exists:users,member_id'],
  84. 'activity_id' => ['required', 'integer'],
  85. ]);
  86. $memberId = request()->input('member_id');
  87. $user = UserService::findOne(['member_id' => $memberId]);
  88. if (empty($user->phone)) throw new Exception('请先绑定手机号', HttpStatus::NOT_BIND_PHONE);
  89. if (ActivityUser::where('member_id', $memberId)->where('status', 0)->exists()) {
  90. throw new Exception('您有正在进行的活动,不可同时参与多个活动', HttpStatus::CUSTOM_ERROR);
  91. }
  92. $activityId = request()->input('activity_id');
  93. if (ActivityUser::where('member_id', $memberId)->where('activity_id', $activityId)->exists()) {
  94. throw new Exception('已参与过此活动,每个活动仅可参加一次', HttpStatus::CUSTOM_ERROR);
  95. }
  96. $time = time();
  97. $activity = ActivityRewardService::findOne([
  98. // 'start_time' => ['<=', $time],
  99. // 'end_time' => ['>=', $time],
  100. 'status' => ActivityRewardModel::STATUS_UP,
  101. 'id' => $activityId
  102. ]);
  103. if (!$activity) throw new Exception('活动不存在', HttpStatus::CUSTOM_ERROR);
  104. if ($activity->start_time > $time) throw new Exception('活动未开始', HttpStatus::CUSTOM_ERROR);
  105. if ($activity->end_time < $time) throw new Exception('活动已结束', HttpStatus::CUSTOM_ERROR);
  106. ActivityUserService::submit([
  107. 'activity_id' => $activityId,
  108. 'title' => $activity->title,
  109. 'sub_title' => $activity->sub_title,
  110. 'start_time' => $activity->start_time,
  111. 'end_time' => $activity->end_time,
  112. 'detail_image' => $activity->detail_image,
  113. 'part_in_time' => $time,
  114. 'member_id' => $memberId,
  115. 'status' => 0
  116. ]);
  117. DB::commit();
  118. } catch (ValidationException $e) {
  119. DB::rollBack();
  120. return $this->error($e->validator->errors()->first());
  121. } catch (Exception $e) {
  122. DB::rollBack();
  123. return $this->error($e->getMessage(), [], $e->getCode());
  124. }
  125. return $this->success();
  126. }
  127. public function index(): JsonResponse
  128. {
  129. try {
  130. request()->validate(['member_id' => ['nullable', 'string']]);
  131. $memberId = request()->input('member_id');
  132. $time = time();
  133. $query = ActivityRewardModel::where(ActivityRewardService::getWhere([
  134. // 'start_time' => ['<=', $time],
  135. 'end_time' => ['>=', $time],
  136. 'status' => ActivityRewardModel::STATUS_UP
  137. ]));
  138. if (!empty($memberId)) {
  139. $query->with(['activityUser' => function ($query1) use ($memberId) {
  140. $query1->where('member_id', $memberId);
  141. }]);
  142. }
  143. $list = $query->orderByDesc('id')->get();
  144. $list->transform(function ($activity) use ($memberId) {
  145. if (!empty($memberId)) {
  146. $activity->is_claimed = $activity->activityUser->isNotEmpty();
  147. } else {
  148. $activity->is_claimed = false;
  149. }
  150. unset($activity->activityUser);
  151. return $activity;
  152. })->toArray();
  153. } catch (ValidationException $e) {
  154. return $this->error($e->validator->errors()->first());
  155. } catch (Exception $e) {
  156. return $this->error($e->getMessage());
  157. }
  158. return $this->success($list);
  159. }
  160. }