实验性分离 Validate 和 Request
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
namespace app\admin\controller;
|
||||
|
||||
use think\Exception;
|
||||
use think\permissions\facade\Roles;
|
||||
use app\validates\RoleValidate;
|
||||
use think\permissions\facade\Permissions;
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\model\UserModel;
|
||||
use app\validates\UserValidate;
|
||||
use app\admin\request\UserRequest;
|
||||
use think\permissions\facade\Roles;
|
||||
|
||||
class User extends Base
|
||||
@@ -29,13 +29,10 @@ class User extends Base
|
||||
* @time at 2018年11月12日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function create(UserModel $userModel, UserValidate $validate)
|
||||
public function create(UserModel $userModel, UserRequest $request)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
if ($request->isPost()) {
|
||||
$data = $request->post();
|
||||
$data['password'] = generatePassword($data['password']);
|
||||
if ($userId = $userModel->store($data)) {
|
||||
// 分配角色
|
||||
@@ -55,13 +52,10 @@ class User extends Base
|
||||
* @time at 2018年11月12日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function edit(UserModel $userModel, UserValidate $validate)
|
||||
public function edit(UserModel $userModel, UserRequest $request)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
if ($request->isPost()) {
|
||||
$data = $request->post();
|
||||
$this->giveRoles($userModel, $data['id'], $data);
|
||||
$data['password'] = generatePassword($data['password']);
|
||||
$userModel->updateBy($data['id'], $data) ? $this->success('修改成功', url('user/index')) : $this->error('修改失败');
|
||||
|
67
application/admin/request/FormRequest.php
Normal file
67
application/admin/request/FormRequest.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* UserRequest.php
|
||||
* Created by wuyanwen <wuyanwen1992@gmail.com>
|
||||
* Date: 2018/11/29 0029 21:56
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$err = $this->validate();
|
||||
|
||||
return $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);
|
||||
}
|
||||
|
||||
$result = [
|
||||
'code' => 0,
|
||||
'msg' => $msg,
|
||||
'data' => $data,
|
||||
'url' => $url,
|
||||
'wait' => $wait,
|
||||
];
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
17
application/admin/request/UserRequest.php
Normal file
17
application/admin/request/UserRequest.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* UserRequest.php
|
||||
* Created by wuyanwen <wuyanwen1992@gmail.com>
|
||||
* Date: 2018/11/29 0029 21:56
|
||||
*/
|
||||
namespace app\admin\request;
|
||||
|
||||
use app\admin\validates\UserValidate;
|
||||
|
||||
class UserRequest extends FormRequest
|
||||
{
|
||||
public function validate()
|
||||
{
|
||||
return (new UserValidate())->getErrors($this->post());
|
||||
}
|
||||
}
|
@@ -5,7 +5,7 @@
|
||||
* Date: 2018/11/12 0012
|
||||
* Time: 下午 16:31
|
||||
*/
|
||||
namespace app\validates;
|
||||
namespace app\admin\validates;;
|
||||
|
||||
use think\Validate;
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* Time: 下午 18:21
|
||||
*/
|
||||
|
||||
namespace app\validates;
|
||||
namespace app\admin\validates;
|
||||
|
||||
class PermissionValidate extends AbstractValidate
|
||||
{
|
@@ -5,7 +5,7 @@
|
||||
* Date: 2018/11/14 0014
|
||||
* Time: 下午 17:42
|
||||
*/
|
||||
namespace app\validates;
|
||||
namespace app\admin\validates;
|
||||
|
||||
class RoleValidate extends AbstractValidate
|
||||
{
|
@@ -6,7 +6,7 @@
|
||||
* Time: 下午 16:38
|
||||
*/
|
||||
|
||||
namespace app\validates;
|
||||
namespace app\admin\validates;
|
||||
|
||||
class UserValidate extends AbstractValidate
|
||||
{
|
Reference in New Issue
Block a user