上菜状态推送

This commit is contained in:
张松
2025-11-28 16:42:59 +08:00
parent 6ea412f876
commit edb7639c9c

View File

@@ -0,0 +1,94 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../support/bootstrap.php';
use ba\Random;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use support\think\Db;
use support\Log;
use GatewayClient\Gateway;
use support\Redis;
try {
// define('AMQP_DEBUG', true);
$host = config('cons.mq.host');
$port = config('cons.mq.port');
$user = config('cons.mq.user');
$password = config('cons.mq.password');
$queue = config('cons.mq.queue_t') . '-order.detail.change.queue';
// 防止空闲时间断线必须设置心跳
$connection = new AMQPStreamConnection($host, $port, $user, $password,
'/',
false,
'AMQPLAIN',
null,
'en_US',
60,
60,
null,
false,
15
);
$rabbit_channel = $connection->channel();
$rabbit_channel->queue_declare($queue, false, true, false, false);
$callback = function ($msg) use ($queue){
$date_time = date('Y-m-d H:i:s');
$data = $msg->body;
Log::info('MQ收到消息[商品状态更新]--->' . $data . '--->' . $date_time . "\n");
// 如果是数字。则打印订单
$is_log = false;
$curl_error = '';
$rand = 'product_update' . Random::build();
$send_id = 'cart_update' . $data . '_all' ;
$method = 'sendToGroup';
$snd_data =[
'msg' => '购物车刷新',
'operate_type' => 'reload',
'msg_id' => $rand,
'type' => 'cashier',
'data_type' => 'cart',
'status' => 1
];
$snd_data_json = json_encode($snd_data);
Gateway::$registerAddress = '127.0.0.1:1238';
$res = Gateway::sendToGroup($send_id, $snd_data_json);
Log::info('商品状态更新推送结果-->' . $res);
$result = Redis::get($snd_data['send_id']);
if($result) {
$msg_id_arr = json_decode($result, true);
$msg_id_arr_c = count($msg_id_arr);
$msg_id_arr[$msg_id_arr_c] = $snd_data;
$result_n = json_encode($msg_id_arr);
\extend\workermans\model\Base::setredis_new($result_n, $snd_data['send_id']);
}else {
$params_arr_n[] = $snd_data;
print_r('数组-------->' . json_encode($params_arr_n)) . "\r\n";
\extend\workermans\model\Base::setredis_new(json_encode($params_arr_n), $snd_data['send_id']);
}
if($is_log) {
Db::table('tb_mq_log')->insert([
'queue' => $queue,
'msg' => $data,
'type' => 'product_update',
'plat' => 'product',
'create_time' => date('Y-m-d H:i:s'),
'fail_time' => date('Y-m-d H:i:s'),
'err_info' => $curl_error,
]);
}
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};
$rabbit_channel->basic_consume($queue, '', false, false, false, false, $callback);
while ($rabbit_channel->is_consuming()) {
Log::info('product.info.change.queue-MQ准备调用wait');
$rabbit_channel->wait();
}
$rabbit_channel->close();
$connection->close();
}catch (Exception $exception) {
Log::info('product.info.change.queue-MQ错误' . $exception->getMessage());
}