修改基类

This commit is contained in:
wuyanwen
2020-01-09 08:22:38 +08:00
parent 4fa1c9638e
commit 9bcb7466bd
3 changed files with 63 additions and 45 deletions

View File

@@ -7,10 +7,10 @@ use think\facade\View;
abstract class CatchController abstract class CatchController
{ {
public function __construct() /**public function __construct()
{ {
$this->loadConfig(); $this->loadConfig();
} }*/
/** /**
@@ -19,7 +19,7 @@ abstract class CatchController
* @time 2019年12月15日 * @time 2019年12月15日
* @return void * @return void
*/ */
protected function loadConfig() protected function loadConfig(): void
{ {
$module = explode('\\', get_class($this))[1]; $module = explode('\\', get_class($this))[1];

View File

@@ -2,17 +2,20 @@
namespace catcher\base; namespace catcher\base;
use app\Request; use app\Request;
use catcher\CatchAuth;
use catcher\exceptions\FailedException; use catcher\exceptions\FailedException;
use catcher\exceptions\ValidateFailedException; use catcher\exceptions\ValidateFailedException;
use think\Validate; use think\Validate;
class CatchRequest extends Request class CatchRequest extends Request
{ {
/** protected $auth;
* 批量验证
* /**
* @var bool * 批量验证
*/ *
* @var bool
*/
protected $batch = false; protected $batch = false;
@@ -36,7 +39,8 @@ class CatchRequest extends Request
*/ */
protected function validate() protected function validate()
{ {
try { if (method_exists($this, 'rules')) {
try {
$validate = new Validate(); $validate = new Validate();
// 批量验证 // 批量验证
if ($this->batch) { if ($this->batch) {
@@ -50,46 +54,60 @@ class CatchRequest extends Request
} }
/** /**
// 场景设置验证 * // 场景设置验证
if (property_exists($this, 'scene') && !empty($this->scene)) { * if (property_exists($this, 'scene') && !empty($this->scene)) {
foreach ($this->scene as $scene => $rules) { * foreach ($this->scene as $scene => $rules) {
$validate->scene($scene); * $validate->scene($scene);
// 只限制字段 * // 只限制字段
if (!isset($rules['only'])) { * if (!isset($rules['only'])) {
$validate->only($rules); * $validate->only($rules);
} else { * } else {
$validate->only($rules['only']); * $validate->only($rules['only']);
// 新增规则 * // 新增规则
if (isset($rules['append'])) { * if (isset($rules['append'])) {
foreach ($rules['append'] as $field => $rule) { * foreach ($rules['append'] as $field => $rule) {
$validate->append($field, $rule); * $validate->append($field, $rule);
} * }
} * }
// 移除规则 * // 移除规则
if (isset($rules['remove'])) { * if (isset($rules['remove'])) {
foreach ($rules['remove'] as $field => $rule) { * foreach ($rules['remove'] as $field => $rule) {
$validate->remove($field, $rule); * $validate->remove($field, $rule);
} * }
} * }
} * }
*
} * }
}**/ * }**/
// 验证 // 验证
if (!$validate->check(request()->param(), $this->rules())) { if (!$validate->check(request()->param(), $this->rules())) {
throw new FailedException($validate->getError()); throw new FailedException($validate->getError());
} }
} catch (\Exception $e) {
// 每次请求都需要设置创建人
$this->param = array_merge($this->param, [
'creator_id' => $this->user()->id,
]);
} catch (\Exception $e) {
throw new ValidateFailedException($e->getMessage()); throw new ValidateFailedException($e->getMessage());
}
} }
// 设置默认参数
$this->param['creator_id'] = $this->user()->id;
return true; return true;
} }
/**
* login user
*
* @time 2020年01月09日
* @return mixed
*/
public function user()
{
if (!$this->auth) {
$this->auth = new CatchAuth;
}
return $this->auth->user();
}
} }

View File

@@ -78,11 +78,11 @@ class ModelGeneratorCommand extends Command
{ {
return <<<EOT return <<<EOT
<?php <?php
namespace catch\{Module}\model; namespace catchAdmin\{Module}\model;
use cather\base\BaseModel; use catcher\base\CatchModel;
class {Class} extends BaseModel class {Class} extends CatchModel
{ {
protected \$name = '{Name}'; protected \$name = '{Name}';