notifyConsumer.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. 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. case 'ghs_pt_error':
  54. //供货商跑腿异常通知
  55. $result = $this->ghsPtErrorAction($data);
  56. break;
  57. break;
  58. case 'hd_pt_error':
  59. //花店跑腿异常通知
  60. $result = $this->hdPtErrorAction($data);
  61. break;
  62. default:
  63. noticeUtil::push("通知的消费者提示:Unknown notify type: {$type}", '15280215347');
  64. $result = false;
  65. }
  66. if ($result) {
  67. return ConsumerInterface::MSG_ACK;
  68. } else {
  69. noticeUtil::push("通知的消费者提示:Notify message processing failed", '15280215347');
  70. return ConsumerInterface::MSG_REQUEUE;
  71. }
  72. } catch (\Exception $e) {
  73. noticeUtil::push("Notify consumer exception: " . $e->getMessage(), '15280215347');
  74. return ConsumerInterface::MSG_REQUEUE;
  75. }
  76. }
  77. private function ghsPtErrorAction($data)
  78. {
  79. $orderId = $data['orderId'] ?? 0;
  80. $event = $data['event'] ?? '';
  81. $order = OrderClass::getById($orderId, true);
  82. if (!empty($order)) {
  83. $sendNum = $order->sendNum ?? '';
  84. $result = $event . ',取货号 ' . $sendNum;
  85. $customName = $order->customName ?? '';
  86. $shopId = $order->shopId ?? '';
  87. $shop = ShopClass::getById($shopId, true);
  88. if (!empty($shop)) {
  89. WxMessageClass::expressErrorInform($shop, $customName, $orderId, $result);
  90. }
  91. }
  92. return true;
  93. }
  94. private function hdPtErrorAction($data)
  95. {
  96. $orderId = $data['orderId'] ?? 0;
  97. $event = $data['event'] ?? '';
  98. $order = \bizHd\order\classes\OrderClass::getById($orderId, true);
  99. if (!empty($order)) {
  100. $sendNum = $order->sendNum ?? '';
  101. $result = $event . ',取货号 ' . $sendNum;
  102. $customName = $order->customName ?? '';
  103. $shopId = $order->shopId ?? '';
  104. $shop = ShopClass::getById($shopId, true);
  105. if (!empty($shop)) {
  106. WxMessageClass::expressErrorInform($shop, $customName, $orderId, $result);
  107. }
  108. }
  109. return true;
  110. }
  111. private function ghsNewOrderNotify($data)
  112. {
  113. $orderId = $data['orderId'] ?? 0;
  114. if (empty($orderId)) {
  115. return false;
  116. }
  117. $order = OrderClass::getById($orderId, true);
  118. if (empty($order)) {
  119. return false;
  120. }
  121. $shopId = $order->shopId ?? 0;
  122. $shop = ShopClass::getById($shopId, true);
  123. if (empty($shop)) {
  124. return false;
  125. }
  126. WxMessageClass::ghsHasNewOrderInform($shop, $order);
  127. // 向供货商App发通知
  128. $cgId = $order->purchaseId ?? 0;
  129. $cg = PurchaseClass::getById($cgId, true);
  130. $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
  131. $push = new push('ghs', push::MSG_TYPE_ORDER);
  132. $push->ghsOrderMessage($shop, $cgShop, $order);
  133. return true;
  134. }
  135. private function hdNewOrderNotify($data)
  136. {
  137. return true;
  138. }
  139. private function hdNewCgNotify($data)
  140. {
  141. $cgId = $data['cgId'] ?? 0;
  142. $cg = PurchaseClass::getById($cgId, true);
  143. $shopId = $cg->shopId ?? 0;
  144. $shop = ShopClass::getById($shopId, true);
  145. $allDevices = HdDeviceClass::getAllByCondition(['shopId' => $shopId]);
  146. $cids = array_column($allDevices, 'clientId');
  147. $title = '买花成功';
  148. $shopName = $shop->merchantName . ($shop->shopName != '首店' ? '(' . $shop->shopName . ')' : '');
  149. $content = $shopName . ' ¥' . $cg->actPrice;
  150. $payload = [
  151. "page" => "pagesPurchase/purDetails",
  152. "params" => ["id" => $cgId]
  153. ];
  154. $push = new push('hd', push::MSG_TYPE_ORDER);
  155. $push->pushByCloud($cids, $title, $content, $payload);
  156. return true;
  157. }
  158. }