customConsumer.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * 客户操作消费者
  4. * 处理客户创建、修改等业务操作
  5. */
  6. namespace common\components\rabbitmq;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizHd\custom\classes\CustomClass as HdCustomClass;
  9. use common\components\noticeUtil;
  10. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  11. use PhpAmqpLib\Message\AMQPMessage;
  12. class customConsumer extends baseConsumer
  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. $this->ensureDbConnection();
  28. // 反序列化消息体
  29. $data = unserialize($msg->body);
  30. if (!is_array($data)) {
  31. noticeUtil::push("客户的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
  32. return ConsumerInterface::MSG_REJECT;
  33. }
  34. print_r($data);
  35. // 根据操作类型分发处理
  36. $type = $data['type'] ?? null;
  37. $result = $this->runWithDbReconnect(function () use ($type, $data) {
  38. switch ($type) {
  39. case 'add_custom':
  40. //添加新客户
  41. return CustomClass::generateCustom($data);
  42. case 'pull_custom_from_other_shop':
  43. //从分店拉取客户
  44. return CustomClass::pullOtherShopCustom($data);
  45. case 'hd_change_custom_expense_level':
  46. //客户消费等级变更
  47. return HdCustomClass::updateCustomExpenseLevel($data);
  48. default:
  49. noticeUtil::push("客户的消费者报错,不存在的类型: {$type}");
  50. return false;
  51. }
  52. });
  53. if ($result) {
  54. return ConsumerInterface::MSG_ACK;
  55. } else {
  56. noticeUtil::push("客户的消费者报错:Notify message processing failed", '15280215347');
  57. return ConsumerInterface::MSG_REQUEUE;
  58. }
  59. } catch (\Exception $e) {
  60. noticeUtil::push("客户的消费者报错:" . $e->getMessage());
  61. return ConsumerInterface::MSG_REQUEUE;
  62. }
  63. }
  64. }