120 lines
4.3 KiB
PHP
120 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace app\command;
|
|
|
|
use app\common\model\MqLog;
|
|
use app\common\model\RabbitMqConfig;
|
|
use ba\Random;
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection;
|
|
use support\think\Db;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use support\Log;
|
|
|
|
|
|
class RabbProductUpdate extends Command
|
|
{
|
|
protected static $defaultName = 'rabbproductupdate';
|
|
protected static $defaultDescription = 'rabbproductupdate';
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
protected function configure()
|
|
{
|
|
$this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
|
|
}
|
|
|
|
/**
|
|
* @param InputInterface $input
|
|
* @param OutputInterface $output
|
|
* @return int
|
|
*/
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$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') . '-product.info.change.queue';
|
|
// 防止空闲时间断线必须设置心跳
|
|
$connection = new AMQPStreamConnection($host, $port, $user, $password,
|
|
'/',
|
|
false,
|
|
'AMQPLAIN',
|
|
null,
|
|
'en_US',
|
|
60,
|
|
60,
|
|
null,
|
|
false,
|
|
30
|
|
);
|
|
$rabbit_channel = $connection->channel();
|
|
$rabbit_channel->queue_declare($queue, false, true, false, false, false, [
|
|
// 'x-message-ttl' => ['I', 180000]
|
|
]);
|
|
|
|
$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 = '';
|
|
if(is_numeric($data)) {
|
|
$rand = 'product_update' . Random::build();
|
|
$send_id = 'product_update'. $data;
|
|
$method = 'sendToGroup';
|
|
$snd_data = [
|
|
'msg' => '商品更新',
|
|
'type' => 'product',
|
|
'operate_type' => 'product_update',
|
|
'data_type' => 'product_update',
|
|
'status' => 1,
|
|
'method' => $method,
|
|
'send_num' => 0,
|
|
'send_id' => $send_id,
|
|
'msg_id' => $rand,
|
|
'data' => $data
|
|
];
|
|
$snd_data_json = json_encode($snd_data);
|
|
$url = 'http://127.0.0.1:8686/?method=' . $method . '&account='. $send_id .'¶ms=' . $snd_data_json; // 替换为你的服务地址和端口
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
$output = curl_exec($ch);
|
|
if (curl_errno($ch)) {
|
|
$is_log = true;
|
|
$curl_error = 'Curl error: ' . curl_error($ch);
|
|
}
|
|
curl_close($ch);
|
|
Log::info('内部通讯结果(商品更新)--->' . $output . '通讯ID--->shop_id-->' .$send_id. '---' . date('Y-m-d H:i:s') . "\n");
|
|
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()) {
|
|
$rabbit_channel->wait();
|
|
}
|
|
$rabbit_channel->close();
|
|
$connection->close();
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
}
|