修改基类

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
{
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];

View File

@@ -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();
}
}

View File

@@ -78,11 +78,11 @@ class ModelGeneratorCommand extends Command
{
return <<<EOT
<?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}';