diff --git a/application/admin/controller/Role.php b/application/admin/controller/Role.php index 4f5071c..339f00e 100644 --- a/application/admin/controller/Role.php +++ b/application/admin/controller/Role.php @@ -1,7 +1,6 @@ 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('修改失败'); diff --git a/application/admin/request/FormRequest.php b/application/admin/request/FormRequest.php new file mode 100644 index 0000000..a6ae732 --- /dev/null +++ b/application/admin/request/FormRequest.php @@ -0,0 +1,67 @@ + + * 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'); + } +} \ No newline at end of file diff --git a/application/admin/request/UserRequest.php b/application/admin/request/UserRequest.php new file mode 100644 index 0000000..ece3885 --- /dev/null +++ b/application/admin/request/UserRequest.php @@ -0,0 +1,17 @@ + + * 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()); + } +} \ No newline at end of file diff --git a/application/validates/AbstractValidate.php b/application/admin/validates/AbstractValidate.php similarity index 92% rename from application/validates/AbstractValidate.php rename to application/admin/validates/AbstractValidate.php index b5478e1..f1f0b35 100644 --- a/application/validates/AbstractValidate.php +++ b/application/admin/validates/AbstractValidate.php @@ -5,7 +5,7 @@ * Date: 2018/11/12 0012 * Time: 下午 16:31 */ -namespace app\validates; +namespace app\admin\validates;; use think\Validate; diff --git a/application/validates/PermissionValidate.php b/application/admin/validates/PermissionValidate.php similarity index 93% rename from application/validates/PermissionValidate.php rename to application/admin/validates/PermissionValidate.php index 7e44771..f3359f3 100644 --- a/application/validates/PermissionValidate.php +++ b/application/admin/validates/PermissionValidate.php @@ -6,7 +6,7 @@ * Time: 下午 18:21 */ -namespace app\validates; +namespace app\admin\validates; class PermissionValidate extends AbstractValidate { diff --git a/application/validates/RoleValidate.php b/application/admin/validates/RoleValidate.php similarity index 88% rename from application/validates/RoleValidate.php rename to application/admin/validates/RoleValidate.php index a14069d..d06240a 100644 --- a/application/validates/RoleValidate.php +++ b/application/admin/validates/RoleValidate.php @@ -5,7 +5,7 @@ * Date: 2018/11/14 0014 * Time: 下午 17:42 */ -namespace app\validates; +namespace app\admin\validates; class RoleValidate extends AbstractValidate { diff --git a/application/validates/UserValidate.php b/application/admin/validates/UserValidate.php similarity index 91% rename from application/validates/UserValidate.php rename to application/admin/validates/UserValidate.php index 51be644..59d9245 100644 --- a/application/validates/UserValidate.php +++ b/application/admin/validates/UserValidate.php @@ -6,7 +6,7 @@ * Time: 下午 16:38 */ -namespace app\validates; +namespace app\admin\validates; class UserValidate extends AbstractValidate { diff --git a/composer.lock b/composer.lock index 7f90f1c..665f59d 100644 --- a/composer.lock +++ b/composer.lock @@ -18,7 +18,13 @@ "type": "zip", "url": "https://api.github.com/repos/top-think/framework/zipball/f1d8ee3a91e8f504507edb5dcc49c50c47b4500f", "reference": "f1d8ee3a91e8f504507edb5dcc49c50c47b4500f", - "shasum": "" + "shasum": "", + "mirrors": [ + { + "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "preferred": true + } + ] }, "require": { "php": ">=5.6.0", @@ -69,7 +75,13 @@ "type": "zip", "url": "https://api.github.com/repos/top-think/think-captcha/zipball/54c8a51552f99ff9ea89ea9c272383a8f738ceee", "reference": "54c8a51552f99ff9ea89ea9c272383a8f738ceee", - "shasum": "" + "shasum": "", + "mirrors": [ + { + "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "preferred": true + } + ] }, "require": { "topthink/framework": "5.1.*" @@ -108,7 +120,13 @@ "type": "zip", "url": "https://api.github.com/repos/top-think/think-installer/zipball/f5400a12c60e513911aef41fe443fa6920952675", "reference": "f5400a12c60e513911aef41fe443fa6920952675", - "shasum": "" + "shasum": "", + "mirrors": [ + { + "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "preferred": true + } + ] }, "require": { "composer-plugin-api": "^1.0" @@ -149,7 +167,13 @@ "type": "zip", "url": "https://api.github.com/repos/top-think/think-migration/zipball/70c89850ca29c2eab988c7c3475d1d5331901bb8", "reference": "70c89850ca29c2eab988c7c3475d1d5331901bb8", - "shasum": "" + "shasum": "", + "mirrors": [ + { + "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "preferred": true + } + ] }, "require": { "topthink/framework": "5.1.*" @@ -188,7 +212,13 @@ "type": "zip", "url": "https://api.github.com/repos/yanwenwu/thinkphp-permission/zipball/670caf6a98a476e769fb24002aeb5feb5f2a6e69", "reference": "670caf6a98a476e769fb24002aeb5feb5f2a6e69", - "shasum": "" + "shasum": "", + "mirrors": [ + { + "url": "https://dl.laravel-china.org/%package%/%reference%.%type%", + "preferred": true + } + ] }, "require": { "topthink/think-migration": "^2.0" diff --git a/config/database.php b/config/database.php index 24d2b02..0867594 100644 --- a/config/database.php +++ b/config/database.php @@ -13,13 +13,13 @@ return [ // 数据库类型 'type' => 'mysql', // 服务器地址 - 'hostname' => '', + 'hostname' => '127.0.0.1', // 数据库名 - 'database' => '', + 'database' => 'cms', // 用户名 - 'username' => '', + 'username' => 'root', // 密码 - 'password' => '', + 'password' => 'admin', // 端口 'hostport' => '3306', // 连接dsn