123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- declare(strict_types=1);
- namespace catcher\base;
- use app\Request;
- use catcher\exceptions\FailedException;
- use catcher\exceptions\ValidateFailedException;
- use think\App;
- class CatchRequest extends Request
- {
-
- protected $needCreatorId = true;
-
- protected $batch = false;
-
- public function __construct()
- {
- parent::__construct();
- $this->validate();
- }
-
- protected function validate()
- {
- if (method_exists($this, 'rules')) {
- try {
- $validate = app('validate');
-
- if ($this->batch) {
- $validate->batch($this->batch);
- }
-
- $message = [];
- if (method_exists($this, 'message')) {
- $message = $this->message();
- }
- if (!$validate->message(empty($message) ? [] : $message)->check(request()->param(), $this->rules())) {
- throw new FailedException($validate->getError());
- }
- } catch (\Exception $e) {
- throw new ValidateFailedException($e->getMessage());
- }
- }
-
- if ($this->needCreatorId) {
- $this->param['creator_id'] = $this->user()->id;
- }
- return true;
- }
-
- public function post($name = '', $default = null, $filter = '')
- {
- if ($this->needCreatorId) {
- $this->post['creator_id'] = $this->user()->id;
- }
- return parent::post($name, $default, $filter);
- }
- }
|