update:优化登录

This commit is contained in:
JaguarJack 2020-09-09 10:40:44 +08:00
parent 8c153cce60
commit 763a05fa80
4 changed files with 75 additions and 41 deletions

View File

@ -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)

View File

@ -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'],
]);
}

View File

@ -22,29 +22,15 @@ class Index extends CatchController
*/
public function login(LoginRequest $request, CatchAuth $auth)
{
try {
$params = $request->param();
$token = $auth->attempt($params);
$user = $auth->user();
if ($user->status == Users::DISABLE) {
throw new LoginFailedException('该用户已被禁用', Code::USER_FORBIDDEN);
}
// 记录用户登录
$user->last_login_ip = request()->ip();
$user->last_login_time = time();
$user->save();
// 登录事件
$params['success'] = $token;
event('loginLog', $params);
return $token ? CatchResponse::success([
return CatchResponse::success([
'token' => $token,
], '登录成功') : CatchResponse::success('', '登录失败');
], '登录成功');
} catch (\Exception $exception) {
return CatchResponse::fail('登录失败', $exception->getCode());
}
}
/**

View File

@ -1,10 +1,12 @@
<?php
namespace catcher;
use catchAdmin\permissions\model\Users;
use catcher\exceptions\FailedException;
use catcher\exceptions\LoginFailedException;
use thans\jwt\facade\JWTAuth;
use think\facade\Session;
use think\helper\Str;
class CatchAuth
{
@ -49,22 +51,70 @@ class CatchAuth
*/
public function attempt($condition)
{
try {
$user = $this->authenticate($condition);
if (!$user) {
throw new LoginFailedException();
}
if (!password_verify($condition['password'], $user->password)) {
throw new LoginFailedException();
if ($user->status == Users::DISABLE) {
throw new LoginFailedException('该用户已被禁用|' . $user->username, Code::USER_FORBIDDEN);
}
return $this->{$this->getDriver()}($user);
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());
}
}
/**
* 用户登录成功后
*
* @time 2020年01月07日
* @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()