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. 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. $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. }