notifyConsumer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. switch ($type) {
  42. case 'ghs_new_order_notify':
  43. //供货商的新订单通知
  44. $result = $this->ghsNewOrderNotify($data);
  45. break;
  46. case 'hd_new_order_notify':
  47. //花店的新订单通知
  48. $result = $this->hdNewOrderNotify($data);
  49. break;
  50. case 'hd_new_cg_notify':
  51. //花店的新采购单通知
  52. $result = $this->hdNewCgNotify($data);
  53. break;
  54. default:
  55. noticeUtil::push("通知的消费者提示:Unknown notify type: {$type}", '15280215347');
  56. $result = false;
  57. }
  58. if ($result) {
  59. return ConsumerInterface::MSG_ACK;
  60. } else {
  61. noticeUtil::push("通知的消费者提示:Notify message processing failed", '15280215347');
  62. return ConsumerInterface::MSG_REQUEUE;
  63. }
  64. } catch (\Exception $e) {
  65. noticeUtil::push("Notify consumer exception: " . $e->getMessage(), '15280215347');
  66. return ConsumerInterface::MSG_REQUEUE;
  67. }
  68. }
  69. private function ghsNewOrderNotify($data)
  70. {
  71. $orderId = $data['orderId'] ?? 0;
  72. if (empty($orderId)) {
  73. return false;
  74. }
  75. $order = OrderClass::getById($orderId, true);
  76. if (empty($order)) {
  77. return false;
  78. }
  79. $shopId = $order->shopId ?? 0;
  80. $shop = ShopClass::getById($shopId, true);
  81. if (empty($shop)) {
  82. return false;
  83. }
  84. WxMessageClass::ghsHasNewOrderInform($shop, $order);
  85. // 向供货商App发通知
  86. $cgId = $order->purchaseId ?? 0;
  87. $cg = PurchaseClass::getById($cgId, true);
  88. $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
  89. $push = new push('ghs', push::MSG_TYPE_ORDER);
  90. $push->ghsOrderMessage($shop, $cgShop, $order);
  91. return true;
  92. }
  93. private function hdNewOrderNotify($data)
  94. {
  95. return true;
  96. }
  97. private function hdNewCgNotify($data)
  98. {
  99. $cgId = $data['cgId'] ?? 0;
  100. $cg = PurchaseClass::getById($cgId, true);
  101. $shopId = $cg->shopId ?? 0;
  102. $shop = ShopClass::getById($shopId, true);
  103. $allDevices = 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. }