AddAgreementPdf.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\common\command;
  3. use app\common\model\master_worker\MasterWorkerAgree;
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. use PhpAmqpLib\Message\AMQPMessage;
  6. use PhpAmqpLib\Wire\AMQPTable;
  7. use think\console\Command;
  8. use think\console\Input;
  9. use think\console\Output;
  10. use think\facade\Config;
  11. use think\facade\Log;
  12. /**
  13. * mq生成PDF协议队列
  14. * Class AddAgreementPdf
  15. * @package app\command
  16. */
  17. class AddAgreementPdf extends Command
  18. {
  19. protected function configure()
  20. {
  21. $this->setName('query_add_agreement')
  22. ->setDescription('mq生成PDF协议队列');
  23. }
  24. protected function execute(Input $input, Output $output)
  25. {
  26. $mq_config = Config::get('mq');
  27. // 连接到RabbitMQ服务
  28. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  29. $channel = $connection->channel();
  30. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  31. // 声明队列
  32. $queue = 'add_agreement';
  33. $channel->queue_declare($queue, true, true, false, true,false);
  34. //接收消息
  35. $callback = function($msg) {
  36. $param = json_decode($msg->body, true);
  37. self::addPdf($param);
  38. };
  39. $channel->basic_consume('add_agreement', '', false, true, false, false, $callback);
  40. while(count($channel->callbacks)) {
  41. $channel->wait();
  42. }
  43. $channel->close();
  44. $connection->close();
  45. }
  46. public static function addPdf($param)
  47. {
  48. $code = $param['code'];
  49. $url = $param['url'];
  50. $pdf = '/'.$code.'.pdf';
  51. $path = 'uploads/agreement_pdf/'.date('Ymd');
  52. if(!file_exists('./public/'.$path)){
  53. mkdir ('./public/'.$path,0777,true);
  54. }
  55. $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.'public/'.$path.$pdf;
  56. system($shell_);
  57. $agreement = MasterWorkerAgree::where('code', $code)->findOrEmpty();
  58. $agreement->pdf_url = $path.$pdf;
  59. $agreement->save();
  60. }
  61. public static function sendMq($code,$url):bool
  62. {
  63. $mq_config = Config::get('mq');
  64. // 连接到RabbitMQ服务
  65. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  66. $channel = $connection->channel();
  67. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  68. // 声明队列
  69. $queue = 'add_agreement';
  70. $channel->queue_declare($queue, true, true, false, true,false);
  71. // 发送消息
  72. $data = [
  73. 'code'=>$code,
  74. 'url'=>$url
  75. ];
  76. $messageBody = json_encode($data,JSON_UNESCAPED_UNICODE);
  77. $message = new AMQPMessage($messageBody);
  78. $channel->basic_publish($message, '', $queue);
  79. $channel->close();
  80. $connection->close();
  81. return true;
  82. }
  83. }