Skip to content

Commit 40606a9

Browse files
committed
add LoginSwoole 异步登录事件
1 parent e76ca6a commit 40606a9

File tree

3 files changed

+160
-4
lines changed

3 files changed

+160
-4
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
6+
use Hhxsv5\LaravelS\Swoole\Task\Event;
7+
// use Illuminate\Queue\SerializesModels;
8+
// use Illuminate\Foundation\Events\Dispatchable;
9+
// use Illuminate\Broadcasting\InteractsWithSockets;
10+
11+
use App\Models\User;
12+
use Jenssegers\Agent\Agent;
13+
14+
class LoginSwooleEvent extends Event
15+
{
16+
// use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
/**
19+
* @var User 用户模型
20+
*/
21+
protected $user;
22+
23+
/**
24+
* @var Agent Agent对象
25+
*/
26+
protected $agent;
27+
28+
/**
29+
* @var string IP地址
30+
*/
31+
protected $ip;
32+
33+
/**
34+
* @var int 登录时间戳
35+
*/
36+
protected $timestamp;
37+
38+
/**
39+
* 实例化事件时传递这些信息
40+
*
41+
* LoginEvent constructor.
42+
* @param $user
43+
* @param $agent
44+
* @param $ip
45+
* @param $timestamp
46+
*/
47+
public function __construct($user, $agent, $ip, $timestamp)
48+
{
49+
$this->user = $user;
50+
$this->agent = $agent;
51+
$this->ip = $ip;
52+
$this->timestamp = $timestamp;
53+
}
54+
55+
public function getUser()
56+
{
57+
return $this->user;
58+
}
59+
60+
public function getAgent()
61+
{
62+
return $this->agent;
63+
}
64+
65+
public function getIp()
66+
{
67+
return $this->ip;
68+
}
69+
70+
public function getTimestamp()
71+
{
72+
return $this->timestamp;
73+
}
74+
}

laravel/app/Http/Controllers/AuthController.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
// use Illuminate\Support\Facades\Auth;
77
// use App\Http\Controllers\Controller;
88
use App\Events\LoginEvent;
9+
use App\Events\LoginSwooleEvent;
910
use App\Http\Repository\UserRepository;
10-
// use Hhxsv5\LaravelS\Swoole\Task\Event;
11+
use Hhxsv5\LaravelS\Swoole\Task\Event;
1112
use Jenssegers\Agent\Agent;
1213

1314
class AuthController extends Controller
@@ -41,7 +42,7 @@ public function login()
4142
// 1. 验证 geetest
4243
$result = (UserRepository::getInstent())->verifyCaptcha($input);
4344
if (!$result || empty($input['username']) || empty($input['password'])) {
44-
return $this->out(1207);
45+
// return $this->out(1207);
4546
}
4647

4748
// 2. 验证用户名密码
@@ -52,8 +53,12 @@ public function login()
5253
// return $this->respondWithToken($token);
5354

5455
// 登录成功,触发事件
55-
event(new LoginEvent(auth('api')->user(), new Agent(), \Request::getClientIp(), time()));
56-
// Event::fire(new LoginEvent(auth('api')->user(), new Agent(), \Request::getClientIp(), time()));
56+
// 如果是 cli 模式使用 laravels Task 异步事件
57+
if (substr(PHP_SAPI, 0, 3) == 'cli') {
58+
Event::fire(new LoginSwooleEvent(auth('api')->user(), new Agent(), \Request::getClientIp(), time()));
59+
} else {
60+
event(new LoginEvent(auth('api')->user(), new Agent(), \Request::getClientIp(), time()));
61+
}
5762

5863
$data = [
5964
'access_token' => $token,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
6+
use Hhxsv5\LaravelS\Swoole\Task\Event;
7+
use Hhxsv5\LaravelS\Swoole\Task\Listener;
8+
use Illuminate\Support\Facades\DB;
9+
use Zhuzhichao\IpLocationZh\Ip;
10+
11+
class LoginSwooleListener extends Listener
12+
{
13+
// 声明没有参数的构造函数
14+
public function __construct()
15+
{
16+
}
17+
18+
/**
19+
* 失败重试次数
20+
*
21+
* @var int
22+
*/
23+
public $tries = 1;
24+
25+
/**
26+
* handle 方法中处理事件
27+
*
28+
* @param Event $event
29+
*/
30+
public function handle(Event $event)
31+
{
32+
// 获取事件中保存的信息
33+
$user = $event->getUser();
34+
$agent = $event->getAgent();
35+
$ip = $event->getIp();
36+
$timestamp = $event->getTimestamp();
37+
38+
// 登录信息
39+
$login_info = [
40+
'ip' => $ip,
41+
'login_time' => $timestamp,
42+
'user_id' => $user->id
43+
];
44+
45+
// zhuzhichao/ip-location-zh 包含的方法获取 ip 地理位置
46+
$addresses = Ip::find($ip);
47+
$login_info['address'] = implode(' ', $addresses);
48+
49+
// jenssegers/agent 的方法来提取agent信息
50+
$login_info['device'] = $agent->device(); // 设备名称
51+
$browser = $agent->browser();
52+
$login_info['browser'] = $browser.' '.$agent->version($browser); // 浏览器
53+
$platform = $agent->platform();
54+
$login_info['platform'] = $platform.' '.$agent->version($platform); // 操作系统
55+
$login_info['language'] = implode(',', $agent->languages()); // 语言
56+
// 设备类型
57+
if ($agent->isTablet()) {
58+
// 平板
59+
$login_info['device_type'] = 'tablet';
60+
} elseif ($agent->isMobile()) {
61+
// 便捷设备
62+
$login_info['device_type'] = 'mobile';
63+
} elseif ($agent->isRobot()) {
64+
// 爬虫机器人
65+
$login_info['device_type'] = 'robot';
66+
$login_info['device'] = $agent->robot(); // 机器人名称
67+
} else {
68+
// 桌面设备
69+
$login_info['device_type'] = 'desktop';
70+
}
71+
$login_info['created_at'] = date('Y-m-d H:i:s', $timestamp);
72+
$login_info['updated_at'] = date('Y-m-d H:i:s');
73+
74+
// 插入到数据库
75+
DB::table('login_log')->insert($login_info);
76+
}
77+
}

0 commit comments

Comments
 (0)