Notification.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\admin\controller\shopro\notification\traits;
  3. /**
  4. * 消息通知,额外方法
  5. */
  6. trait Notification
  7. {
  8. protected $notificationTypes = [
  9. \addons\shopro\notification\order\OrderNew::class,
  10. \addons\shopro\notification\order\OrderDispatched::class,
  11. \addons\shopro\notification\order\aftersale\OrderAftersaleChange::class,
  12. \addons\shopro\notification\order\aftersale\OrderAdminAftersaleChange::class,
  13. \addons\shopro\notification\order\OrderRefund::class,
  14. \addons\shopro\notification\order\OrderApplyRefund::class,
  15. // 商品
  16. \addons\shopro\notification\goods\StockWarning::class,
  17. // 钱包
  18. \addons\shopro\notification\wallet\CommissionChange::class,
  19. \addons\shopro\notification\wallet\MoneyChange::class,
  20. \addons\shopro\notification\wallet\ScoreChange::class,
  21. // 活动
  22. \addons\shopro\notification\activity\GrouponFail::class,
  23. \addons\shopro\notification\activity\GrouponFinish::class,
  24. ];
  25. /**
  26. * 根据接收人类型,获取消息类型
  27. *
  28. * @param array|string $receiverType
  29. * @return array
  30. */
  31. protected function getNotificationsByReceiverType($receiverType = 'user')
  32. {
  33. $receiverType = is_array($receiverType) ? $receiverType : [$receiverType];
  34. $notifications = $this->getNotifications();
  35. $receiverNotifications = [];
  36. foreach ($notifications as $notification) {
  37. if (in_array($notification['receiver_type'], $receiverType)) {
  38. $receiverNotifications[] = $notification;
  39. }
  40. }
  41. return $receiverNotifications;
  42. }
  43. /**
  44. * 根据事件类型获取消息
  45. *
  46. * @param string $event
  47. * @return void
  48. */
  49. protected function getNotificationByEvent($event)
  50. {
  51. $notifications = $this->getNotifications();
  52. $notifications = array_column($notifications, null, 'event');
  53. return $notifications[$event] ?? null;
  54. }
  55. /**
  56. * 按照事件类型获取配置分组
  57. *
  58. * @param string $event
  59. * @return array
  60. */
  61. protected function getGroupConfigs($event = null)
  62. {
  63. // 获取所有配置
  64. $configs = $this->model->select();
  65. $newConfigs = [];
  66. foreach ($configs as $config) {
  67. $newConfigs[$config['event']][$config['channel']] = $config;
  68. }
  69. return $event ? ($newConfigs[$event] ?? []) : $newConfigs;
  70. }
  71. /**
  72. * 获取所有消息类型
  73. *
  74. * @return array
  75. */
  76. protected function getNotifications()
  77. {
  78. $types = [];
  79. foreach ($this->notificationTypes as $key => $class_name) {
  80. $class = new $class_name();
  81. $currentFields = $class->returnField;
  82. $currentFields['event'] = $class->event;
  83. $currentFields['receiver_type'] = $class->receiver_type;
  84. $currentFields['template'] = $class->template;
  85. $types[] = $currentFields;
  86. }
  87. return $types;
  88. }
  89. /**
  90. * 格式化详情返回结果
  91. *
  92. * @param array $notification
  93. * @param string $event
  94. * @param string $channel
  95. * @return array
  96. */
  97. protected function formatNotification($notification, $event, $channel)
  98. {
  99. $currentConfigs = $this->getGroupConfigs($event);
  100. $currentConfig = $currentConfigs[$channel] ?? null;
  101. if (in_array($channel, ['WechatOfficialAccount', 'WechatMiniProgram', 'WechatOfficialAccountBizsend'])) {
  102. $currentTemplate = $notification['template'][$channel] ?? [];
  103. unset($notification['template']);
  104. $notification['wechat'] = $currentTemplate;
  105. }
  106. $notification['type'] = $currentConfig['type'] ?? 'default';
  107. $content = $currentConfig['content'] ?? null;
  108. if (!is_array($content)) {
  109. $notification['content_text'] = $content;
  110. }
  111. if ($content && is_array($content)) {
  112. $contentFields = [];
  113. if (isset($content['fields']) && $content['fields']) { // 判断数组是否存在 fields 设置
  114. $contentFields = array_column($content['fields'], null, 'field');
  115. }
  116. $tempFields = array_column($notification['fields'], null, 'field');
  117. $configField = array_merge($tempFields, $contentFields);
  118. $content['fields'] = array_values($configField);
  119. $notification['content'] = $content;
  120. } else {
  121. $notification['content'] = [
  122. 'template_id' => '',
  123. 'fields' => $notification['fields']
  124. ];
  125. }
  126. unset($notification['fields']);
  127. return $notification;
  128. }
  129. /**
  130. * 格式化微信公众号,小程序默认模板时 自动配置 模板字段
  131. *
  132. * @return void
  133. */
  134. protected function formatWechatTemplateFields($event, $channel, $fields)
  135. {
  136. $notification = $this->getNotificationByEvent($event);
  137. $channelFields = $notification['template'][$channel]['fields'] ?? [];
  138. $channelFields = array_column($channelFields, null, 'field');
  139. foreach ($fields as $key => &$field) {
  140. $field_name = $field['field'] ?? '';
  141. if ($field_name && isset($channelFields[$field_name])) {
  142. $field['template_field'] = $channelFields[$field_name]['template_field'] ?? '';
  143. }
  144. }
  145. return $fields;
  146. }
  147. }