| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace app\common\command;
- use app\common\model\master_worker\MasterWorkerAgree;
- use app\common\model\tenant\TenantAgree;
- use PhpAmqpLib\Connection\AMQPStreamConnection;
- use PhpAmqpLib\Message\AMQPMessage;
- use PhpAmqpLib\Wire\AMQPTable;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Config;
- use think\facade\Log;
- /**
- * mq生成PDF协议队列
- * Class AddAgreementPdf
- * @package app\command
- */
- class AddAgreementPdf extends Command
- {
- protected function configure()
- {
- $this->setName('query_add_agreement')
- ->setDescription('mq生成PDF协议队列');
- }
- protected function execute(Input $input, Output $output)
- {
- $mq_config = Config::get('mq');
- // 连接到RabbitMQ服务
- $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
- $channel = $connection->channel();
- $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
- // 声明队列
- $queue = 'add_agreement';
- $channel->queue_declare($queue, true, true, false, true,false);
- //接收消息
- $callback = function($msg) {
- $param = json_decode($msg->body, true);
- self::addPdf($param);
- };
- $channel->basic_consume('add_agreement', '', false, true, false, false, $callback);
- while(count($channel->callbacks)) {
- $channel->wait();
- }
- $channel->close();
- $connection->close();
- }
- public static function notifyAgreement()
- {
- // $mq_config = Config::get('mq');
- // // 连接到RabbitMQ服务
- // $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
- // $channel = $connection->channel();
- // $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
- //
- // // 声明队列
- // $queue = 'add_agreement';
- // $channel->queue_declare($queue, true, true, false, true,false);
- //
- // //接收消息
- // $callback = function($msg) {
- // $param = json_decode($msg->body, true);
- // self::addPdf($param);
- // };
- // $channel->basic_consume('add_agreement', '', false, true, false, false, $callback);
- // while(count($channel->callbacks)) {
- // $channel->wait();
- // }
- // $channel->close();
- // $connection->close();
- }
- public static function addPdf($param)
- {
- $code = $param['code'];
- $url = $param['url'];
- $pdf = '/'.$code.'.pdf';
- $path = 'uploads/agreement_pdf/'.date('Ymd');
- if(!file_exists($path)){
- mkdir ($path,0777,true);
- }
- $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.$path.$pdf;
- $output = [];
- $return_var = 0;
- \exec($shell_ . ' > /dev/null 2>&1', $output, $return_var);
- if ($return_var === 0) {
- $agreement = MasterWorkerAgree::where('code', $code)->findOrEmpty();
- //crime harmless 直接通过
- if(in_array($agreement->agree_type,['harmless_content','crime_content'])){
- $agreement->audit_state = 1;
- }
- $agreement->pdf_url = $path.$pdf;
- $agreement->save();
- } else {
- // 处理错误
- echo "Command failed with return code: $return_var";
- }
- }
- /**
- * 团队协议生成PDF文件
- * @param $param
- * @return void
- */
- public static function addTenantAgreePdf($param)
- {
- $code = $param['code'];
- $url = $param['url'];
- $pdf = '/'.$code.'.pdf';
- $path = 'uploads/agreement_pdf/'.date('Ymd');
- if(!file_exists($path)){
- mkdir ($path,0777,true);
- }
- $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.$path.$pdf;
- $output = [];
- $return_var = 0;
- \exec($shell_ . ' > /dev/null 2>&1', $output, $return_var);
- if ($return_var === 0) {
- $agreement = TenantAgree::where('code', $code)->findOrEmpty();
- $agreement->pdf_url = $path.$pdf;
- $agreement->save();
- } else {
- // 处理错误
- echo "Command failed with return code: $return_var";
- }
- }
- public static function sendMq($code,$url):bool
- {
- $mq_config = Config::get('mq');
- // 连接到RabbitMQ服务
- $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
- $channel = $connection->channel();
- $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
- // 声明队列
- $queue = 'add_agreement';
- $channel->queue_declare($queue, true, true, false, true,false);
- // 发送消息
- $data = [
- 'code'=>$code,
- 'url'=>$url
- ];
- $messageBody = json_encode($data,JSON_UNESCAPED_UNICODE);
- $message = new AMQPMessage($messageBody);
- $channel->basic_publish($message, '', $queue);
- $channel->close();
- $connection->close();
- return true;
- }
- }
|