Notification.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace addons\shopro\job;
  3. use think\queue\Job;
  4. use think\exception\HttpResponseException;
  5. /**
  6. * 队列消息通知
  7. */
  8. class Notification extends BaseJob
  9. {
  10. /**
  11. * 发送通知
  12. */
  13. public function send(Job $job, $data)
  14. {
  15. // 删除 job,防止这个队列一直异常无法被删除
  16. $job->delete();
  17. try {
  18. // 这里获取到的 $notifiables 和 notification 两个都是数组,不是类,尴尬, 更可恨的 notification 只是 {"delay":0,"event":"changemobile"}
  19. $notifiables = $data['notifiables'];
  20. $notification = $data['notification'];
  21. // 因为 notification 只有参数,需要把对应的类传过来,在这里重新初始化
  22. $notification_name = $data['notification_name'];
  23. $notifiable_name = $data['notifiable_name'];
  24. // 重新实例化 notification 实例
  25. if (class_exists($notification_name)) {
  26. $notification = new $notification_name($notification['data']);
  27. }
  28. // 重新实例化 notifiable
  29. if (class_exists($notifiable_name)) {
  30. $notifiable = new $notifiable_name();
  31. $notifiables = $notifiable->where('id', 'in', array_column($notifiables, 'id'))->select();
  32. }
  33. // 发送消息
  34. \addons\shopro\library\notify\Notify::sendNow($notifiables, $notification);
  35. } catch (HttpResponseException $e) {
  36. $data = $e->getResponse()->getData();
  37. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  38. format_log_error($e, 'Notification.send.HttpResponseException', $message);
  39. } catch (\Exception $e) {
  40. format_log_error($e, 'Notification.send.' . ($notification->event ?? ''));
  41. }
  42. }
  43. }