Promotion.php 1.7 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\PromotionAdminService;
  6. use App\Support\PromotionAmountFormatter;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Validation\ValidationException;
  9. class Promotion extends Controller
  10. {
  11. private function run(callable $callback)
  12. {
  13. try { return $this->success(PromotionAmountFormatter::response($callback())); }
  14. catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); }
  15. catch (\Throwable $e) { Log::error('promotion_admin_failed', ['exception' => $e]); return $this->error(HttpStatus::CUSTOM_ERROR, '独立活动服务暂不可用'); }
  16. }
  17. public function index(PromotionAdminService $s) { return $this->run(fn () => $s->listing(request()->all())); }
  18. public function options(PromotionAdminService $s) { return $this->run(fn () => $s->options()); }
  19. public function submit(PromotionAdminService $s) { return $this->run(fn () => $s->save(request()->all())); }
  20. public function records(PromotionAdminService $s) { return $this->run(fn () => $s->records(request()->all())); }
  21. public function detail(PromotionAdminService $s)
  22. {
  23. return $this->run(function () use ($s) { $p = request()->validate(['id' => ['required', 'integer', 'min:1']]); return $s->detail((int)$p['id']); });
  24. }
  25. public function status(PromotionAdminService $s)
  26. {
  27. return $this->run(function () use ($s) { $p = request()->validate(['id' => ['required', 'integer', 'min:1'], 'status' => ['required', 'integer', 'in:0,1']]); return $s->status((int)$p['id'], (int)$p['status']); });
  28. }
  29. }