customConsumer.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * 客户操作消费者
  4. * 处理客户创建、修改等业务操作
  5. */
  6. namespace common\components\rabbitmq;
  7. use bizGhs\custom\classes\CustomClass;
  8. use common\components\noticeUtil;
  9. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  10. use PhpAmqpLib\Message\AMQPMessage;
  11. use Yii;
  12. class customConsumer implements ConsumerInterface
  13. {
  14. /**
  15. * 执行消费者逻辑
  16. *
  17. * @param AMQPMessage $msg 消息对象
  18. * @return string 消息处理结果
  19. *
  20. * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
  21. * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
  22. * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
  23. */
  24. public function execute(AMQPMessage $msg)
  25. {
  26. try {
  27. // 反序列化消息体
  28. $data = unserialize($msg->body);
  29. if (!is_array($data)) {
  30. noticeUtil::push("客户的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
  31. return ConsumerInterface::MSG_REJECT;
  32. }
  33. print_r($data);
  34. // 根据操作类型分发处理
  35. $type = $data['type'] ?? null;
  36. switch ($type) {
  37. case 'add_custom':
  38. //添加新客户
  39. $result = CustomClass::generateCustom($data);
  40. break;
  41. case 'pull_custom_from_other_shop':
  42. //从分店拉取客户
  43. $result = CustomClass::pullOtherShopCustom($data);
  44. break;
  45. default:
  46. noticeUtil::push("客户的消费者报错,不存在的类型: {$type}");
  47. $result = false;
  48. }
  49. if ($result) {
  50. return ConsumerInterface::MSG_ACK;
  51. } else {
  52. noticeUtil::push("客户的消费者报错:Notify message processing failed", '15280215347');
  53. return ConsumerInterface::MSG_REQUEUE;
  54. }
  55. } catch (\Exception $e) {
  56. noticeUtil::push("客户的消费者报错:" . $e->getMessage());
  57. return ConsumerInterface::MSG_REQUEUE;
  58. }
  59. }
  60. }