Promotion.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\PromotionService;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Validation\ValidationException;
  8. class Promotion extends Controller
  9. {
  10. private function execute(string $action, PromotionService $service)
  11. {
  12. try {
  13. $rules = ['activity_id' => ['required', 'integer', 'min:1'], 'platform' => ['required', 'in:PC,H5,APP']];
  14. if ($action === 'spin') $rules['request_key'] = ['required', 'string', 'regex:/^[a-zA-Z0-9_-]{16,64}$/'];
  15. if ($action === 'claim') $rules['round_no'] = ['required', 'integer', 'min:1'];
  16. if (in_array($action, ['records', 'rewards'], true)) $rules += ['page' => ['sometimes', 'integer', 'min:1'], 'limit' => ['sometimes', 'integer', 'between:1,100']];
  17. $input = request()->validate($rules); $id = (int)$input['activity_id']; $userId = (int)request()->user->id;
  18. $data = match ($action) {
  19. 'detail' => $service->detail($id, $userId, $input['platform']),
  20. 'progress' => $service->progress($id, $userId, $input['platform']),
  21. 'apply' => $service->apply($id, $userId, $input['platform']),
  22. 'spin' => $service->spin($id, $userId, $input['request_key'], $input['platform']),
  23. 'claim' => $service->claim($id, $userId, (int)$input['round_no'], $input['platform']),
  24. 'records' => $service->records($id, $userId, (int)($input['page'] ?? 1), (int)($input['limit'] ?? 20)),
  25. 'rewards' => $service->rewards($id, $userId, (int)($input['page'] ?? 1), (int)($input['limit'] ?? 20)),
  26. 'broadcasts' => $service->broadcasts($id),
  27. };
  28. return $this->success($data);
  29. } catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); }
  30. catch (\Throwable $e) { Log::error('promotion_failed', ['action' => $action, 'exception' => $e]); return $this->error(HttpStatus::CUSTOM_ERROR, '活动服务暂不可用,请稍后重试'); }
  31. }
  32. public function detail(PromotionService $s) { return $this->execute('detail', $s); }
  33. public function heartbeat(PromotionService $s)
  34. {
  35. try {
  36. $p = request()->validate(['platform' => ['required', 'in:PC,H5,APP']]);
  37. return $this->success($s->heartbeat((int)request()->user->id, (string)request()->header('Authorization'), $p['platform']));
  38. } catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); }
  39. catch (\Throwable $e) { Log::error('promotion_heartbeat_failed', ['exception' => $e]); return $this->error(HttpStatus::CUSTOM_ERROR, '活动记录暂不可用'); }
  40. }
  41. public function index(PromotionService $s)
  42. {
  43. try {
  44. $p = request()->validate(['platform' => ['required', 'in:PC,H5,APP'], 'lang' => ['sometimes', 'string', 'max:10'], 'type' => ['sometimes', 'integer', 'in:7,8,9']]);
  45. return $this->success($s->listing((int)request()->user->id, $p['platform'], $p['lang'] ?? 'zh', isset($p['type']) ? (int)$p['type'] : null));
  46. } catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); }
  47. catch (\Throwable $e) { Log::error('promotion_list_failed', ['exception' => $e]); return $this->error(HttpStatus::CUSTOM_ERROR, '活动服务暂不可用'); }
  48. }
  49. public function progress(PromotionService $s) { return $this->execute('progress', $s); }
  50. public function apply(PromotionService $s) { return $this->execute('apply', $s); }
  51. public function spin(PromotionService $s) { return $this->execute('spin', $s); }
  52. public function claim(PromotionService $s) { return $this->execute('claim', $s); }
  53. public function records(PromotionService $s) { return $this->execute('records', $s); }
  54. public function rewards(PromotionService $s) { return $this->execute('rewards', $s); }
  55. public function broadcasts(PromotionService $s) { return $this->execute('broadcasts', $s); }
  56. }