RewardGrantSettings.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\RewardGrantSettingsService;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Validation\ValidationException;
  8. use Throwable;
  9. class RewardGrantSettings extends Controller
  10. {
  11. public function rebate() { return $this->read('rebate'); }
  12. public function backflow() { return $this->read('backflow'); }
  13. public function saveRebate() { return $this->write('rebate'); }
  14. public function saveBackflow() { return $this->write('backflow'); }
  15. private function read(string $type)
  16. {
  17. return $this->run(fn () => RewardGrantSettingsService::get($type));
  18. }
  19. private function write(string $type)
  20. {
  21. return $this->run(fn () => RewardGrantSettingsService::save($type, request()->all()));
  22. }
  23. private function run(callable $callback)
  24. {
  25. try { return $this->success($callback()); }
  26. catch (ValidationException $e) {
  27. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  28. } catch (Throwable $e) {
  29. Log::error('reward_grant_settings_failed', ['exception' => $e]);
  30. return $this->error(HttpStatus::CUSTOM_ERROR, '奖励发放设置暂不可用,请稍后重试');
  31. }
  32. }
  33. }