ApiRequest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Http\Exceptions\HttpResponseException;
  6. use Illuminate\Contracts\Validation\Validator;
  7. class ApiRequest extends FormRequest
  8. {
  9. public $scenes = [];
  10. public $currentScene; //当前场景
  11. public $autoValidate = false; //是否注入之后自动验证
  12. public function authorize()
  13. {
  14. return true;
  15. }
  16. /**
  17. * 设置场景
  18. * @param $scene
  19. * @return $this
  20. */
  21. public function scene($scene)
  22. {
  23. $this->currentScene = $scene;
  24. return $this;
  25. }
  26. /**
  27. * 覆盖自动验证方法
  28. */
  29. public function validateResolved()
  30. {
  31. if ($this->autoValidate) {
  32. $this->handleValidate();
  33. }
  34. }
  35. /**
  36. * 验证方法
  37. * @param string $scene
  38. * @throws \Illuminate\Auth\Access\AuthorizationException
  39. * @throws \Illuminate\Validation\ValidationException
  40. */
  41. public function validate($scene = '')
  42. {
  43. if ($scene) {
  44. $this->currentScene = $scene;
  45. }
  46. $this->handleValidate();
  47. }
  48. /**
  49. * 根据场景获取规则
  50. * @return array|mixed
  51. */
  52. public function getRules()
  53. {
  54. $rules = $this->container->call([$this, 'rules']);
  55. $newRules = [];
  56. if ($this->currentScene && isset($this->scenes[$this->currentScene])) {
  57. $sceneFields = is_array($this->scenes[$this->currentScene])
  58. ? $this->scenes[$this->currentScene] : explode(',', $this->scenes[$this->currentScene]);
  59. foreach ($sceneFields as $field) {
  60. if (array_key_exists($field, $rules)) {
  61. $newRules[$field] = $rules[$field];
  62. }
  63. }
  64. return $newRules;
  65. }
  66. return $rules;
  67. }
  68. /**
  69. * 覆盖设置 自定义验证器
  70. * @param $factory
  71. * @return mixed
  72. */
  73. public function validator($factory)
  74. {
  75. return $factory->make(
  76. $this->validationData(), $this->getRules(),
  77. $this->messages(), $this->attributes()
  78. );
  79. }
  80. /**
  81. * 最终验证方法
  82. * @throws \Illuminate\Auth\Access\AuthorizationException
  83. * @throws \Illuminate\Validation\ValidationException
  84. */
  85. protected function handleValidate()
  86. {
  87. $instance = $this->getValidatorInstance();
  88. if ($instance->fails()) {
  89. $this->failedValidation($instance);
  90. }
  91. $this->passedValidation();
  92. }
  93. /**
  94. * 重写报错部分-适应API JSON下发的需求
  95. */
  96. protected function failedValidation(Validator $validator)
  97. {
  98. $error= $validator->errors()->all();
  99. throw new HttpResponseException(response()->json(['status' => false,
  100. 'code' => 422,
  101. 'message' => $error[0],
  102. 'data' => []]));
  103. }
  104. }