ActivityReward.php 6.9 KB

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