ActivityReward.php 6.8 KB

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