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)
|
- [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)
|
- [thans/tp-jwt-auth](https://packagist.org/packages/thans/tp-jwt-auth)
|
||||||
- [workerman/workerman](https://github.com/walkor/Workerman)
|
- [workerman/workerman](https://github.com/walkor/Workerman)
|
||||||
- [jaguarjack/think-filesystem-cloud](https://github.com/yanwenwu/think-filesystem-cloud)
|
- [jaguarjack/think-filesystem-cloud](https://github.com/yanwenwu/think-filesystem-cloud)
|
||||||
|
@ -11,15 +11,13 @@ class LoginLogEvent
|
|||||||
{
|
{
|
||||||
$agent = request()->header('user-agent');
|
$agent = request()->header('user-agent');
|
||||||
|
|
||||||
$username = Users::where('email', $params['email'])->value('username');
|
|
||||||
|
|
||||||
app(LoginLog::class)->storeBy([
|
app(LoginLog::class)->storeBy([
|
||||||
'login_name' => $username ? : $params['email'],
|
'login_name' => $params['login_name'],
|
||||||
'login_ip' => request()->ip(),
|
'login_ip' => request()->ip(),
|
||||||
'browser' => $this->getBrowser($agent),
|
'browser' => $this->getBrowser($agent),
|
||||||
'os' => $this->getOs($agent),
|
'os' => $this->getOs($agent),
|
||||||
'login_at' => time(),
|
'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)
|
public function login(LoginRequest $request, CatchAuth $auth)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$params = $request->param();
|
$params = $request->param();
|
||||||
|
|
||||||
$token = $auth->attempt($params);
|
$token = $auth->attempt($params);
|
||||||
|
return CatchResponse::success([
|
||||||
$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([
|
|
||||||
'token' => $token,
|
'token' => $token,
|
||||||
], '登录成功') : CatchResponse::success('', '登录失败');
|
], '登录成功');
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
return CatchResponse::fail('登录失败', $exception->getCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace catcher;
|
namespace catcher;
|
||||||
|
|
||||||
|
use catchAdmin\permissions\model\Users;
|
||||||
use catcher\exceptions\FailedException;
|
use catcher\exceptions\FailedException;
|
||||||
use catcher\exceptions\LoginFailedException;
|
use catcher\exceptions\LoginFailedException;
|
||||||
use thans\jwt\facade\JWTAuth;
|
use thans\jwt\facade\JWTAuth;
|
||||||
use think\facade\Session;
|
use think\facade\Session;
|
||||||
|
use think\helper\Str;
|
||||||
|
|
||||||
class CatchAuth
|
class CatchAuth
|
||||||
{
|
{
|
||||||
@ -49,22 +51,70 @@ class CatchAuth
|
|||||||
*/
|
*/
|
||||||
public function attempt($condition)
|
public function attempt($condition)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$user = $this->authenticate($condition);
|
$user = $this->authenticate($condition);
|
||||||
|
|
||||||
if (!$user) {
|
if (!$user) {
|
||||||
throw new LoginFailedException();
|
throw new LoginFailedException();
|
||||||
}
|
}
|
||||||
|
if ($user->status == Users::DISABLE) {
|
||||||
if (!password_verify($condition['password'], $user->password)) {
|
throw new LoginFailedException('该用户已被禁用|' . $user->username, Code::USER_FORBIDDEN);
|
||||||
throw new LoginFailedException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function user()
|
public function user()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user