From 9bcb7466bdf2d09de833cc35d83ca8d867361249 Mon Sep 17 00:00:00 2001 From: wuyanwen Date: Thu, 9 Jan 2020 08:22:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9F=BA=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/base/CatchController.php | 6 +- extend/catcher/base/CatchRequest.php | 96 +++++++++++-------- .../catcher/command/ModelGeneratorCommand.php | 6 +- 3 files changed, 63 insertions(+), 45 deletions(-) diff --git a/extend/catcher/base/CatchController.php b/extend/catcher/base/CatchController.php index 6f5a450..dcbebe9 100644 --- a/extend/catcher/base/CatchController.php +++ b/extend/catcher/base/CatchController.php @@ -7,10 +7,10 @@ use think\facade\View; abstract class CatchController { - public function __construct() + /**public function __construct() { $this->loadConfig(); - } + }*/ /** @@ -19,7 +19,7 @@ abstract class CatchController * @time 2019年12月15日 * @return void */ - protected function loadConfig() + protected function loadConfig(): void { $module = explode('\\', get_class($this))[1]; diff --git a/extend/catcher/base/CatchRequest.php b/extend/catcher/base/CatchRequest.php index 1efa78e..fbf6941 100644 --- a/extend/catcher/base/CatchRequest.php +++ b/extend/catcher/base/CatchRequest.php @@ -2,17 +2,20 @@ namespace catcher\base; use app\Request; +use catcher\CatchAuth; use catcher\exceptions\FailedException; use catcher\exceptions\ValidateFailedException; use think\Validate; class CatchRequest extends Request { - /** - * 批量验证 - * - * @var bool - */ + protected $auth; + + /** + * 批量验证 + * + * @var bool + */ protected $batch = false; @@ -36,7 +39,8 @@ class CatchRequest extends Request */ protected function validate() { - try { + if (method_exists($this, 'rules')) { + try { $validate = new Validate(); // 批量验证 if ($this->batch) { @@ -50,46 +54,60 @@ class CatchRequest extends Request } /** - // 场景设置验证 - if (property_exists($this, 'scene') && !empty($this->scene)) { - foreach ($this->scene as $scene => $rules) { - $validate->scene($scene); - // 只限制字段 - if (!isset($rules['only'])) { - $validate->only($rules); - } else { - $validate->only($rules['only']); - // 新增规则 - if (isset($rules['append'])) { - foreach ($rules['append'] as $field => $rule) { - $validate->append($field, $rule); - } - } - // 移除规则 - if (isset($rules['remove'])) { - foreach ($rules['remove'] as $field => $rule) { - $validate->remove($field, $rule); - } - } - } - - } - }**/ + * // 场景设置验证 + * if (property_exists($this, 'scene') && !empty($this->scene)) { + * foreach ($this->scene as $scene => $rules) { + * $validate->scene($scene); + * // 只限制字段 + * if (!isset($rules['only'])) { + * $validate->only($rules); + * } else { + * $validate->only($rules['only']); + * // 新增规则 + * if (isset($rules['append'])) { + * foreach ($rules['append'] as $field => $rule) { + * $validate->append($field, $rule); + * } + * } + * // 移除规则 + * if (isset($rules['remove'])) { + * foreach ($rules['remove'] as $field => $rule) { + * $validate->remove($field, $rule); + * } + * } + * } + * + * } + * }**/ // 验证 if (!$validate->check(request()->param(), $this->rules())) { - throw new FailedException($validate->getError()); + throw new FailedException($validate->getError()); } - - // 每次请求都需要设置创建人 - $this->param = array_merge($this->param, [ - 'creator_id' => $this->user()->id, - ]); - - } catch (\Exception $e) { + } catch (\Exception $e) { throw new ValidateFailedException($e->getMessage()); + } } + // 设置默认参数 + $this->param['creator_id'] = $this->user()->id; + return true; } + + + /** + * login user + * + * @time 2020年01月09日 + * @return mixed + */ + public function user() + { + if (!$this->auth) { + $this->auth = new CatchAuth; + } + + return $this->auth->user(); + } } diff --git a/extend/catcher/command/ModelGeneratorCommand.php b/extend/catcher/command/ModelGeneratorCommand.php index d6380bf..92e062c 100644 --- a/extend/catcher/command/ModelGeneratorCommand.php +++ b/extend/catcher/command/ModelGeneratorCommand.php @@ -78,11 +78,11 @@ class ModelGeneratorCommand extends Command { return <<