validate 分离 controller
This commit is contained in:
parent
e957f9f474
commit
430e466e12
@ -3,7 +3,7 @@ namespace app\admin\controller;
|
||||
|
||||
use think\Collection;
|
||||
use think\permissions\facade\Permissions;
|
||||
use app\validates\PermissionValidate;
|
||||
use app\admin\request\PermissionRequest;
|
||||
use app\service\MenuService;
|
||||
|
||||
class Permission extends Base
|
||||
@ -20,13 +20,10 @@ class Permission extends Base
|
||||
* @time at 2018年11月13日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function create(PermissionValidate $validate, MenuService $menuService)
|
||||
public function create(PermissionRequest $request, MenuService $menuService)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
if ($request->isPost()) {
|
||||
$data = $request->post();
|
||||
Permissions::store($data) ? $this->success('添加成功', url('permission/index')) : $this->error('添加失败');
|
||||
}
|
||||
|
||||
@ -41,13 +38,10 @@ class Permission extends Base
|
||||
* @time at 2018年11月13日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function edit(PermissionValidate $validate, MenuService $menuService)
|
||||
public function edit(PermissionRequest $request, MenuService $menuService)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
if ($request->isPost()) {
|
||||
$data = $request->post();
|
||||
Permissions::updateBy($data['id'], $data) !== false ? $this->success('编辑成功', url('permission/index')) : $this->error('');
|
||||
}
|
||||
$permissionId = $this->request->param('id');
|
||||
|
@ -2,7 +2,7 @@
|
||||
namespace app\admin\controller;
|
||||
|
||||
use think\permissions\facade\Roles;
|
||||
use app\validates\RoleValidate;
|
||||
use app\admin\request\RoleRequest;
|
||||
use think\permissions\facade\Permissions;
|
||||
use app\service\MenuService;
|
||||
|
||||
@ -20,14 +20,10 @@ class Role extends Base
|
||||
* @time at 2018年11月13日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function create(RoleValidate $validate)
|
||||
public function create(RoleRequest $request)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
Roles::store($data) ? $this->success('创建成功', url('role/index')) : $this->error('创建失败');
|
||||
if ($request->isPost()) {
|
||||
Roles::store($request->post()) ? $this->success('创建成功', url('role/index')) : $this->error('创建失败');
|
||||
}
|
||||
return $this->fetch();
|
||||
}
|
||||
@ -38,14 +34,10 @@ class Role extends Base
|
||||
* @time at 2018年11月13日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function edit(RoleValidate $validate)
|
||||
public function edit(RoleRequest $request)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
Roles::updateBy($data['id'], $data) !== false ? $this->success('编辑成功', url('role/index')) : $this->error('编辑失败');
|
||||
Roles::updateBy($request->post('id'), $request->post()) !== false ? $this->success('编辑成功', url('role/index')) : $this->error('编辑失败');
|
||||
}
|
||||
|
||||
$this->role = Roles::getRoleBy($this->request->param('id'));
|
||||
|
@ -34,6 +34,7 @@ class User extends Base
|
||||
if ($request->isPost()) {
|
||||
$data = $request->post();
|
||||
$data['password'] = generatePassword($data['password']);
|
||||
|
||||
if ($userId = $userModel->store($data)) {
|
||||
// 分配角色
|
||||
$this->giveRoles($userModel, $userId, $data);
|
||||
|
@ -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
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
$result = [
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => $msg,
|
||||
'data' => $data,
|
||||
'url' => $url,
|
||||
'wait' => $wait,
|
||||
];
|
||||
|
||||
if ('html' == strtolower($type)) {
|
||||
$type = 'jump';
|
||||
])->send();
|
||||
}
|
||||
|
||||
$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/PermissionRequest.php
Normal file
17
application/admin/request/PermissionRequest.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\PermissionValidate;
|
||||
|
||||
class PermissionRequest extends FormRequest
|
||||
{
|
||||
public function validate()
|
||||
{
|
||||
return (new PermissionValidate())->getErrors($this->post());
|
||||
}
|
||||
}
|
17
application/admin/request/RoleRequest.php
Normal file
17
application/admin/request/RoleRequest.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\RoleValidate;
|
||||
|
||||
class RoleRequest extends FormRequest
|
||||
{
|
||||
public function validate()
|
||||
{
|
||||
return (new RoleValidate())->getErrors($this->post());
|
||||
}
|
||||
}
|
@ -25,4 +25,11 @@ abstract class AbstractValidate extends Validate
|
||||
|
||||
return $this->getError();
|
||||
}
|
||||
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
// TODO: Implement __set() method.
|
||||
$this->rule[$name] = $value;
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ return [
|
||||
// 数据库编码默认采用utf8
|
||||
'charset' => 'utf8',
|
||||
// 数据库表前缀
|
||||
'prefix' => '',
|
||||
'prefix' => 'cms_',
|
||||
// 数据库调试模式
|
||||
'debug' => true,
|
||||
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
|
||||
|
@ -25,7 +25,7 @@
|
||||
<div class="form-group">
|
||||
<div class="col-sm-4 col-sm-offset-2">
|
||||
<button class="btn btn-primary" type="submit">保存</button>
|
||||
<button class="btn btn-white" onclick="history.back()">返回</button>
|
||||
<span class="btn btn-white" onclick="history.back()">返回</span>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
Loading…
x
Reference in New Issue
Block a user