AddAgreementPdf.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. while (true) {
  27. echo date('Y-m-d H:i:s') . PHP_EOL;
  28. }
  29. return false;
  30. $mq_config = Config::get('mq');
  31. // 连接到RabbitMQ服务
  32. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  33. $channel = $connection->channel();
  34. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  35. // 声明队列
  36. $queue = 'add_agreement';
  37. $channel->queue_declare($queue, true, true, false, true,false);
  38. //接收消息
  39. $callback = function($msg) {
  40. $param = json_decode($msg->body, true);
  41. self::addPdf($param);
  42. };
  43. $channel->basic_consume('add_agreement', '', false, true, false, false, $callback);
  44. while(count($channel->callbacks)) {
  45. $channel->wait();
  46. }
  47. $channel->close();
  48. $connection->close();
  49. }
  50. public static function addPdf($param)
  51. {
  52. $code = $param['code'];
  53. $url = $param['url'];
  54. $pdf = '/'.$code.'.pdf';
  55. $path = 'uploads/agreement_pdf/'.date('Ymd');
  56. if(!file_exists('./public/'.$path)){
  57. mkdir ('./public/'.$path,0777,true);
  58. }
  59. $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.'public/'.$path.$pdf;
  60. shell_exec($shell_);
  61. $agreement = MasterWorkerAgree::where('code', $code)->findOrEmpty();
  62. $agreement->pdf_url = $path.$pdf;
  63. $agreement->save();
  64. return true;
  65. }
  66. public static function sendMq($code,$url):bool
  67. {
  68. $mq_config = Config::get('mq');
  69. // 连接到RabbitMQ服务
  70. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  71. $channel = $connection->channel();
  72. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  73. // 声明队列
  74. $queue = 'add_agreement';
  75. $channel->queue_declare($queue, true, true, false, true,false);
  76. // 发送消息
  77. $data = [
  78. 'code'=>$code,
  79. 'url'=>$url
  80. ];
  81. $messageBody = json_encode($data,JSON_UNESCAPED_UNICODE);
  82. $message = new AMQPMessage($messageBody);
  83. $channel->basic_publish($message, '', $queue);
  84. $channel->close();
  85. $connection->close();
  86. return true;
  87. }
  88. }