BaseRequest.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * 描述.
  4. *
  5. * @author nj <nj@gmail.com>
  6. * @date 2019-11-03 17:44:30
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Web\Requests;
  10. use Hyperf\Contract\ValidatorInterface;
  11. use App\Exception\ApiException;
  12. use Hyperf\Validation\Request\FormRequest;
  13. class BaseRequest extends FormRequest
  14. {
  15. /**
  16. * Determine if the user is authorized to make this request.
  17. *
  18. * @return bool
  19. */
  20. public function authorize()
  21. {
  22. // 默认放行auth校验,不通过其他方式鉴权.
  23. return true;
  24. }
  25. /**
  26. * Get the validation rules that apply to the request.
  27. *
  28. * @return array
  29. */
  30. public function rules()
  31. {
  32. return [
  33. ];
  34. }
  35. /**
  36. * 重定义校验失败
  37. * @param ValidatorInterface $validator
  38. * @throws ApiException
  39. */
  40. protected function failedValidation(ValidatorInterface $validator)
  41. {
  42. throw new ApiException($validator->messages()->first(), 500);
  43. }
  44. /**
  45. * 重定义认证失败
  46. * @throws ApiException
  47. */
  48. protected function failedAuthorization()
  49. {
  50. throw new ApiException(trans('common.err_failed_authorization'), 403);
  51. }
  52. }