增加登录事件

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 catchAdmin\user\model\Users;
use think\facade\Db; use think\facade\Db;
class LoginEvent class LoginLogListener
{ {
protected $params; public function handle($params)
public function __construct(array $params)
{ {
$this->params = $params;
}
public function handle()
{
dd('ad');
$agent = request()->header('user-agent'); $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([ Db::name('login_log')->insert([
'login_name' => $username ? : $this->params['email'], 'login_name' => $username ? : $params['email'],
'login_ip' => ip2long(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' => $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)) { if (false !== stripos($agent, 'win') && preg_match('/nt 6.1/i', $agent)) {
return 'Windows 7'; return 'Windows 7';
@ -56,7 +52,13 @@ class LoginEvent
return '未知'; return '未知';
} }
private function getBrowser($agent) /**
*
* @time 2019年12月12日
* @param $agent
* @return string
*/
private function getBrowser($agent): string
{ {
if (false !== stripos($agent, "MSIE")) { if (false !== stripos($agent, "MSIE")) {
return 'MSIE'; return 'MSIE';

View File

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