customConsumer.php 2.2 KB

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