这是一个精简的通知模块,支持邮件、企业微信和Webhook通知。使用DTO提供类型安全和更好的代码可读性,同时保持简洁的设计。配置使用独立的配置类管理。
// config.php
return [
'notify' => [
'default_channel' => 'email', // 默认通知渠道: email, wechat_work, webhook
]
];复用现有的邮件模块配置:
// config.php
return [
'mail' => [
'host' => 'smtp.qq.com',
'port' => 465,
'username' => '[email protected]',
'password' => 'your-password',
'site' => '系统通知'
]
];// config.php
return [
'wechat' => [
'corp_id' => 'your-corp-id',
'corp_secret' => 'your-corp-secret',
'agent_id' => 'your-agent-id',
'default_recipient' => 'user-id'
]
];// config.php
return [
'webhook' => [
'url' => 'https://your-webhook-url.com/api/notify',
'auth_header' => 'Authorization: Bearer your-token', // 可选
'timeout' => 30 // 超时时间(秒)
]
];use nova\plugin\notify\NotifyManager;
use nova\plugin\notify\dto\NotifyDataDTO;
$notify = NotifyManager::getInstance();
// 创建DTO
$dto = new NotifyDataDTO([
'title' => '订单通知',
'message' => '您有一个新订单',
'type' => 'success',
'recipient' => '[email protected]'
]);
// 发送邮件通知
$result = $notify->send($dto, 'email');
// 发送企业微信通知
$result = $notify->send($dto, 'wechat_work');
// 发送Webhook通知
$result = $notify->send($dto, 'webhook');// 直接使用数组发送
$result = $notify->sendFromArray([
'title' => '测试通知',
'message' => '这是一个测试',
'type' => 'default',
'recipient' => '[email protected]'
], 'webhook');$dto = new NotifyDataDTO([
'title' => '新订单',
'message' => '您有一个新订单需要处理',
'type' => 'success',
'recipient' => '[email protected]',
'actionLeftUrl' => 'https://example.com/orders/123',
'actionLeftText' => '查看订单',
'actionRightUrl' => 'https://example.com/orders/123/approve',
'actionRightText' => '批准订单'
]);
$notify->send($dto, 'webhook');// 测试邮件通知
$notify->test('email');
// 测试企业微信通知
$notify->test('wechat_work');
// 测试Webhook通知
$notify->test('webhook');支持的通知类型:
default- 默认通知(蓝色)success- 成功通知(绿色)warning- 警告通知(橙色)error- 错误通知(红色)
class NotifyDataDTO
{
public string $title; // 通知标题
public string $message; // 通知内容
public string $type = 'default'; // 通知类型
public ?string $recipient = null; // 收件人
public ?string $actionLeftUrl = null; // 左侧按钮链接
public ?string $actionLeftText = null; // 左侧按钮文本
public ?string $actionRightUrl = null; // 右侧按钮链接
public ?string $actionRightText = null; // 右侧按钮文本
}Webhook渠道会向指定URL发送POST请求,数据格式如下:
{
"title": "通知标题",
"message": "通知内容",
"type": "success",
"recipient": "收件人",
"actionLeftUrl": "左侧按钮链接",
"actionLeftText": "左侧按钮文本",
"actionRightUrl": "右侧按钮链接",
"actionRightText": "右侧按钮文本",
"timestamp": 1640995200,
"channel": "webhook"
}default_channel- 默认通知渠道
host- SMTP服务器地址port- SMTP端口username- 邮箱用户名password- 邮箱密码site- 发件人名称
corp_id- 企业微信企业IDcorp_secret- 企业微信应用Secretagent_id- 企业微信应用IDdefault_recipient- 默认收件人
url- Webhook接收地址auth_header- 认证头部(可选)timeout- 请求超时时间(秒)
- 类型安全 - 使用DTO提供编译时类型检查
- 代码可读性 - 属性访问比数组键更直观
- IDE支持 - 更好的自动完成和重构支持
- 配置分离 - 每个模块使用独立的配置类
- 复用现有配置 - 邮件配置复用MailConfig
- 灵活扩展 - 支持多种通知渠道,包括Webhook
- 性能更好 - 移除了数据库操作和复杂的输出缓冲
- 易于扩展 - 简单的接口设计,容易添加新的通知渠道