ServerChanChannel.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Channels;
  3. use Cache;
  4. use Helpers;
  5. use Http;
  6. use Illuminate\Notifications\Notification;
  7. use Log;
  8. class ServerChanChannel
  9. {
  10. public function send($notifiable, Notification $notification)
  11. {
  12. $message = $notification->toCustom($notifiable);
  13. $cacheKey = 'serverChanCount'.date('d');
  14. if (Cache::has($cacheKey)) {
  15. Cache::increment($cacheKey);
  16. } else {
  17. Cache::put($cacheKey, 1, Day); // 24小时
  18. }
  19. // 一天仅可发送不超过500条
  20. if (Cache::get($cacheKey) < 500) {
  21. $response = Http::timeout(15)
  22. ->get('https://sc.ftqq.com/'.sysConfig('server_chan_key').'.send?text='.$message['title'].'&desp='.urlencode($message['content']));
  23. } else {
  24. Log::error('ServerChan消息推送异常:今日500条限额已耗尽!');
  25. return false;
  26. }
  27. // 发送成功
  28. if ($response->ok()) {
  29. $ret = $response->json();
  30. if (! $ret['errno']) {
  31. Helpers::addNotificationLog($message['title'], $message['content'], 2);
  32. return $ret;
  33. }
  34. // 发送失败
  35. Helpers::addNotificationLog($message['title'], $message['content'], 2, 'admin', -1, $ret ? $ret['errmsg'] : '未知');
  36. return false;
  37. }
  38. // 发送错误
  39. Log::error('ServerChan消息推送异常:'.var_export($response, true));
  40. return false;
  41. }
  42. }