notifyConsumer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * 消息通知消费者
  4. * 处理各种消息通知:新订单、新客户、充值、销账、配送状态变更等
  5. */
  6. namespace common\components\rabbitmq;
  7. use biz\wx\classes\WxMessageClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizHd\device\classes\HdDeviceClass;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. use bizHd\shop\classes\ShopClass;
  12. use common\components\noticeUtil;
  13. use common\components\push;
  14. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  15. use PhpAmqpLib\Message\AMQPMessage;
  16. use Yii;
  17. class notifyConsumer implements ConsumerInterface
  18. {
  19. /**
  20. * 执行消费者逻辑
  21. *
  22. * @param AMQPMessage $msg 消息对象
  23. * @return string 消息处理结果
  24. *
  25. * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
  26. * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
  27. * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
  28. */
  29. public function execute(AMQPMessage $msg)
  30. {
  31. try {
  32. // 反序列化消息体
  33. $data = unserialize($msg->body);
  34. if (!is_array($data)) {
  35. noticeUtil::push("通知的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
  36. return ConsumerInterface::MSG_REJECT;
  37. }
  38. print_r($data);
  39. // 根据通知类型分发处理
  40. $type = $data['type'] ?? null;
  41. Yii::$app->db->open();//处理消息前确保连接可用
  42. switch ($type) {
  43. case 'ghs_new_order_notify':
  44. //供货商的新订单通知
  45. $result = $this->ghsNewOrderNotify($data);
  46. break;
  47. case 'hd_new_order_notify':
  48. //花店的新订单通知
  49. $result = $this->hdNewOrderNotify($data);
  50. break;
  51. case 'hd_new_cg_notify':
  52. //花店的新采购单通知
  53. $result = $this->hdNewCgNotify($data);
  54. break;
  55. default:
  56. noticeUtil::push("通知的消费者提示:Unknown notify type: {$type}", '15280215347');
  57. $result = false;
  58. }
  59. Yii::$app->db->close(); // 处理完后关闭连接
  60. if ($result) {
  61. return ConsumerInterface::MSG_ACK;
  62. } else {
  63. noticeUtil::push("通知的消费者提示:Notify message processing failed", '15280215347');
  64. return ConsumerInterface::MSG_REQUEUE;
  65. }
  66. } catch (\Exception $e) {
  67. noticeUtil::push("Notify consumer exception: " . $e->getMessage(), '15280215347');
  68. return ConsumerInterface::MSG_REQUEUE;
  69. }
  70. }
  71. private function ghsNewOrderNotify($data)
  72. {
  73. $orderId = $data['orderId'] ?? 0;
  74. if (empty($orderId)) {
  75. return false;
  76. }
  77. $order = OrderClass::getById($orderId, true);
  78. if (empty($order)) {
  79. return false;
  80. }
  81. $shopId = $order->shopId ?? 0;
  82. $shop = ShopClass::getById($shopId, true);
  83. if (empty($shop)) {
  84. return false;
  85. }
  86. WxMessageClass::ghsHasNewOrderInform($shop, $order);
  87. // 向供货商App发通知
  88. $cgId = $order->purchaseId ?? 0;
  89. $cg = PurchaseClass::getById($cgId, true);
  90. $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
  91. $push = new push('ghs', push::MSG_TYPE_ORDER);
  92. $push->ghsOrderMessage($shop, $cgShop, $order);
  93. return true;
  94. }
  95. private function hdNewOrderNotify($data)
  96. {
  97. return true;
  98. }
  99. private function hdNewCgNotify($data)
  100. {
  101. $cgId = $data['cgId'] ?? 0;
  102. $cg = PurchaseClass::getById($cgId, true);
  103. $shopId = $cg->shopId ?? 0;
  104. $shop = ShopClass::getById($shopId, true);
  105. $allDevices = HdDeviceClass::getAllByCondition(['shopId' => $shopId]);
  106. $cids = array_column($allDevices, 'clientId');
  107. $title = '买花成功';
  108. $shopName = $shop->merchantName . ($shop->shopName != '首店' ? '(' . $shop->shopName . ')' : '');
  109. $content = $shopName . ' ¥' . $cg->actPrice;
  110. $payload = [
  111. "page" => "pagesPurchase/purDetails",
  112. "params" => ["id" => $cgId]
  113. ];
  114. $push = new push('hd', push::MSG_TYPE_ORDER);
  115. $push->pushByCloud($cids, $title, $content, $payload);
  116. return true;
  117. }
  118. }