AddAgreementPdf.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\common\command;
  3. use app\common\model\master_worker\MasterWorkerAgree;
  4. use app\common\model\tenant\TenantAgree;
  5. use PhpAmqpLib\Connection\AMQPStreamConnection;
  6. use PhpAmqpLib\Message\AMQPMessage;
  7. use PhpAmqpLib\Wire\AMQPTable;
  8. use think\console\Command;
  9. use think\console\Input;
  10. use think\console\Output;
  11. use think\facade\Config;
  12. use think\facade\Log;
  13. /**
  14. * mq生成PDF协议队列
  15. * Class AddAgreementPdf
  16. * @package app\command
  17. */
  18. class AddAgreementPdf extends Command
  19. {
  20. protected function configure()
  21. {
  22. $this->setName('query_add_agreement')
  23. ->setDescription('mq生成PDF协议队列');
  24. }
  25. protected function execute(Input $input, Output $output)
  26. {
  27. $mq_config = Config::get('mq');
  28. // 连接到RabbitMQ服务
  29. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  30. $channel = $connection->channel();
  31. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  32. // 声明队列
  33. $queue = 'add_agreement';
  34. $channel->queue_declare($queue, true, true, false, true,false);
  35. //接收消息
  36. $callback = function($msg) {
  37. $param = json_decode($msg->body, true);
  38. self::addPdf($param);
  39. };
  40. $channel->basic_consume('add_agreement', '', false, true, false, false, $callback);
  41. while(count($channel->callbacks)) {
  42. $channel->wait();
  43. }
  44. $channel->close();
  45. $connection->close();
  46. }
  47. public static function notifyAgreement()
  48. {
  49. // $mq_config = Config::get('mq');
  50. // // 连接到RabbitMQ服务
  51. // $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  52. // $channel = $connection->channel();
  53. // $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  54. //
  55. // // 声明队列
  56. // $queue = 'add_agreement';
  57. // $channel->queue_declare($queue, true, true, false, true,false);
  58. //
  59. // //接收消息
  60. // $callback = function($msg) {
  61. // $param = json_decode($msg->body, true);
  62. // self::addPdf($param);
  63. // };
  64. // $channel->basic_consume('add_agreement', '', false, true, false, false, $callback);
  65. // while(count($channel->callbacks)) {
  66. // $channel->wait();
  67. // }
  68. // $channel->close();
  69. // $connection->close();
  70. }
  71. public static function addPdf($param)
  72. {
  73. $code = $param['code'];
  74. $url = $param['url'];
  75. $pdf = '/'.$code.'.pdf';
  76. $path = 'uploads/agreement_pdf/'.date('Ymd');
  77. if(!file_exists($path)){
  78. mkdir ($path,0777,true);
  79. }
  80. $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.$path.$pdf;
  81. $output = [];
  82. $return_var = 0;
  83. \exec($shell_ . ' > /dev/null 2>&1', $output, $return_var);
  84. if ($return_var === 0) {
  85. $agreement = MasterWorkerAgree::where('code', $code)->findOrEmpty();
  86. $agreement->pdf_url = $path.$pdf;
  87. $agreement->save();
  88. } else {
  89. // 处理错误
  90. echo "Command failed with return code: $return_var";
  91. }
  92. }
  93. /**
  94. * 团队协议生成PDF文件
  95. * @param $param
  96. * @return void
  97. */
  98. public static function addTenantAgreePdf($param)
  99. {
  100. $code = $param['code'];
  101. $url = $param['url'];
  102. $pdf = '/'.$code.'.pdf';
  103. $path = 'uploads/agreement_pdf/'.date('Ymd');
  104. if(!file_exists($path)){
  105. mkdir ($path,0777,true);
  106. }
  107. $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.$path.$pdf;
  108. $output = [];
  109. $return_var = 0;
  110. \exec($shell_ . ' > /dev/null 2>&1', $output, $return_var);
  111. if ($return_var === 0) {
  112. $agreement = TenantAgree::where('code', $code)->findOrEmpty();
  113. $agreement->pdf_url = $path.$pdf;
  114. $agreement->save();
  115. } else {
  116. // 处理错误
  117. echo "Command failed with return code: $return_var";
  118. }
  119. }
  120. public static function sendMq($code,$url):bool
  121. {
  122. $mq_config = Config::get('mq');
  123. // 连接到RabbitMQ服务
  124. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  125. $channel = $connection->channel();
  126. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  127. // 声明队列
  128. $queue = 'add_agreement';
  129. $channel->queue_declare($queue, true, true, false, true,false);
  130. // 发送消息
  131. $data = [
  132. 'code'=>$code,
  133. 'url'=>$url
  134. ];
  135. $messageBody = json_encode($data,JSON_UNESCAPED_UNICODE);
  136. $message = new AMQPMessage($messageBody);
  137. $channel->basic_publish($message, '', $queue);
  138. $channel->close();
  139. $connection->close();
  140. return true;
  141. }
  142. }