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