购物车迁入
This commit is contained in:
135
app/command/RabbOrderPrint.php
Normal file
135
app/command/RabbOrderPrint.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use app\common\model\MqLog;
|
||||
use app\common\model\OrderInfo;
|
||||
use app\common\model\RabbitMqConfig;
|
||||
use ba\Random;
|
||||
use PhpAmqpLib\Connection\AMQPStreamConnection;
|
||||
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 RabbOrderPrint extends Command
|
||||
{
|
||||
protected static $defaultName = 'RabbOrderPrint';
|
||||
protected static $defaultDescription = 'RabbOrderPrint';
|
||||
|
||||
/**
|
||||
* @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 = RabbitMqConfig::$host;
|
||||
$port = RabbitMqConfig::$port;
|
||||
$user = RabbitMqConfig::$user;
|
||||
$password = RabbitMqConfig::$password;
|
||||
$queue = RabbitMqConfig::$queue_t . '-order.print.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){
|
||||
$date_time = date('Y-m-d H:i:s');
|
||||
$data = $msg->body;
|
||||
Log::write('收到消息--->' . $data . '--->' . $date_time . "\n");
|
||||
// 如果是数字。则打印订单
|
||||
$is_log = false;
|
||||
$curl_error = '';
|
||||
print_r($data . "\r\n");
|
||||
$order_id_t = strpos($data, '_');
|
||||
print_r($order_id_t . "嘿嘿\r\n");
|
||||
if($order_id_t !== false) {
|
||||
print_r($order_id_t . "\r\n");
|
||||
$order_id = substr($data, 0, $order_id_t);
|
||||
}else {
|
||||
print_r( "无下划线\r\n");
|
||||
$order_id = $data;
|
||||
}
|
||||
print_r($order_id . "\r\n");
|
||||
$order = OrderInfo::where(['id' => $order_id])->find();
|
||||
if($order) {
|
||||
$rand = 'cashier_order' . Random::build();
|
||||
$send_id = 'cashier_'. $order['shop_id'];
|
||||
// 收银机打印订单
|
||||
$snd_data = [
|
||||
'msg' => '收银机订单打印',
|
||||
'type' => 'cashier',
|
||||
'operate_type' => 'order_print',
|
||||
'data_type' => 'order',
|
||||
'status' => 1,
|
||||
'method' => 'sendToUid',
|
||||
'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=sendToUid&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::write('内部通讯结果(订单打印)--->' . $output . '通讯ID--->cashier_' .$order['shop_id']. '---' . date('Y-m-d H:i:s') . "\n");
|
||||
}else {
|
||||
$curl_error = 'order Data is empty';
|
||||
$is_log = true;
|
||||
}
|
||||
if($is_log) {
|
||||
MqLog::create([
|
||||
'queue' => '',
|
||||
'msg' => $data,
|
||||
'type' => 'orderPrint',
|
||||
'plat' => 'Cashier',
|
||||
'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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user