2022-12-11 11:33:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Notifications;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @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-01-10 12:43:54 +00:00
|
|
|
if (!$data) {
|
|
|
|
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-01-10 12:43:54 +00:00
|
|
|
if (!$key) {
|
|
|
|
$key = config('settings.wecom.robot_hook.default');
|
|
|
|
}
|
|
|
|
|
|
|
|
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $key, [
|
|
|
|
'msgtype' => 'markdown',
|
|
|
|
'markdown' => [
|
|
|
|
'content' => view($view, [
|
|
|
|
'data' => $data['data'],
|
|
|
|
])->render(),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!$resp->successful()) {
|
|
|
|
Log::error('企业微信机器人发送失败', $data['data']);
|
|
|
|
}
|
2022-12-11 11:33:34 +00:00
|
|
|
}
|
|
|
|
}
|