AddAgreementPdf.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. $output = [];
  57. $return_var = 0;
  58. \exec($shell_ . ' > /dev/null 2>&1', $output, $return_var);
  59. if ($return_var === 0) {
  60. $agreement = MasterWorkerAgree::where('code', $code)->findOrEmpty();
  61. $agreement->pdf_url = $path.$pdf;
  62. $agreement->save();
  63. } else {
  64. // 处理错误
  65. echo "Command failed with return code: $return_var";
  66. }
  67. }
  68. public static function sendMq($code,$url):bool
  69. {
  70. $mq_config = Config::get('mq');
  71. // 连接到RabbitMQ服务
  72. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  73. $channel = $connection->channel();
  74. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  75. // 声明队列
  76. $queue = 'add_agreement';
  77. $channel->queue_declare($queue, true, true, false, true,false);
  78. // 发送消息
  79. $data = [
  80. 'code'=>$code,
  81. 'url'=>$url
  82. ];
  83. $messageBody = json_encode($data,JSON_UNESCAPED_UNICODE);
  84. $message = new AMQPMessage($messageBody);
  85. $channel->basic_publish($message, '', $queue);
  86. $channel->close();
  87. $connection->close();
  88. return true;
  89. }
  90. }