AgreementPdfController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\adminapi\controller\crontab;
  3. use app\adminapi\controller\BaseAdminController;
  4. use app\common\model\master_worker\MasterWorkerAgree;
  5. use PhpAmqpLib\Connection\AMQPStreamConnection;
  6. use PhpAmqpLib\Message\AMQPMessage;
  7. use think\facade\Config;
  8. /**
  9. * 工程师服务合作协议控制器
  10. * Class AgreementPdfController
  11. * @package app\adminapi\controller\crontab
  12. */
  13. class AgreementPdfController extends BaseAdminController
  14. {
  15. public array $notNeedLogin = ['index'];
  16. public function index()
  17. {
  18. $mq_config = Config::get('mq');
  19. // 连接到RabbitMQ服务
  20. $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
  21. $channel = $connection->channel();
  22. $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
  23. // 声明队列
  24. $queue = 'add_agreement';
  25. $channel->queue_declare($queue, true, true, false, true,false);
  26. //接收消息
  27. $callback = function($msg) {
  28. $param = json_decode($msg->body, true);
  29. self::addPdf($param);
  30. };
  31. $channel->basic_consume('add_agreement', '', false, true, false, false, $callback);
  32. while(count($channel->callbacks)) {
  33. $channel->wait();
  34. }
  35. $channel->close();
  36. $connection->close();
  37. }
  38. public static function addPdf($param)
  39. {
  40. $code = $param['code'];
  41. $url = $param['url'];
  42. $pdf = '/'.$code.'.pdf';
  43. $path = 'uploads/agreement_pdf/'.date('Ymd');
  44. if(!file_exists('./public/'.$path)){
  45. mkdir ('./public/'.$path,0777,true);
  46. }
  47. $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.'public/'.$path.$pdf;
  48. $output = [];
  49. $return_var = 0;
  50. \exec($shell_ . ' > /dev/null 2>&1', $output, $return_var);
  51. if ($return_var === 0) {
  52. $agreement = MasterWorkerAgree::where('code', $code)->findOrEmpty();
  53. $agreement->pdf_url = $path.$pdf;
  54. $agreement->save();
  55. } else {
  56. // 处理错误
  57. echo "Command failed with return code: $return_var";
  58. }
  59. }
  60. }