| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace App\Http\Controllers\api;
- use App\Constants\HttpStatus;
- use App\Http\Controllers\Controller;
- use App\Services\PromotionService;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Validation\ValidationException;
- class Promotion extends Controller
- {
- private function execute(string $action, PromotionService $service)
- {
- try {
- $rules = ['activity_id' => ['required', 'integer', 'min:1'], 'platform' => ['required', 'in:PC,H5,APP']];
- if ($action === 'spin') $rules['request_key'] = ['required', 'string', 'regex:/^[a-zA-Z0-9_-]{16,64}$/'];
- if ($action === 'claim') $rules['round_no'] = ['required', 'integer', 'min:1'];
- if (in_array($action, ['records', 'rewards'], true)) $rules += ['page' => ['sometimes', 'integer', 'min:1'], 'limit' => ['sometimes', 'integer', 'between:1,100']];
- $input = request()->validate($rules); $id = (int)$input['activity_id']; $userId = (int)request()->user->id;
- $data = match ($action) {
- 'detail' => $service->detail($id, $userId, $input['platform']),
- 'progress' => $service->progress($id, $userId, $input['platform']),
- 'apply' => $service->apply($id, $userId, $input['platform']),
- 'spin' => $service->spin($id, $userId, $input['request_key'], $input['platform']),
- 'claim' => $service->claim($id, $userId, (int)$input['round_no'], $input['platform']),
- 'records' => $service->records($id, $userId, (int)($input['page'] ?? 1), (int)($input['limit'] ?? 20)),
- 'rewards' => $service->rewards($id, $userId, (int)($input['page'] ?? 1), (int)($input['limit'] ?? 20)),
- 'broadcasts' => $service->broadcasts($id),
- };
- return $this->success($data);
- } catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); }
- catch (\Throwable $e) { Log::error('promotion_failed', ['action' => $action, 'exception' => $e]); return $this->error(HttpStatus::CUSTOM_ERROR, '活动服务暂不可用,请稍后重试'); }
- }
- public function detail(PromotionService $s) { return $this->execute('detail', $s); }
- public function heartbeat(PromotionService $s)
- {
- try {
- $p = request()->validate(['platform' => ['required', 'in:PC,H5,APP']]);
- return $this->success($s->heartbeat((int)request()->user->id, (string)request()->header('Authorization'), $p['platform']));
- } catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); }
- catch (\Throwable $e) { Log::error('promotion_heartbeat_failed', ['exception' => $e]); return $this->error(HttpStatus::CUSTOM_ERROR, '活动记录暂不可用'); }
- }
- public function index(PromotionService $s)
- {
- try {
- $p = request()->validate(['platform' => ['required', 'in:PC,H5,APP'], 'lang' => ['sometimes', 'string', 'max:10'], 'type' => ['sometimes', 'integer', 'in:7,8,9']]);
- return $this->success($s->listing((int)request()->user->id, $p['platform'], $p['lang'] ?? 'zh', isset($p['type']) ? (int)$p['type'] : null));
- } catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); }
- catch (\Throwable $e) { Log::error('promotion_list_failed', ['exception' => $e]); return $this->error(HttpStatus::CUSTOM_ERROR, '活动服务暂不可用'); }
- }
- public function progress(PromotionService $s) { return $this->execute('progress', $s); }
- public function apply(PromotionService $s) { return $this->execute('apply', $s); }
- public function spin(PromotionService $s) { return $this->execute('spin', $s); }
- public function claim(PromotionService $s) { return $this->execute('claim', $s); }
- public function records(PromotionService $s) { return $this->execute('records', $s); }
- public function rewards(PromotionService $s) { return $this->execute('rewards', $s); }
- public function broadcasts(PromotionService $s) { return $this->execute('broadcasts', $s); }
- }
|