notifyConsumer.php 6.0 KB

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