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

@@ -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)
{
$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('', '登录失败');
}
/**