feat(notice): NoticeSended 事件与登录提醒内置监听

This commit is contained in:
augushong
2026-10-12 12:09:01 +00:00
parent 02f77f4eca
commit 7aba2d02fe
6 changed files with 233 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
<?php
namespace base\common\event\AdminLoginSuccess;
use app\admin\model\SystemAdmin;
use app\common\service\NoticeService;
use think\facade\Log;
class NoticeEventBase
{
public function handle(SystemAdmin $admin)
{
try {
// 登录提醒场景默认关闭:无配置行(null 强转 0)不发,DB value 为字符串须强转 int
if ((int) sysconfig('notify', 'notify_login_scene_enable') === 0) {
return;
}
// 不传 channels=按场景注册渠道展开(admin_login=[inmail,email]),再经各管理员订阅矩阵过滤
NoticeService::send([$admin->id], '登录提醒', "账号 {$admin->username} 于 ".date('Y-m-d H:i').' 登录成功', ['scene' => 'admin_login']);
} catch (\Throwable $e) {
// 通知失败不得影响登录主流程
Log::error('admin login notice failed: ' . $e->getMessage());
}
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace base\common\event\NoticeSended;
use think\facade\Log;
class LogEventBase
{
public function handle(array $payload)
{
// 事件监听处理:payload = [$notice(模型), $targets(投递行数组)](NoticeServiceBase::send trigger 形态)
[$notice, $targets] = $payload;
Log::info('notice sended', [
'notice_id' => (int) $notice->id,
'target_count' => count($targets),
'channels' => array_values(array_unique(array_column($targets, 'channel'))),
]);
}
}

View File

@@ -45,8 +45,10 @@ use app\common\command\tools\log\ToolsLogShow;
use app\common\command\tools\log\ToolsLogStats;
use app\common\command\Test;
use app\common\event\AdminLoginSuccess\LogEvent;
use app\common\event\AdminLoginSuccess\NoticeEvent;
use app\common\event\AdminLoginType\DemoEvent;
use app\common\event\AdminMenuTab\DemoEvent as AdminMenuTabDemoEvent;
use app\common\event\NoticeSended\LogEvent as NoticeSendedLogEvent;
use app\common\provider\ExceptionHandle;
use app\common\provider\Request;
use app\common\provider\View;
@@ -68,6 +70,7 @@ class UlthonAdminService extends Service
'LogWrite' => [],
'AdminLoginSuccess' => [
LogEvent::class,
NoticeEvent::class,
],
'AdminLoginType' => [
DemoEvent::class,
@@ -75,6 +78,9 @@ class UlthonAdminService extends Service
'AdminMenuTab' => [
AdminMenuTabDemoEvent::class,
],
'NoticeSended' => [
NoticeSendedLogEvent::class,
],
];
$this->app->event->listenEvents($event_listen);