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; } }