protected $listen = [ ..., //添加登录事件及对应监听器,一个事件可绑定多个监听器 'App\Events\LoginEvent' => [ 'App\Listeners\LoginListener', ], ];
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use App\Models\User;
use Jenssegers\Agent\Agent;
class LoginEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* @var User 用户模型
*/
protected $user;
/**
* @var Agent Agent对象
*/
protected $agent;
/**
* @var string IP地址
*/
protected $ip;
/**
* @var int 登录时间戳
*/
protected $timestamp;
/**
* 实例化事件时传递这些信息
*/
public function __construct($user, $agent, $ip, $timestamp)
{
$this->user = $user;
$this->agent = $agent;
$this->ip = $ip;
$this->timestamp = $timestamp;
}
public function getUser()
{
return $this->user;
}
public function getAgent()
{
return $this->agent;
}
public function getIp()
{
return $this->ip;
}
public function getTimestamp()
{
return $this->timestamp;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-default');
}
}
namespace App\Listeners;
use App\Events\LoginEvent;
class LoginListener
{
// handle方法中处理事件
public function handle(LoginEvent $event)
{
//获取事件中保存的信息
$user = $event->getUser();
$agent = $event->getAgent();
$ip = $event->getIp();
$timestamp = $event->getTimestamp();
//登录信息
$login_info = [
'ip' => $ip,
'login_time' => $timestamp,
'user_id' => $user->id
];
// zhuzhichao/ip-location-zh 包含的方法获取ip地理位置
$addresses = \Ip::find($ip);
$login_info['address'] = implode(' ', $addresses);
// jenssegers/agent 的方法来提取agent信息
$login_info['device'] = $agent->device(); //设备名称
$browser = $agent->browser();
$login_info['browser'] = $browser . ' ' . $agent->version($browser); //浏览器
$platform = $agent->platform();
$login_info['platform'] = $platform . ' ' . $agent->version($platform); //操作系统
$login_info['language'] = implode(',', $agent->languages()); //语言
//设备类型
if ($agent->isTablet()) {
// 平板
$login_info['device_type'] = 'tablet';
} else if ($agent->isMobile()) {
// 便捷设备
$login_info['device_type'] = 'mobile';
} else if ($agent->isRobot()) {
// 爬虫机器人
$login_info['device_type'] = 'robot';
$login_info['device'] = $agent->robot(); //机器人名称
} else {
// 桌面设备
$login_info['device_type'] = 'desktop';
}
//插入到数据库
DB::table('login_log')->insert($login_info);
}
}
namespace App\Controllers;
...
use App\Events\LoginEvent;
use Jenssegers\Agent\Agent;
class AuthControoler extends Controller
{
...
public function login(Request $request)
{
//登录实现
...
//登录成功,触发事件
event(new LoginEvent($this->guard()->user(), new Agent(), \Request::getClientIp(), time()));
...
}
}
namespace App\Listeners;
...
use Illuminate\Contracts\Queue\ShouldQueue;
class LoginListener implements ShouldQueue
{
/**
* 失败重试次数
* @var int
*/
public $tries = 1;
...
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有