From ceda3a224ac4422bf812ca7b1679c4a77bf71adb Mon Sep 17 00:00:00 2001 From: wuyanwen Date: Tue, 24 Dec 2019 18:21:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/user/controller/User.php | 20 +++--- catch/user/model/Users.php | 6 +- extend/catcher/CatchResponse.php | 108 +++++++++++++++---------------- 3 files changed, 66 insertions(+), 68 deletions(-) diff --git a/catch/user/controller/User.php b/catch/user/controller/User.php index cc86773..ecad715 100644 --- a/catch/user/controller/User.php +++ b/catch/user/controller/User.php @@ -50,7 +50,7 @@ class User extends CatchController * * @param CreateRequest $request * @time 2019年12月06日 - * @return Json + * @return \think\response\Json */ public function save(CreateRequest $request) { @@ -60,14 +60,14 @@ class User extends CatchController $this->user->attach($request->param('roleids')); } - return CatchResponse::success(); + return CatchResponse::success('', '添加成功'); } /** * * @time 2019年12月04日 * @param $id - * @return Json + * @return \think\response\Json */ public function read($id) { @@ -85,7 +85,7 @@ class User extends CatchController * @time 2019年12月04日 * @param $id * @param UpdateRequest $request - * @return Json + * @return \think\response\Json */ public function update($id, UpdateRequest $request) { @@ -106,7 +106,7 @@ class User extends CatchController * * @time 2019年12月04日 * @param $id - * @return Json + * @return \think\response\Json */ public function delete($id) { @@ -122,9 +122,9 @@ class User extends CatchController * * @time 2019年12月07日 * @param $id - * @return Json + * @return \think\response\Json */ - public function switchStatus($id): Json + public function switchStatus($id): \think\response\Json { $user = $this->user->findBy($id); return CatchResponse::success($this->user->updateBy($id, [ @@ -136,12 +136,12 @@ class User extends CatchController * * @time 2019年12月07日 * @param $id - * @return Json + * @return \think\response\Json * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\DataNotFoundException */ - public function recover($id): Json + public function recover($id): \think\response\Json { $trashedUser = $this->user->findBy($id, ['*'], true); @@ -176,4 +176,4 @@ class User extends CatchController 'hasRoles' => $roleIds, ]); } -} \ No newline at end of file +} diff --git a/catch/user/model/Users.php b/catch/user/model/Users.php index ce311e6..ef81a7c 100644 --- a/catch/user/model/Users.php +++ b/catch/user/model/Users.php @@ -56,7 +56,9 @@ class Users extends CatchModel }) ->when($search['status'] ?? false, function ($query) use ($search){ $query->where('status', $search['status']); - })->paginate($search['limit'] ?? $this->limit); + }) + ->order('id', 'desc') + ->paginate($search['limit'] ?? $this->limit); } /** @@ -80,4 +82,4 @@ class Users extends CatchModel return array_unique($permissionIds); } -} \ No newline at end of file +} diff --git a/extend/catcher/CatchResponse.php b/extend/catcher/CatchResponse.php index 1a9dad8..4344f41 100644 --- a/extend/catcher/CatchResponse.php +++ b/extend/catcher/CatchResponse.php @@ -1,67 +1,63 @@ $code, - 'msg' => $msg, - 'data' => $data, - ]); - } + /** + * 成功的响应 + * + * @time 2019年12月02日 + * @param array $data + * @param $msg + * @param int $code + * @return Json + */ + public static function success($data = [], $msg = 'success', $code = Code::SUCCESS): Json + { + return json([ + 'code' => $code, + 'message' => $msg, + 'data' => $data, + ]); + } - /** - * 分页 - * - * @time 2019年12月06日 - * @param Paginator $list - * @return \think\response\Json - */ - public static function paginate(Paginator $list): \think\response\Json - { - return json([ - 'code' => 10000, - 'msg' => 'success', - 'count' => $list->total(), - 'current' => $list->currentPage(), - 'limit' => $list->listRows(), - 'data' => $list->getCollection(), - ]); - } + /** + * 分页 + * + * @time 2019年12月06日 + * @param Paginator $list + * @return + */ + public static function paginate(Paginator $list) + { + return json([ + 'code' => 10000, + 'msg' => 'success', + 'count' => $list->total(), + 'current' => $list->currentPage(), + 'limit' => $list->listRows(), + 'data' => $list->getCollection(), + ]); + } - /** - * 错误的响应 - * - * @time 2019年12月02日 - * @param string $msg - * @param int $code - * @return mixed - * @throws \Exception - */ - public static function fail($msg = '', $code = 10001) - { - if (request()->isAjax()) { - return json([ - 'code' => $code, - 'msg' => $msg, - ]); - } - - return Response::create(config('catch.error'), 'view', $code)->assign(['msg' => $msg, 'code' => $code]); - } + /** + * 错误的响应 + * + * @time 2019年12月02日 + * @param string $msg + * @param int $code + * @return Json + */ + public static function fail($msg = '', $code = 10001): Json + { + return json([ + 'code' => $code, + 'msg' => $msg, + ]); + } }