RewardRules.php 1.2 KB

1234567891011121314151617181920212223242526272829
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\RewardRuleService;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Validation\ValidationException;
  8. use Throwable;
  9. class RewardRules extends Controller
  10. {
  11. public function rebate() { return $this->run(fn () => RewardRuleService::get('rebate')); }
  12. public function backflow() { return $this->run(fn () => RewardRuleService::get('backflow')); }
  13. public function saveRebate() { return $this->run(fn () => RewardRuleService::save('rebate', request()->all())); }
  14. public function saveBackflow() { return $this->run(fn () => RewardRuleService::save('backflow', request()->all())); }
  15. private function run(callable $callback)
  16. {
  17. try { return $this->success($callback()); }
  18. catch (ValidationException $e) {
  19. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  20. } catch (Throwable $e) {
  21. Log::error('reward_rules_failed', ['exception' => $e]);
  22. return $this->error(HttpStatus::CUSTOM_ERROR, '奖励规则暂不可用,请稍后重试');
  23. }
  24. }
  25. }