123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace App\Http\Requests;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Support\Str;
- use Illuminate\Http\Exceptions\HttpResponseException;
- use Illuminate\Contracts\Validation\Validator;
- class ApiRequest extends FormRequest
- {
- public $scenes = [];
- public $currentScene; //当前场景
- public $autoValidate = false; //是否注入之后自动验证
- public function authorize()
- {
- return true;
- }
- /**
- * 设置场景
- * @param $scene
- * @return $this
- */
- public function scene($scene)
- {
- $this->currentScene = $scene;
- return $this;
- }
- /**
- * 覆盖自动验证方法
- */
- public function validateResolved()
- {
- if ($this->autoValidate) {
- $this->handleValidate();
- }
- }
- /**
- * 验证方法
- * @param string $scene
- * @throws \Illuminate\Auth\Access\AuthorizationException
- * @throws \Illuminate\Validation\ValidationException
- */
- public function validate($scene = '')
- {
- if ($scene) {
- $this->currentScene = $scene;
- }
- $this->handleValidate();
- }
- /**
- * 根据场景获取规则
- * @return array|mixed
- */
- public function getRules()
- {
- $rules = $this->container->call([$this, 'rules']);
- $newRules = [];
- if ($this->currentScene && isset($this->scenes[$this->currentScene])) {
- $sceneFields = is_array($this->scenes[$this->currentScene])
- ? $this->scenes[$this->currentScene] : explode(',', $this->scenes[$this->currentScene]);
- foreach ($sceneFields as $field) {
- if (array_key_exists($field, $rules)) {
- $newRules[$field] = $rules[$field];
- }
- }
- return $newRules;
- }
- return $rules;
- }
- /**
- * 覆盖设置 自定义验证器
- * @param $factory
- * @return mixed
- */
- public function validator($factory)
- {
- return $factory->make(
- $this->validationData(), $this->getRules(),
- $this->messages(), $this->attributes()
- );
- }
- /**
- * 最终验证方法
- * @throws \Illuminate\Auth\Access\AuthorizationException
- * @throws \Illuminate\Validation\ValidationException
- */
- protected function handleValidate()
- {
- $instance = $this->getValidatorInstance();
- if ($instance->fails()) {
- $this->failedValidation($instance);
- }
- $this->passedValidation();
- }
- /**
- * 重写报错部分-适应API JSON下发的需求
- */
- protected function failedValidation(Validator $validator)
- {
- $error= $validator->errors()->all();
- throw new HttpResponseException(response()->json(['status' => false,
- 'code' => 422,
- 'message' => $error[0],
- 'data' => []]));
- }
- }
|