Skip to content

Commit 9e8ce6c

Browse files
committed
fix(task): 兼容 swoole 执行的登录事件
1 parent 7a12616 commit 9e8ce6c

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

laravel/app/Events/LoginEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use App\Models\User;
1212
use Jenssegers\Agent\Agent;
1313

14-
class LoginEvent
14+
class LoginEvent extends \Hhxsv5\LaravelS\Swoole\Task\Event
1515
{
1616
use Dispatchable, InteractsWithSockets, SerializesModels;
1717

laravel/app/Http/Controllers/AuthController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use App\Events\LoginEvent;
99
use App\Http\Repository\UserRepository;
10+
use Hhxsv5\LaravelS\Swoole\Task\Event;
1011
use Jenssegers\Agent\Agent;
1112

1213
class AuthController extends Controller
@@ -52,6 +53,7 @@ public function login()
5253

5354
// 登录成功,触发事件
5455
event(new LoginEvent(auth('api')->user(), new Agent(), \Request::getClientIp(), time()));
56+
Event::fire(new LoginEvent(auth('api')->user(), new Agent(), \Request::getClientIp(), time()));
5557

5658
$data = [
5759
'access_token' => $token,

laravel/app/Listeners/LoginListener.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
namespace App\Listeners;
44

55
use App\Events\LoginEvent;
6+
use Hhxsv5\LaravelS\Swoole\Task\Event;
7+
use Hhxsv5\LaravelS\Swoole\Task\Listener;
68
use Illuminate\Contracts\Queue\ShouldQueue;
79
use Illuminate\Support\Facades\DB;
810
use Zhuzhichao\IpLocationZh\Ip;
911

10-
class LoginListener implements ShouldQueue
12+
class LoginListener extends Listener implements ShouldQueue
1113
{
14+
// 声明没有参数的构造函数
15+
public function __construct()
16+
{
17+
}
18+
1219
/**
1320
* 失败重试次数
1421
*
@@ -21,7 +28,7 @@ class LoginListener implements ShouldQueue
2128
*
2229
* @param LoginEvent $event
2330
*/
24-
public function handle(LoginEvent $event)
31+
public function handle(Event $event)
2532
{
2633
//获取事件中保存的信息
2734
$user = $event->getUser();

laravel/config/laravels.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,29 @@
2929
'processes' => [
3030
],
3131
'timer' => [
32-
'enable' => false,
32+
'enable' => false, // 开启定时任务类
3333
'jobs' => [
34+
// 启用LaravelScheduleJob来执行`php artisan schedule:run`,每分钟一次,替代Linux Crontab
35+
// \Hhxsv5\LaravelS\Illuminate\LaravelScheduleJob::class,
36+
// 两种配置参数的方式:
37+
// [\App\Jobs\Timer\TestCronJob::class, [1000, true]], // 注册时传入参数
38+
\App\Jobs\Timer\TestCronJob::class, // 重载对应的方法来返回参数
3439
// Enable LaravelScheduleJob to run `php artisan schedule:run` every 1 minute, replace Linux Crontab
3540
//\Hhxsv5\LaravelS\Illuminate\LaravelScheduleJob::class,
3641
// Two ways to configure parameters:
3742
// [\App\Jobs\XxxCronJob::class, [1000, true]], // Pass in parameters when registering
3843
// \App\Jobs\XxxCronJob::class, // Override the corresponding method to return the configuration
3944
],
45+
'max_wait_time' => 5, // Reload 时最大等待时间
4046
],
4147
// 绑定事件与监听器,一个事件可以有多个监听器,多个监听器按顺序执行
4248
'events' => [
4349
\App\Events\TestEvent::class => [
4450
\App\Listeners\TestListener::class,
4551
],
52+
// App\Events\LoginEvent::class => [
53+
// App\Listeners\LoginListener::class,
54+
// ],
4655
],
4756
'swoole_tables' => [
4857
],

0 commit comments

Comments
 (0)