This commit is contained in:
ASUS 2025-10-29 10:25:39 +08:00
parent ae24fea621
commit c5fab07c23
4 changed files with 268 additions and 249 deletions

View File

@ -13,52 +13,57 @@ use Webman\RedisQueue\Redis;
$user = config('cons.mq.user');
$password = config('cons.mq.password');
$queue = config('cons.mq.queue_t') . '-apply.sms.temp';
define('AMQP_DEBUG', true);
// 防止空闲时间断线必须设置心跳
$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){
$data = $msg->body;
Log::info('MQ收到消息[申请新短信模版/营销短信发送/微信模版消息]--->' . $data . "\n");
$arr = explode(',', $data);
if(isset($arr[2])) {
$type = $arr[2];
if($type == 'applySmsTemp') {
// 发给队列 短信模版审核
$a = Redis::send('apply.sms.temp', $data);
Log::info('消息队列投递结果----》' . $a);
}elseif ($type == 'sendMarkSms') {
// 发给队列 营销短信发送
Redis::send('send.mark.sms', $data);
}elseif ($type == 'sendWechatTemp') {
// 发给队列 发送微信模版消息
Redis::send('send.wechat.temp', $data);
}
}else {
Log::info('MQ收到消息[申请新短信模版/营销短信发送/微信模版消息]格式错误');
try {
define('AMQP_DEBUG', true);
// 防止空闲时间断线必须设置心跳
$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);
$callback = function ($msg) use ($queue){
$data = $msg->body;
Log::info('MQ收到消息[申请新短信模版/营销短信发送/微信模版消息]--->' . $data . "\n");
$arr = explode(',', $data);
if(isset($arr[2])) {
$type = $arr[2];
if($type == 'applySmsTemp') {
// 发给队列 短信模版审核
$a = Redis::send('apply.sms.temp', $data);
Log::info('消息队列投递结果----》' . $a);
}elseif ($type == 'sendMarkSms') {
// 发给队列 营销短信发送
Redis::send('send.mark.sms', $data);
}elseif ($type == 'sendWechatTemp') {
// 发给队列 发送微信模版消息
Redis::send('send.wechat.temp', $data);
}
$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('apply.sms.temp-MQ准备调用wait');
$rabbit_channel->wait();
}else {
Log::info('MQ收到消息[申请新短信模版/营销短信发送/微信模版消息]格式错误');
}
$rabbit_channel->close();
$connection->close();
$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('apply.sms.temp-MQ准备调用wait');
$rabbit_channel->wait();
}
$rabbit_channel->close();
$connection->close();
}catch (Exception $exception) {
Log::info('apply.sms.temp-MQ错误' . $exception->getMessage());
}

View File

@ -7,44 +7,48 @@ use PhpAmqpLib\Connection\AMQPStreamConnection;
use support\Log;
use Webman\RedisQueue\Redis;
// 生日有礼
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') . '-birthday.gift.sms';
// 防止空闲时间断线必须设置心跳
$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]
]);
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') . '-birthday.gift.sms';
// 防止空闲时间断线必须设置心跳
$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);
$callback = function ($msg) use ($queue){
$date_time = date('Y-m-d H:i:s');
$data = $msg->body;
Log::info('MQ收到消息[生日有礼]--->' . $data . '--->' . $date_time . "\n");
// 发给队列
Redis::send('birthday.gift.sms', $data);
$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('birthday.gift.sms-MQ准备调用wait');
$rabbit_channel->wait();
}
$rabbit_channel->close();
$connection->close();
}catch (Exception $exception) {
Log::info('birthday.gift.sms-MQ错误' . $exception->getMessage());
}
$callback = function ($msg) use ($queue){
$date_time = date('Y-m-d H:i:s');
$data = $msg->body;
Log::info('MQ收到消息[生日有礼]--->' . $data . '--->' . $date_time . "\n");
// 发给队列
Redis::send('birthday.gift.sms', $data);
$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('birthday.gift.sms-MQ准备调用wait');
$rabbit_channel->wait();
}
$rabbit_channel->close();
$connection->close();

View File

@ -13,94 +13,102 @@ use support\Log;
$user = config('cons.mq.user');
$password = config('cons.mq.password');
$queue = config('cons.mq.queue_t') . '-order.print.queue';
define('AMQP_DEBUG', true);
// 防止空闲时间断线必须设置心跳
$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 = '';
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 = Db::table('tb_order_info')->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 .'&params=' . $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--->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) {
Db::table('tb_mq_log')->insert([
'queue' => $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()) {
Log::info('order.print.queue-MQ准备调用wait');
$rabbit_channel->wait();
try {
define('AMQP_DEBUG', true);
// 防止空闲时间断线必须设置心跳
$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 = '';
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;
}
$rabbit_channel->close();
$connection->close();
print_r($order_id . "\r\n");
$order = Db::table('tb_order_info')->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 .'&params=' . $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--->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) {
Db::table('tb_mq_log')->insert([
'queue' => $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()) {
Log::info('order.print.queue-MQ准备调用wait');
$rabbit_channel->wait();
}
$rabbit_channel->close();
$connection->close();
}catch (Exception $exception) {
Log::info('order.print.queue-MQ错误' . $exception->getMessage());
}

View File

@ -8,83 +8,85 @@ use PhpAmqpLib\Connection\AMQPStreamConnection;
use support\think\Db;
use support\Log;
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') . '-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]
]);
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') . '-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);
$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 .'&params=' . $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,
]);
$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 .'&params=' . $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()) {
Log::info('product.info.change.queue-MQ准备调用wait');
$rabbit_channel->wait();
}
$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());
}
$rabbit_channel->close();
$connection->close();