作者 李美松

优化程序

... ... @@ -12,6 +12,8 @@ use Illuminate\Http\File;
class ProjectFilePDF extends Command
{
use CmdSignal;
/**
* The name and signature of the console command.
*
... ... @@ -26,12 +28,17 @@ class ProjectFilePDF extends Command
*/
protected $description = '网站项目数据,生成PDF文件';
protected $AiccWechat;
protected ProjectAssociation $AiccWechat;
protected $DataFile;
protected DataFile $DataFile;
protected $time;
// 最大支持5个进程
public int $maxRunNumber = 50;
public FileController $fileController;
/**
* Create a new command instance.
*
... ... @@ -42,40 +49,27 @@ class ProjectFilePDF extends Command
$this->AiccWechat = new ProjectAssociation();
$this->DataFile = new DataFile();
$this->time = date("Y-m");
$this->fileController = new FileController();
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
public function start(): int
{
$data = $this->get_data();
# 详细数据
$items = $data['items'];
# 总分页
$totalPage = $data['totalPage'];
$this->dataPush($items);
if ($totalPage > 1) {
for ($page = 2; $page <= $totalPage; $page++) {
$da = $this->get_data();
$this->dataPush($da['items']);
}
}
$this->info('生成pdf完成');
$status = 1; # 1 - 正常, 0 - 禁用
$is_pdf = 0; # 0 - 未生成 1 - 已生成 2 - 其它问题
$lists = $this->DataFile::query()->where('status', $status)
->where('project_id', '!=', 0)
->where('friend_id', '!=', 0)
->where('user_id', '!=', 0)
->where('is_pdf', $is_pdf)->where('created_at', 'like', $this->time . '%')->first();
if (is_null($lists)) {
$this->debug_echo('没有任务,等待中');
sleep(60);
return 0;
}
/**
* 数据生成并保存
* @param array $items
* @return void
*/
public function dataPush(array $items)
{
foreach ($items as $item) {
$key = $this->signature . '-' . $lists->id;
$count = redis_get($key);
$item = $lists->getOriginal();
$project_id = $item->project_id;
$user_id = $item->user_id;
$friend_id = $item->friend_id;
... ... @@ -84,28 +78,25 @@ class ProjectFilePDF extends Command
$html = $this->html($project_data);
$filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);
$file_path = $this->savePDF($html, $filename);
var_dump($file_path);
$this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
if (empty($file_path)) {
$this->debug_echo('pdf生成失败!');
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 {
redis_add($key, $count + 1);
$this->debug_echo('项目文件数据保存失败! - ' . $key);
}
public function get_data($page = 1, $perPage = 20)
{
$data = $this->AiccWechat->allData($page, $perPage);
# 总条数
$total = $data['total'];
if (empty($total)) {
$this->error('暂无绑定AICC微信数据');
return 0;
}
# 详细数据
$items = $data['items'];
# 总分页
$totalPage = $data['totalPage'];
# 当前页
$currentPage = $data['currentPage'];
return compact('total', 'items', 'totalPage', 'currentPage');
$lists->is_pdf = 1;
$lists->save();
$this->debug_echo('项目文件数据保存成功!');
return 0;
}
public function savePDF($html, $filename)
... ... @@ -138,16 +129,13 @@ class ProjectFilePDF extends Command
// 获取PDF内容
$pdfContent = $dompdf->output();
$fileController = new FileController();
// 将PDF内容保存到文件
@file_put_contents($savePath, $pdfContent);
// 创建一个文件实例
$file = new File($savePath);
var_dump($file->getFilename());
exit();
return $fileController->single($file);
return $this->fileController->single($file);
}
/**
... ...
... ... @@ -24,10 +24,12 @@ class WebsiteData extends Command
protected $description = '向AICC推送数据';
// 最大支持5个进程
public $maxRunNumber = 50;
public int $maxRunNumber = 50;
protected $time;
protected $url;
/**
* Create a new command instance.
*
... ... @@ -35,13 +37,17 @@ class WebsiteData extends Command
*/
public function __construct()
{
$this->time = date('y-d');
$this->time = date('Y-m');
$this->url = env('AICC_URL');
parent::__construct();
}
public function start(): int
{
# 0 - 未推送
# 1 - 已推送
# 2 - 其他问题
$status = 0;
$lists = DataFile::query()->where('status', $status)
->where('created_at', 'like', $this->time . '%')->first();
... ... @@ -50,44 +56,23 @@ class WebsiteData extends Command
sleep(60);
return 0;
}
var_dump($lists);
exit();
$data = $lists['items'];
$url = env('AICC_URL');
$msg = http_post($url, json_encode(compact('data')));
$key = $this->signature . '-' . $lists->id;
$count = redis_get($key);
$isRes = http_post($this->url, json_encode(['data' => $lists->getOriginal()]));
if (!$isRes) {
if ($count == 2) {
$lists->is_pdf = 2;
$lists->save();
$this->debug_echo('项目文件数据推送失败! - 其他原因 - ' . $key);
} else {
redis_add($key, $count + 1);
$this->debug_echo('项目文件数据推送失败! - ' . $key);
}
/**
* Execute the console command.
*
* @return int
*/
// public function handle()
// {
// $DataFile = new DataFile();
// $data = $DataFile->allData();
// # 详细数据
// $items = $data['items'];
// # 总分页
// $totalPage = $data['totalPage'];
// $this->post_data($items);
// if ($totalPage > 1) {
// for ($page = 2; $page <= $totalPage; $page++) {
// $da = $DataFile->allData($page);
// $this->post_data($da['items']);
// }
// }
// $this->info('项目文件数据推送完成!');
// return 0;
// }
public function post_data($data)
{
$url = env('AICC_URL');
$msg = http_post($url, json_encode(compact('data')));
print_r($msg);
}
$lists->status = 1;
$lists->save();
$this->debug_echo('项目文件数据保存成功!');
return 0;
}
}
... ...
... ... @@ -9,6 +9,7 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Carbon;
use App\Models\File\File;
use Illuminate\Support\Facades\Redis;
define('HTTP_OPENAI_URL', 'http://openai.waimaoq.com/');
/**
... ... @@ -616,3 +617,29 @@ function getRouteMap($source,$source_id){
}
return $route;
}
function redis_get($key){
return Redis::connection()->client()->get($key);
}
function redis_del(...$key){
return Redis::connection()->client()->del(...$key);
}
function redis_set($key,$val,$ttl=3600){
return Redis::connection()->client()->set($key,$val,$ttl);
}
/**
* 添加缓存,存在则失败
* @param $key
* @param $val
* @param int $ttl
* @return mixed
* @author:dc
* @time 2023/10/25 9:48
*/
function redis_add($key,$val,$ttl=3600){
return Redis::connection()->client()->eval(
"return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])", [$key, $val, $ttl], 1
);
}
... ...