From 763a05fa805566b345b7843958739b49d3520f13 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Wed, 9 Sep 2020 10:40:44 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- catch/login/LoginLogEvent.php | 6 +-- catch/login/controller/Index.php | 30 ++++-------- extend/catcher/CatchAuth.php | 78 ++++++++++++++++++++++++++------ 4 files changed, 75 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index b2e2cf0..5e7692a 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ composer create-project jaguarjack/catchadmin:dev-master > 排名部分先后 - [top-think/think](https://github.com/top-think/think) -- [ant-design-pro-vue](https://github.com/sendya/ant-design-pro-vue) +- [element-admin](https://panjiachen.gitee.io/vue-element-admin-site/zh/) - [thans/tp-jwt-auth](https://packagist.org/packages/thans/tp-jwt-auth) - [workerman/workerman](https://github.com/walkor/Workerman) - [jaguarjack/think-filesystem-cloud](https://github.com/yanwenwu/think-filesystem-cloud) diff --git a/catch/login/LoginLogEvent.php b/catch/login/LoginLogEvent.php index 77dea50..fc6d8dd 100644 --- a/catch/login/LoginLogEvent.php +++ b/catch/login/LoginLogEvent.php @@ -11,15 +11,13 @@ class LoginLogEvent { $agent = request()->header('user-agent'); - $username = Users::where('email', $params['email'])->value('username'); - app(LoginLog::class)->storeBy([ - 'login_name' => $username ? : $params['email'], + 'login_name' => $params['login_name'], 'login_ip' => request()->ip(), 'browser' => $this->getBrowser($agent), 'os' => $this->getOs($agent), 'login_at' => time(), - 'status' => $params['success'] ? 1 : 2, + 'status' => $params['success'], ]); } diff --git a/catch/login/controller/Index.php b/catch/login/controller/Index.php index 1859da0..ac29c39 100644 --- a/catch/login/controller/Index.php +++ b/catch/login/controller/Index.php @@ -22,29 +22,15 @@ class Index extends CatchController */ public function login(LoginRequest $request, CatchAuth $auth) { - $params = $request->param(); - - $token = $auth->attempt($params); - - $user = $auth->user(); - - if ($user->status == Users::DISABLE) { - throw new LoginFailedException('该用户已被禁用', Code::USER_FORBIDDEN); + try { + $params = $request->param(); + $token = $auth->attempt($params); + return CatchResponse::success([ + 'token' => $token, + ], '登录成功'); + } catch (\Exception $exception) { + return CatchResponse::fail('登录失败', $exception->getCode()); } - - // 记录用户登录 - $user->last_login_ip = request()->ip(); - $user->last_login_time = time(); - $user->save(); - - // 登录事件 - $params['success'] = $token; - - event('loginLog', $params); - - return $token ? CatchResponse::success([ - 'token' => $token, - ], '登录成功') : CatchResponse::success('', '登录失败'); } /** diff --git a/extend/catcher/CatchAuth.php b/extend/catcher/CatchAuth.php index 446c6f9..799b1de 100644 --- a/extend/catcher/CatchAuth.php +++ b/extend/catcher/CatchAuth.php @@ -1,10 +1,12 @@ authenticate($condition); + try { + $user = $this->authenticate($condition); - if (!$user) { - throw new LoginFailedException(); + if (!$user) { + throw new LoginFailedException(); + } + if ($user->status == Users::DISABLE) { + throw new LoginFailedException('该用户已被禁用|' . $user->username, Code::USER_FORBIDDEN); + } + + if (!password_verify($condition['password'], $user->password)) { + throw new LoginFailedException('登录失败|' . $user->username); + } + + $token = $this->{$this->getDriver()}($user); + $this->afterLoginSuccess($user); + // 登录事件 + $this->loginEvent($user->username); + return $token; + } catch (\Exception $exception) { + $message = $exception->getMessage(); + if (strpos($message, '|') !== false) { + $username = explode('|', $message)[1]; + } else { + $username = $condition['email']; + } + $this->loginEvent($username, false); + throw new LoginFailedException('登录失败', $exception->getCode()); } - - if (!password_verify($condition['password'], $user->password)) { - throw new LoginFailedException(); - } - - return $this->{$this->getDriver()}($user); } - /** - * - * @time 2020年01月07日 - * @return mixed - */ + /** + * 用户登录成功后 + * + * @time 2020年09月09日 + * @param $user + * @return void + */ + protected function afterLoginSuccess($user) + { + $user->last_login_ip = request()->ip(); + $user->last_login_time = time(); + $user->save(); + } + + /** + * 登录事件 + * + * @time 2020年09月09日 + * @param $name + * @param bool $success + * @return void + */ + protected function loginEvent($name, $success = true) + { + $params['login_name'] = $name; + $params['success'] = $success ? 1 : 2; + event('loginLog', $params); + } + + /** + * user + * + * @time 2020年09月09日 + * @return mixed + */ public function user() { $user = $this->user[$this->guard] ?? null;