Lae/app/Notifications/Channels/WeComChannel.php

50 lines
1.2 KiB
PHP
Raw Normal View History

2022-12-11 11:33:34 +00:00
<?php
2023-01-13 14:11:56 +00:00
namespace App\Notifications\Channels;
2022-12-11 11:33:34 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
2023-01-10 12:43:54 +00:00
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
2022-12-11 11:33:34 +00:00
class WeComChannel extends Notification
{
use Queueable;
/**
2023-01-10 12:43:54 +00:00
* Send the given notification.
*
2023-02-07 09:04:11 +00:00
* @param mixed $notifiable
* @param Notification $notification
2022-12-11 11:33:34 +00:00
* @return void
*/
2023-01-10 12:43:54 +00:00
public function send(mixed $notifiable, Notification $notification): void
2022-12-11 11:33:34 +00:00
{
2023-01-10 12:43:54 +00:00
$data = $notification->toWeCom($notifiable);
2022-12-11 11:33:34 +00:00
2023-02-07 09:04:11 +00:00
if (! $data) {
2023-01-10 12:43:54 +00:00
return;
}
2022-12-11 11:33:34 +00:00
2023-01-10 12:43:54 +00:00
$view = $data['view'];
$key = $data['wecom_key'] ?? null;
2022-12-11 11:33:34 +00:00
2023-02-07 09:04:11 +00:00
if (! $key) {
2023-01-10 12:43:54 +00:00
$key = config('settings.wecom.robot_hook.default');
}
2023-02-07 09:04:11 +00:00
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='.$key, [
2023-01-10 12:43:54 +00:00
'msgtype' => 'markdown',
'markdown' => [
'content' => view($view, [
'data' => $data['data'],
])->render(),
],
]);
2023-02-07 09:04:11 +00:00
if (! $resp->successful()) {
2023-01-10 12:43:54 +00:00
Log::error('企业微信机器人发送失败', $data['data']);
}
2022-12-11 11:33:34 +00:00
}
}