ActivityReward.php 7.6 KB

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