CustomeNotice.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace addons\shopro\notification;
  3. use addons\shopro\channel\Database;
  4. use addons\shopro\channel\Websocket;
  5. use addons\shopro\notification\traits\Notification as NotificationTrait;
  6. /**
  7. * 自定义通知
  8. */
  9. class CustomeNotice extends Notification
  10. {
  11. use NotificationTrait;
  12. // 队列延迟时间,必须继承 ShouldQueue 接口
  13. public $delay = 0;
  14. public $receiver_type = 'admin';
  15. // 消息类型 Notification::$notificationType
  16. public $notification_type = 'system';
  17. // 发送类型
  18. public $event = 'custom';
  19. // 额外发送渠道
  20. public $channels = [];
  21. // 额外数据
  22. public $data = [];
  23. public $template = [
  24. 'MessageDefaultContent' => '',
  25. ];
  26. // 返回的字段列表
  27. public $returnField = [];
  28. public function __construct($params = [], $data = [])
  29. {
  30. $this->receiver_type = $params['receiver_type'] ?? 'admin';
  31. $this->notification_type = $params['notification_type'] ?? 'system';
  32. $this->channels = $params['channels'] ?? [];
  33. $this->data = $data;
  34. }
  35. public function channels($notifiable)
  36. {
  37. // 默认发送渠道
  38. $channels = [Database::class, Websocket::class];
  39. return array_merge($channels, $this->channels);
  40. }
  41. /**
  42. * 数据库通知数据
  43. *
  44. * @param \think\Model $notifiable
  45. * @return array
  46. */
  47. public function toDatabase($notifiable)
  48. {
  49. $params = $this->getArray();
  50. return $params;
  51. }
  52. /**
  53. * socket 通知数据
  54. *
  55. * @param \think\Model $notifiable
  56. * @return array
  57. */
  58. public function toWebsocket($notifiable)
  59. {
  60. $params = $this->getArray();
  61. return $params;
  62. }
  63. /**
  64. * 组合数据参数
  65. *
  66. * @param \think\Model $notifiable
  67. * @return array
  68. */
  69. protected function getArray()
  70. {
  71. $params['data'] = [
  72. 'jump_url' => $this->data['jump_url'] ?? ''
  73. ];
  74. $params['message_type'] = 'notification';
  75. $params['class_name'] = static::class;
  76. $params['message_text'] = $this->data['message_text'] ?? '';
  77. $params['message_title'] = $this->data['message_title'] ?? '';
  78. $this->template['MessageDefaultContent'] = $this->data['message_title'] ?? '';
  79. // 统一跳转地址
  80. return $params;
  81. }
  82. }