| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace app\adminapi\controller\crontab;
- use app\adminapi\controller\BaseAdminController;
- use app\common\model\master_worker\MasterWorkerAgree;
- use PhpAmqpLib\Connection\AMQPStreamConnection;
- use PhpAmqpLib\Message\AMQPMessage;
- use think\facade\Config;
- /**
- * 工程师服务合作协议控制器
- * Class AgreementPdfController
- * @package app\adminapi\controller\crontab
- */
- class AgreementPdfController extends BaseAdminController
- {
- public array $notNeedLogin = ['index'];
- public function index()
- {
- $mq_config = Config::get('mq');
- // 连接到RabbitMQ服务
- $connection = new AMQPStreamConnection($mq_config['host'], $mq_config['port'], $mq_config['username'], $mq_config['password'], $mq_config['vhost']);
- $channel = $connection->channel();
- $channel->exchange_declare('add_agreement','topic',true,true,true,false,false);
- // 声明队列
- $queue = 'add_agreement';
- $channel->queue_declare($queue, true, true, false, true,false);
- //接收消息
- $callback = function($msg) {
- $param = json_decode($msg->body, true);
- self::addPdf($param);
- };
- $channel->basic_consume('add_agreement', '', false, true, false, false, $callback);
- while(count($channel->callbacks)) {
- $channel->wait();
- }
- $channel->close();
- $connection->close();
- }
- public static function addPdf($param)
- {
- $code = $param['code'];
- $url = $param['url'];
- $pdf = '/'.$code.'.pdf';
- $path = 'uploads/agreement_pdf/'.date('Ymd');
- if(!file_exists('./public/'.$path)){
- mkdir ('./public/'.$path,0777,true);
- }
- $shell_ = 'wkhtmltopdf --page-height 297mm '.$url.' '.'public/'.$path.$pdf;
- $output = [];
- $return_var = 0;
- \exec($shell_ . ' > /dev/null 2>&1', $output, $return_var);
- if ($return_var === 0) {
- $agreement = MasterWorkerAgree::where('code', $code)->findOrEmpty();
- $agreement->pdf_url = $path.$pdf;
- $agreement->save();
- } else {
- // 处理错误
- echo "Command failed with return code: $return_var";
- }
- }
- }
|