2019-12-06 09:17:40 +08:00
|
|
|
<?php
|
|
|
|
namespace catcher\base;
|
|
|
|
|
|
|
|
use app\Request;
|
2019-12-15 13:38:25 +08:00
|
|
|
use catcher\exceptions\FailedException;
|
2019-12-06 09:17:40 +08:00
|
|
|
use catcher\exceptions\ValidateFailedException;
|
|
|
|
use think\Validate;
|
|
|
|
|
2019-12-13 17:26:54 +08:00
|
|
|
class CatchRequest extends Request
|
2019-12-06 09:17:40 +08:00
|
|
|
{
|
2020-01-09 22:20:57 +08:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $needCreatorId = true;
|
2020-01-09 08:22:38 +08:00
|
|
|
/**
|
|
|
|
* 批量验证
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
protected $batch = false;
|
|
|
|
|
|
|
|
|
2019-12-06 09:17:40 +08:00
|
|
|
/**
|
|
|
|
* Request constructor.
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 初始化验证
|
|
|
|
*
|
|
|
|
* @time 2019年11月27日
|
|
|
|
* @throws \Exception
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function validate()
|
|
|
|
{
|
2020-01-09 08:22:38 +08:00
|
|
|
if (method_exists($this, 'rules')) {
|
|
|
|
try {
|
2019-12-15 13:38:25 +08:00
|
|
|
$validate = new Validate();
|
2020-01-07 17:27:55 +08:00
|
|
|
// 批量验证
|
|
|
|
if ($this->batch) {
|
|
|
|
$validate->batch($this->batch);
|
|
|
|
}
|
2020-01-08 22:31:17 +08:00
|
|
|
|
2020-01-07 17:27:55 +08:00
|
|
|
/**
|
2020-01-09 08:22:38 +08:00
|
|
|
* // 场景设置验证
|
|
|
|
* 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);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
* }**/
|
2019-12-06 09:17:40 +08:00
|
|
|
|
2020-01-07 17:27:55 +08:00
|
|
|
// 验证
|
2019-12-15 13:38:25 +08:00
|
|
|
if (!$validate->check(request()->param(), $this->rules())) {
|
2020-01-09 08:22:38 +08:00
|
|
|
throw new FailedException($validate->getError());
|
2019-12-15 13:38:25 +08:00
|
|
|
}
|
2020-01-09 08:22:38 +08:00
|
|
|
} catch (\Exception $e) {
|
2019-12-15 13:38:25 +08:00
|
|
|
throw new ValidateFailedException($e->getMessage());
|
2020-01-09 08:22:38 +08:00
|
|
|
}
|
2019-12-06 09:17:40 +08:00
|
|
|
}
|
|
|
|
|
2020-01-09 08:22:38 +08:00
|
|
|
// 设置默认参数
|
2020-01-09 22:20:57 +08:00
|
|
|
if ($this->needCreatorId) {
|
|
|
|
$this->param['creator_id'] = $this->user()->id;
|
|
|
|
}
|
2020-01-09 08:22:38 +08:00
|
|
|
|
2019-12-06 09:17:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|