update:优化登录
This commit is contained in:
parent
8c153cce60
commit
763a05fa80
@ -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)
|
||||
|
@ -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'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -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('', '登录失败');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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,24 +51,72 @@ class CatchAuth
|
||||
*/
|
||||
public function attempt($condition)
|
||||
{
|
||||
$user = $this->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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user