notifyConsumer.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * 消息通知消费者
  4. * 处理各种消息通知:新订单、新客户、充值、销账、配送状态变更等
  5. */
  6. namespace common\components\rabbitmq;
  7. use common\components\noticeUtil;
  8. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  9. use PhpAmqpLib\Message\AMQPMessage;
  10. use Yii;
  11. class notifyConsumer implements ConsumerInterface
  12. {
  13. /**
  14. * 执行消费者逻辑
  15. *
  16. * @param AMQPMessage $msg 消息对象
  17. * @return string 消息处理结果
  18. *
  19. * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
  20. * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
  21. * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
  22. */
  23. public function execute(AMQPMessage $msg)
  24. {
  25. try {
  26. // 反序列化消息体
  27. $data = unserialize($msg->body);
  28. if (!is_array($data)) {
  29. noticeUtil::push("通知的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
  30. return ConsumerInterface::MSG_REJECT;
  31. }
  32. // 根据通知类型分发处理
  33. $type = $data['type'] ?? null;
  34. switch ($type) {
  35. case 'ghs_new_order':
  36. //供货商的新订单通知
  37. $result = $this->ghsNewOrder($data);
  38. break;
  39. case 'hd_new_order':
  40. //花店的新订单通知
  41. $result = $this->hdNewOrder($data);
  42. break;
  43. case 'hd_new_cg_order':
  44. //花店的新采购单通知
  45. $result = $this->hdNewCgOrder($data);
  46. break;
  47. default:
  48. noticeUtil::push("通知的消费者提示:Unknown notify type: {$type}", '15280215347');
  49. $result = false;
  50. }
  51. if ($result) {
  52. return ConsumerInterface::MSG_ACK;
  53. } else {
  54. noticeUtil::push("通知的消费者提示:Notify message processing failed", '15280215347');
  55. return ConsumerInterface::MSG_REQUEUE;
  56. }
  57. } catch (\Exception $e) {
  58. noticeUtil::push("Notify consumer exception: " . $e->getMessage(), '15280215347');
  59. return ConsumerInterface::MSG_REQUEUE;
  60. }
  61. }
  62. private function ghsNewOrder($data)
  63. {
  64. $msg = $data['msg'] ?? '';
  65. noticeUtil::push($msg, '15280215347');
  66. return true;
  67. }
  68. private function hdNewOrder($data)
  69. {
  70. $msg = $data['msg'] ?? '';
  71. noticeUtil::push($msg, '15280215347');
  72. return true;
  73. }
  74. private function hdNewCgOrder($data)
  75. {
  76. $msg = $data['msg'] ?? '';
  77. noticeUtil::push($msg, '15280215347');
  78. return true;
  79. }
  80. }