| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- /**
- * 客户操作消费者
- * 处理客户创建、修改等业务操作
- */
- namespace common\components\rabbitmq;
- use bizGhs\custom\classes\CustomClass;
- use common\components\noticeUtil;
- use mikemadisonweb\rabbitmq\components\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- use Yii;
- class customConsumer implements ConsumerInterface
- {
- /**
- * 执行消费者逻辑
- *
- * @param AMQPMessage $msg 消息对象
- * @return string 消息处理结果
- *
- * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
- * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
- * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
- */
- public function execute(AMQPMessage $msg)
- {
- try {
- // 反序列化消息体
- $data = unserialize($msg->body);
- if (!is_array($data)) {
- noticeUtil::push("客户的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
- return ConsumerInterface::MSG_REJECT;
- }
- print_r($data);
- // 根据操作类型分发处理
- $type = $data['type'] ?? null;
- switch ($type) {
- case 'add_custom':
- //添加新客户
- $result = CustomClass::generateCustom($data);
- break;
- case 'pull_custom_from_other_shop':
- //从分店拉取客户
- $result = CustomClass::pullOtherShopCustom($data);
- break;
- default:
- noticeUtil::push("客户的消费者报错,不存在的类型: {$type}");
- $result = false;
- }
- if ($result) {
- return ConsumerInterface::MSG_ACK;
- } else {
- noticeUtil::push("客户的消费者报错:Notify message processing failed", '15280215347');
- return ConsumerInterface::MSG_REQUEUE;
- }
- } catch (\Exception $e) {
- noticeUtil::push("客户的消费者报错:" . $e->getMessage());
- return ConsumerInterface::MSG_REQUEUE;
- }
- }
- }
|