AddAgreementPdf.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. //crime harmless 直接通过
  87. if(in_array($agreement->agree_type,['harmless_content','crime_content'])){
  88. $agreement->audit_state = 1;
  89. }
  90. $agreement->pdf_url = $path.$pdf;
  91. $agreement->save();
  92. } else {
  93. // 处理错误
  94. echo "Command failed with return code: $return_var";
  95. }
  96. }
  97. /**
  98. * 团队协议生成PDF文件
  99. * @param $param
  100. * @return void
  101. */
  102. public static function addTenantAgreePdf($param)
  103. {
  104. $code = $param['code'];
  105. $url = $param['url'];
  106. $pdf = '/'.$code.'.pdf';
  107. $path = 'uploads/agreement_pdf/'.date('Ymd');
  108. if(!file_exists($path)){
  109. mkdir ($path,0777,true);
  110. }
  111. $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.$path.$pdf;
  112. $output = [];
  113. $return_var = 0;
  114. \exec($shell_ . ' > /dev/null 2>&1', $output, $return_var);
  115. if ($return_var === 0) {
  116. $agreement = TenantAgree::where('code', $code)->findOrEmpty();
  117. $agreement->pdf_url = $path.$pdf;
  118. $agreement->save();
  119. } else {
  120. // 处理错误
  121. echo "Command failed with return code: $return_var";
  122. }
  123. }
  124. public static function sendMq($code,$url):bool
  125. {
  126. $mq_config = Config::get('mq');
  127. // 连接到RabbitMQ服务
  128. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  129. $channel = $connection->channel();
  130. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  131. // 声明队列
  132. $queue = 'add_agreement';
  133. $channel->queue_declare($queue, true, true, false, true,false);
  134. // 发送消息
  135. $data = [
  136. 'code'=>$code,
  137. 'url'=>$url
  138. ];
  139. $messageBody = json_encode($data,JSON_UNESCAPED_UNICODE);
  140. $message = new AMQPMessage($messageBody);
  141. $channel->basic_publish($message, '', $queue);
  142. $channel->close();
  143. $connection->close();
  144. return true;
  145. }
  146. }