validate 分离 controller

This commit is contained in:
yanwnewu
2018-11-30 09:56:27 +08:00
parent e957f9f474
commit 430e466e12
9 changed files with 74 additions and 75 deletions

View File

@@ -7,61 +7,32 @@
namespace app\admin\request;
use think\Request;
use think\Container;
use think\exception\HttpResponseException;
use think\Response;
use think\response\Redirect;
abstract class FormRequest extends Request
{
/**
* FormRequest constructor.
* @throws \think\Exception
*/
/**
* FormRequest constructor.
*/
public function __construct()
{
parent::__construct();
$err = $this->validate();
return $this->error($err);
if ($this->withServer($_SERVER)->isAjax(true) && $err = $this->validate()) {
exit($this->error($err));
}
}
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
$type = $this->getResponseType();
if (is_null($url)) {
$url = $this->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ('' !== $url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : url($url);
}
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
return json([
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
])->send();
}
if ('html' == strtolower($type)) {
$type = 'jump';
}
$response = Response::create($result, $type)->header($header)->options(['jump_template' => config('dispatch_error_tmpl')]);
throw new HttpResponseException($response);
}
/**
* 获取当前的response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
return !$this->isAjax()
? config('default_ajax_return')
: config('default_return_type');
}
}