增加登录事件

This commit is contained in:
wuyanwen 2019-12-12 22:33:45 +08:00
parent bdbf812941
commit 02b973d0b5
2 changed files with 35 additions and 20 deletions

View File

@ -4,35 +4,31 @@ namespace catchAdmin\login;
use catchAdmin\user\model\Users;
use think\facade\Db;
class LoginEvent
class LoginLogListener
{
protected $params;
public function __construct(array $params)
public function handle($params)
{
$this->params = $params;
}
public function handle()
{
dd('ad');
$agent = request()->header('user-agent');
$username = Users::where('email', $this->params['email'])->value('username');
$username = Users::where('email', $params['email'])->value('username');
Db::name('login_log')->insert([
'login_name' => $username ? : $this->params['email'],
'login_ip' => ip2long(request()->ip()),
'login_name' => $username ? : $params['email'],
'login_ip' => request()->ip(),
'browser' => $this->getBrowser($agent),
'os' => $this->getOs($agent),
'login_at' => time(),
'status' => $this->params['success'] ? 1 : 2,
'status' => $params['success'] ? 1 : 2,
]);
}
private function getOs($agent)
/**
*
* @time 2019年12月12日
* @param $agent
* @return string
*/
private function getOs($agent): string
{
if (false !== stripos($agent, 'win') && preg_match('/nt 6.1/i', $agent)) {
return 'Windows 7';
@ -56,7 +52,13 @@ class LoginEvent
return '未知';
}
private function getBrowser($agent)
/**
*
* @time 2019年12月12日
* @param $agent
* @return string
*/
private function getBrowser($agent): string
{
if (false !== stripos($agent, "MSIE")) {
return 'MSIE';

View File

@ -1,11 +1,16 @@
<?php
namespace catchAdmin\login\controller;
use catchAdmin\login\LoginEvent;
use catchAdmin\login\LoginLogListener;
use catchAdmin\user\Auth;
use catchAdmin\login\request\LoginRequest;
use catchAdmin\user\model\Users;
use catcher\base\CatchController;
use catcher\CatchResponse;
use think\captcha\Captcha;
use think\Event;
use think\facade\Db;
class Index extends CatchController
{
@ -27,13 +32,21 @@ class Index extends CatchController
* @time 2019年11月28日
* @param LoginRequest $request
* @return bool|string
* @throws \catcher\exceptions\LoginFailedException
* @throws \cather\exceptions\LoginFailedException
* @throws \app\exceptions\LoginFailedException
*/
public function login(LoginRequest $request)
{
return Auth::login($request->param()) ?
CatchResponse::success('', '登录成功') : CatchResponse::success('', '登录失败');
$params = $request->param();
$isSucceed = Auth::login($params);
// 登录事件
$params['success'] = $isSucceed;
event('log', $params);
return $isSucceed ? CatchResponse::success('', '登录成功') :
CatchResponse::success('', '登录失败');
}
/**