BarkChannel.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Channels;
  3. use Helpers;
  4. use Http;
  5. use Illuminate\Notifications\Notification;
  6. use Illuminate\Support\Arr;
  7. use Log;
  8. use Str;
  9. class BarkChannel
  10. {
  11. public function send($notifiable, Notification $notification)
  12. {
  13. if (method_exists($notification, 'toBark')) {
  14. $message = $notification->toBark($notifiable);
  15. } else {
  16. $message = $notification->toCustom($notifiable);
  17. }
  18. if (isset($message['url_type'])) { // 生成对公消息查询URL
  19. $msgId = Str::uuid();
  20. $message['url'] = route('message.show', ['type' => $message['url_type'], $msgId]);
  21. unset($message['url_type']);
  22. }
  23. $response = Http::timeout(15)
  24. ->get('https://api.day.app/'.sysConfig('bark_key')."/{$message['title']}/{$message['content']}?".http_build_query(Arr::except($message, ['title', 'content'])));
  25. if ($response->ok()) {
  26. $ret = $response->json();
  27. // 发送成功
  28. if ($ret['code'] === 200) {
  29. Helpers::addNotificationLog($message['title'], $message['content'], 3, 1, null, $msgId ?? null);
  30. return $ret;
  31. }
  32. // 发送失败
  33. Helpers::addNotificationLog($message['title'], $message['content'], 3, -1);
  34. return false;
  35. }
  36. // 发送错误
  37. Log::critical('[Bark] 消息推送异常:'.var_export($response, true));
  38. return false;
  39. }
  40. }