Notify.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace addons\shopro\library\notify;
  3. use addons\shopro\exception\Exception;
  4. use think\queue\ShouldQueue;
  5. class Notify
  6. {
  7. public function sendNotify($notifiables, $notification) {
  8. if ($notification instanceof ShouldQueue) {
  9. // 队列执行
  10. return $this->sendQueueNotify($notifiables, $notification, $notification->delay);
  11. }
  12. return $this->sendNowNotify($notifiables, $notification);
  13. }
  14. /**
  15. * 立即发送
  16. */
  17. public function sendNowNotify($notifiables, $notification) {
  18. foreach ($notifiables as $key => $notifiable) {
  19. $channels = $notification->via($notifiable);
  20. if (empty($channels)) {
  21. continue;
  22. }
  23. foreach ($channels as $k => $channel) {
  24. (new $channel)->send($notifiable, $notification);
  25. }
  26. }
  27. }
  28. /**
  29. * 队列发送
  30. * delay 延迟时间
  31. */
  32. public function sendQueueNotify($notifiables, $notification, $delay) {
  33. if ($delay > 0) {
  34. // 异步延迟发送
  35. \think\Queue::later($delay, '\addons\shopro\job\Notification@send', [
  36. 'notifiables' => $notifiables,
  37. 'notification' => $notification,
  38. 'notification_name' => get_class($notification)
  39. ], 'shopro');
  40. } else {
  41. // 异步立即发送
  42. \think\Queue::push('\addons\shopro\job\Notification@send', [
  43. 'notifiables' => $notifiables,
  44. 'notification' => $notification,
  45. 'notification_name' => get_class($notification)
  46. ], 'shopro');
  47. }
  48. }
  49. public static function __callStatic($name, $arguments)
  50. {
  51. return (new self)->{$name . 'Notify'}(...$arguments);
  52. }
  53. }