作者 李美松

优化程序

... ... @@ -12,6 +12,8 @@ use Illuminate\Http\File;
class ProjectFilePDF extends Command
{
use CmdSignal;
/**
* The name and signature of the console command.
*
... ... @@ -28,10 +30,17 @@ class ProjectFilePDF extends Command
protected $AiccWechat;
protected $ProjectAssociation;
protected $DataFile;
protected $time;
protected $fileController;
// 最大支持5个进程
public $maxRunNumber = 50;
/**
* Create a new command instance.
*
... ... @@ -40,74 +49,119 @@ class ProjectFilePDF extends Command
public function __construct()
{
$this->AiccWechat = new ProjectAssociation();
$this->ProjectAssociation = 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完成');
# 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 0;
}
$key = $this->signature . '-' . $lists->id;
$count = redis_get($key);
/**
* 数据生成并保存
* @param array $items
* @return void
*/
public function dataPush(array $items)
{
foreach ($items as $item) {
$project_id = $item->project_id;
$user_id = $item->user_id;
$friend_id = $item->friend_id;
$project_id = $lists->project_id;
$user_id = $lists->user_id;
$friend_id = $lists->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);
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;
}
$file_path = $file_path['data'];
$isRes = $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
if (!$isRes) {
if ($count == 2) {
$lists->status = 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->status = 1;
$lists->save();
$this->debug_echo('项目文件数据保存成功!');
return 0;
}
/**
* Execute the console command.
*
* @return int
*/
// public function handle()
// {
// # 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->error('没有任务,等待中');
// sleep(60);
// 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;
// // 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生成失败!');
// return 0;
// }
// $file_path = $file_path['data'];
// $isRes = $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
// if (!$isRes) {
// if ($count == 2) {
// $lists->status = 2;
// $lists->save();
// $this->error('项目文件数据保存失败! - 其他原因 - ' . $key);
// } else {
// redis_add($key, $count + 1);
// $this->error('项目文件数据保存失败! - ' . $key);
// }
// }
// $lists->status = 1;
// $lists->save();
// $this->info('项目文件数据保存成功!');
// return 0;
// }
public function savePDF($html, $filename)
{
... ... @@ -137,17 +191,13 @@ class ProjectFilePDF extends Command
// 获取PDF内容
$pdfContent = $dompdf->output();
$fileController = new FileController();
$this->fileController->path = '/V6/PDF';
// 将PDF内容保存到文件
@file_put_contents($savePath, $pdfContent);
// 创建一个文件实例
$file = new File($savePath);
var_dump($file->getFilename());
exit();
return $fileController->single($file);
return $this->fileController->api_upload_single($file);
}
/**
... ... @@ -160,7 +210,7 @@ exit();
$html = '<html>';
$html .= '<body style="font-family:arial">';
$html .= '<h1>Hello, World!</h1>';
$html .= '<p>中文内容</p>';
$html .= '<p>中文内容ffffff</p>';
$html .= '</body>';
$html .= '</html>';
return $html;
... ...
... ... @@ -35,28 +35,85 @@ class WebsiteData extends Command
*/
public function __construct()
{
$this->time = date('y-d');
$this->time = date('Y-m');
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
// public function handle()
// {
// # 0 - 未推送
// # 1 - 已推送
// # 2 - 其他问题
// $status = 0;
// $lists = DataFile::query()->where('status', $status)
// ->where('created_at', 'like', $this->time . '%')->first();
// if (is_null($lists)) {
// $this->error('没有任务,等待中');
// sleep(30);
// return 0;
// }
//
// $key = $this->signature . '-' . $lists->id;
// $count = redis_get($key);
// $data = $lists;
//
// $url = env('AICC_URL');
// $msg = http_post($url, json_encode(compact('data')));
// $status_code = $msg['status'];
// if ($status_code != 200) {
// if ($count == 2) {
// $lists->status = 2;
// $lists->save();
// $this->error('项目文件数据保存失败! - 其他原因 - ' . $key);
// } else {
// redis_add($key, $count + 1);
// $this->error('项目文件数据保存失败! - ' . $key);
// }
// }
// $lists->status = 1;
// $lists->save();
// return 0;
// }
public function start(): int
{
# 0 - 未推送
# 1 - 已推送
# 2 - 其他问题
$status = 0;
$lists = DataFile::query()->where('status', $status)
->where('created_at', 'like', $this->time . '%')->first();
if (is_null($lists)) {
$this->debug_echo('没有任务,等待中');
sleep(60);
sleep(30);
return 0;
}
var_dump($lists);
exit();
$data = $lists['items'];
$key = $this->signature . '-' . $lists->id;
$count = redis_get($key);
$data = $lists;
$url = env('AICC_URL');
$msg = http_post($url, json_encode(compact('data')));
$status_code = $msg['status'];
if ($status_code != 200) {
if ($count == 2) {
$lists->status = 2;
$lists->save();
$this->debug_echo('项目文件数据保存失败! - 其他原因 - ' . $key);
} else {
redis_add($key, $count + 1);
$this->debug_echo('项目文件数据保存失败! - ' . $key);
}
}
$lists->status = 1;
$lists->save();
$this->info('项目文件数据保存成功!');
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
);
}
... ...
... ... @@ -106,9 +106,8 @@ class FileController
* @time :2023/6/17 16:32
*/
public function single(&$files){
// $hash = hash_file('md5', $files->getPathname());
$hash = hash_file('md5', $files->getPathname());
$name = $files->getClientOriginalName();
$name = $files->getFilename();
//查看文件是否存在
$fileModel = new File();
//查看图片是否已上传
... ... @@ -122,7 +121,6 @@ class FileController
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
// $fileName = uniqid().rand(10000,99999).'.'.$files->getExtension();
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
... ... @@ -134,11 +132,57 @@ class FileController
}
}
$this->saveMysql($fileModel,$files->getSize(),$files->getClientOriginalExtension(),$fileName,$hash,$this->upload_location,$files->getMimeType(),$name);
// $this->saveMysql($fileModel,$files->getSize(),$files->getExtension(),$fileName,$hash,$this->upload_location,$files->getMimeType(),$name);
return $this->response('资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name));
}
/**
* 接口上传单文件
* @param $files
* @return array
*/
public function api_upload_single(&$files)
{
$hash = hash_file('md5', $files->getPathname());
$name = $files->getFilename();
//查看文件是否存在
$fileModel = new File();
//查看图片是否已上传
$param = ['hash' => $hash, 'refer' => $this->param['refer'] ?? 0];
if (isset($this->cache['project_id']) && !empty($this->cache['project_id'])) {
$param['project_id'] = $this->cache['project_id'];
}
$file_hash = $fileModel->read($param);
if ($file_hash !== false) {
return [
'message' => '资源',
'code' => Code::SUCCESS,
'data' => $this->responseData($file_hash['path'], $name)
];
}
$url = $this->config['root'] . $this->path;
$fileName = uniqid() . rand(10000, 99999) . '.' . $files->getExtension();
//同步数据到cos
if ($this->upload_location == 1) {
$cosService = new CosService();
$cosService->uploadFile($files, $this->path, $fileName);
} else {
$res = $files->move($url, $fileName);
if ($res === false) {
return [
'message' => $files->getError(),
'code' => Code::USER_ERROR
];
}
}
$this->saveMysql($fileModel, $files->getSize(), $files->getExtension(), $fileName, $hash, $this->upload_location, $files->getMimeType(), $name);
return [
'message' => '资源',
'code' => Code::SUCCESS,
'data' => $this->responseData($this->path . '/' . $fileName, $name)
];
}
/**
* @remark :保存数据库
* @name :saveMysql
* @author :lyh
... ...