|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
namespace App\Console\Commands\GeneratePDF;
|
|
|
|
|
|
|
|
use App\Http\Controllers\File\FileController;
|
|
|
|
use App\Models\File\DataFile;
|
|
|
|
use App\Models\File\PdfFile;
|
|
|
|
use App\Models\ProjectAssociation\ProjectAssociation;
|
|
|
|
use App\Services\CosService;
|
|
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
...
|
...
|
@@ -12,7 +11,7 @@ use Illuminate\Http\File; |
|
|
|
|
|
|
|
class ProjectFilePDF extends Command
|
|
|
|
{
|
|
|
|
use CmdSignal;
|
|
|
|
// use CmdSignal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
...
|
...
|
@@ -28,14 +27,8 @@ class ProjectFilePDF extends Command |
|
|
|
*/
|
|
|
|
protected $description = '网站项目数据,生成PDF文件';
|
|
|
|
|
|
|
|
protected $ProjectAssociation;
|
|
|
|
|
|
|
|
protected $DataFile;
|
|
|
|
|
|
|
|
protected $time;
|
|
|
|
|
|
|
|
protected $fileController;
|
|
|
|
|
|
|
|
protected $CosService;
|
|
|
|
|
|
|
|
// 最大支持5个进程
|
|
...
|
...
|
@@ -48,71 +41,89 @@ class ProjectFilePDF extends Command |
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->ProjectAssociation = new ProjectAssociation();
|
|
|
|
$this->DataFile = new DataFile();
|
|
|
|
$this->time = date("Y-m");
|
|
|
|
$this->fileController = new FileController();
|
|
|
|
$this->CosService = new CosService();
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
// public function handle()
|
|
|
|
// {
|
|
|
|
// $project_data = [];
|
|
|
|
// $html = $this->html($project_data);
|
|
|
|
// $filename = hash('md5', $this->time . '-' . '$project_id' . '-' . '$friend_id' . '-' . '$user_id');
|
|
|
|
// $this->savePDF($html, $filename);
|
|
|
|
//// $this->testStreamPdf($html, $filename);
|
|
|
|
//// $file_path = $this->savePDF2($html, $filename);
|
|
|
|
// return 0;
|
|
|
|
// }
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
// 开始时间
|
|
|
|
$startTime = microtime(true);
|
|
|
|
$html = $this->html([]);
|
|
|
|
$filename = hash('md5', $this->time . '-' . random_int(100000, 999999));
|
|
|
|
$this->savePDF($html, $filename);
|
|
|
|
|
|
|
|
// 结束时间
|
|
|
|
$endTime = microtime(true);
|
|
|
|
|
|
|
|
// 计算执行时间
|
|
|
|
$executionTime = ($endTime - $startTime);
|
|
|
|
|
|
|
|
// 输出执行时间
|
|
|
|
var_dump("程序执行时间: " . $executionTime . " 秒");
|
|
|
|
// return $this->main();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function start(): int
|
|
|
|
{
|
|
|
|
# 0 - 未生成
|
|
|
|
# 1 - 已生成
|
|
|
|
# 2 - 其它问题
|
|
|
|
$is_pdf = 0;
|
|
|
|
$lists = $this->ProjectAssociation::query()->where('is_pdf', $is_pdf)
|
|
|
|
->where('project_id', '!=', 0)
|
|
|
|
->where('friend_id', '!=', 0)
|
|
|
|
->where('user_id', '!=', 0)
|
|
|
|
->where('created_at', 'like', $this->time . '%')->first();
|
|
|
|
|
|
|
|
if (is_null($lists)) {
|
|
|
|
$this->debug_echo('没有任务,等待中');
|
|
|
|
sleep(30);
|
|
|
|
return $this->main();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function main()
|
|
|
|
{
|
|
|
|
$list = PdfFile::query()->whereIsPdf(PdfFile::GENERATE_NOT_PDF)->first();
|
|
|
|
|
|
|
|
if (is_null($list)) {
|
|
|
|
$this->error('没有任务,等待中');
|
|
|
|
sleep(60);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$pid = $list->pid;
|
|
|
|
if (empty($pid)) {
|
|
|
|
$this->error('数据错误');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$isExists = ProjectAssociation::query()->whereId($pid)->whereStatus(ProjectAssociation::STATUS_NORMAL)->first();
|
|
|
|
if (is_null($isExists)) {
|
|
|
|
$this->error('数据已被禁用 —— ' . $pid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$key = $this->signature . '-' . $lists->id;
|
|
|
|
$count = redis_get($key);
|
|
|
|
|
|
|
|
$project_id = $lists->project_id;
|
|
|
|
$user_id = $lists->user_id;
|
|
|
|
$friend_id = $lists->friend_id;
|
|
|
|
$key = 'generate_pdf_' . $list->id;
|
|
|
|
$count = (int)redis_get($key) ?: 0;
|
|
|
|
$project_id = $isExists->project_id;
|
|
|
|
$user_id = $isExists->user_id;
|
|
|
|
$friend_id = $isExists->friend_id;
|
|
|
|
// todo 根据项目查询数据
|
|
|
|
$project_data = [];
|
|
|
|
$html = $this->html($project_data);
|
|
|
|
$filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);
|
|
|
|
|
|
|
|
$file_path = $this->savePDF($html, $filename);
|
|
|
|
if (empty($file_path)) {
|
|
|
|
$this->debug_echo('pdf生成失败!');
|
|
|
|
if ($count == 2) {
|
|
|
|
$list->is_pdf = PdfFile::GENERATE_OTHER_PDF;
|
|
|
|
$list->save();
|
|
|
|
$this->error('项目文件数据保存失败! - 其他原因 - ' . $key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$isRes = $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
|
|
|
|
if (!$isRes) {
|
|
|
|
if ($count == 2) {
|
|
|
|
$lists->is_pdf = 2;
|
|
|
|
$lists->save();
|
|
|
|
$this->debug_echo('项目文件数据保存失败! - 其他原因 - ' . $key);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$file_path = $this->savePDF($html, $filename);
|
|
|
|
if (empty($file_path)) {
|
|
|
|
redis_set($key, $count + 1);
|
|
|
|
$this->debug_echo('项目文件数据保存失败! - ' . $key);
|
|
|
|
}
|
|
|
|
$this->error('pdf生成失败!');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$lists->is_pdf = 1;
|
|
|
|
$lists->save();
|
|
|
|
|
|
|
|
// 保存文件路径
|
|
|
|
$list->file_path = $file_path;
|
|
|
|
$list->is_pdf = PdfFile::GENERATE_PDF;
|
|
|
|
$list->save();
|
|
|
|
$this->info('项目文件数据保存成功!');
|
|
|
|
return 0;
|
|
|
|
}
|
|
...
|
...
|
@@ -131,18 +142,22 @@ class ProjectFilePDF extends Command |
|
|
|
}
|
|
|
|
// 指定保存路径和文件名
|
|
|
|
$savePath = $pdf_path . $filename . '.pdf';
|
|
|
|
|
|
|
|
if (file_exists($savePath)) {
|
|
|
|
echo '文件已经存在';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = '/PDF/' . $this->time;
|
|
|
|
$pdf = PDF::loadHTML($html);
|
|
|
|
$pdf->save($savePath);
|
|
|
|
|
|
|
|
$path = '/V6/PDF/' . $this->time;
|
|
|
|
// 创建一个文件实例
|
|
|
|
$file = new File($savePath);
|
|
|
|
return $this->CosService->uploadFile($file, $path, $filename . '.pdf');
|
|
|
|
// 获取二进制文件
|
|
|
|
$binary = $pdf->output(['compres' => 0]);
|
|
|
|
// 将文件保存到本地
|
|
|
|
// $pdf->save($savePath);
|
|
|
|
// $binary = new File($savePath);
|
|
|
|
|
|
|
|
return $this->CosService->uploadFile($binary, $path, $filename . '.pdf');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -153,6 +168,6 @@ class ProjectFilePDF extends Command |
|
|
|
protected function html($item)
|
|
|
|
{
|
|
|
|
$font_path = storage_path('fonts\msyh.ttf');
|
|
|
|
return '<html><head><title>Laravel</title><meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'/><style>body{ font-family: \'msyh\'; } @font-face { font-family: \'msyh\'; font-style: normal; font-weight: normal; src: url(' . $font_path . ') format(\'truetype\'); }</style></head><body><div class=\'container\'><div class=\'content\'><p style=\'font-family: msyh, DejaVu Sans,sans-serif;\'>献给母亲的爱</p><div style=\'font-family: msyh, DejaVu Sans,sans-serif;\' class=\'title\'>Laravel 5中文测试</div><div class=\'title\'>测试三askjdhfkjasdhf</div></div></div></body></html>';
|
|
|
|
return '<html><head><title>Laravel</title><meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'/><style>body{ font-family: \'msyh\'; } @font-face { font-family: \'msyh\'; font-style: normal; font-weight: normal; src: url(' . $font_path . ') format(\'truetype\'); }</style></head><body><div class=\'container\'><div class=\'content\'><p style=\'font-family: msyh, DejaVu Sans,sans-serif;\'>献给母亲的爱</p><div style=\'font-family: msyh, DejaVu Sans,sans-serif;\' class=\'title\'>Laravel 5中文测试sdsd</div><div class=\'title\'>测试三askjdhfkjasdhf</div></div></div></body></html>';
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|