作者 李美松

优化程序

@@ -12,6 +12,8 @@ use Illuminate\Http\File; @@ -12,6 +12,8 @@ use Illuminate\Http\File;
12 12
13 class ProjectFilePDF extends Command 13 class ProjectFilePDF extends Command
14 { 14 {
  15 + use CmdSignal;
  16 +
15 /** 17 /**
16 * The name and signature of the console command. 18 * The name and signature of the console command.
17 * 19 *
@@ -28,10 +30,17 @@ class ProjectFilePDF extends Command @@ -28,10 +30,17 @@ class ProjectFilePDF extends Command
28 30
29 protected $AiccWechat; 31 protected $AiccWechat;
30 32
  33 + protected $ProjectAssociation;
  34 +
31 protected $DataFile; 35 protected $DataFile;
32 36
33 protected $time; 37 protected $time;
34 38
  39 + protected $fileController;
  40 +
  41 + // 最大支持5个进程
  42 + public $maxRunNumber = 50;
  43 +
35 /** 44 /**
36 * Create a new command instance. 45 * Create a new command instance.
37 * 46 *
@@ -39,74 +48,119 @@ class ProjectFilePDF extends Command @@ -39,74 +48,119 @@ class ProjectFilePDF extends Command
39 */ 48 */
40 public function __construct() 49 public function __construct()
41 { 50 {
42 - $this->AiccWechat = new ProjectAssociation();  
43 - $this->DataFile = new DataFile();  
44 - $this->time = date("Y-m"); 51 + $this->AiccWechat = new ProjectAssociation();
  52 + $this->ProjectAssociation = new ProjectAssociation();
  53 + $this->DataFile = new DataFile();
  54 + $this->time = date("Y-m");
  55 + $this->fileController = new FileController();
45 parent::__construct(); 56 parent::__construct();
46 } 57 }
47 58
48 - /**  
49 - * Execute the console command.  
50 - *  
51 - * @return int  
52 - */  
53 - public function handle() 59 + public function start(): int
54 { 60 {
55 - $data = $this->get_data();  
56 - # 详细数据  
57 - $items = $data['items'];  
58 - # 总分页  
59 - $totalPage = $data['totalPage'];  
60 - $this->dataPush($items);  
61 - if ($totalPage > 1) {  
62 - for ($page = 2; $page <= $totalPage; $page++) {  
63 - $da = $this->get_data();  
64 - $this->dataPush($da['items']); 61 + # 0 - 未生成
  62 + # 1 - 已生成
  63 + # 2 - 其它问题
  64 + $is_pdf = 0;
  65 + $lists = $this->ProjectAssociation::query()->where('is_pdf', $is_pdf)
  66 + ->where('project_id', '!=', 0)
  67 + ->where('friend_id', '!=', 0)
  68 + ->where('user_id', '!=', 0)
  69 + ->where('created_at', 'like', $this->time . '%')->first();
  70 +
  71 + if (is_null($lists)) {
  72 + $this->debug_echo('没有任务,等待中');
  73 + sleep(30);
  74 + return 0;
  75 + }
  76 + $key = $this->signature . '-' . $lists->id;
  77 + $count = redis_get($key);
  78 +
  79 + $project_id = $lists->project_id;
  80 + $user_id = $lists->user_id;
  81 + $friend_id = $lists->friend_id;
  82 + // todo 根据项目查询数据
  83 + $project_data = [];
  84 + $html = $this->html($project_data);
  85 + $filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);
  86 +
  87 + $file_path = $this->savePDF($html, $filename);
  88 + if (empty($file_path)) {
  89 + $this->debug_echo('pdf生成失败!');
  90 + return 0;
  91 + }
  92 + $file_path = $file_path['data'];
  93 + $isRes = $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
  94 + if (!$isRes) {
  95 + if ($count == 2) {
  96 + $lists->status = 2;
  97 + $lists->save();
  98 + $this->debug_echo('项目文件数据保存失败! - 其他原因 - ' . $key);
  99 + } else {
  100 + redis_add($key, $count + 1);
  101 + $this->debug_echo('项目文件数据保存失败! - ' . $key);
65 } 102 }
66 } 103 }
67 - $this->info('生成pdf完成'); 104 + $lists->status = 1;
  105 + $lists->save();
  106 + $this->debug_echo('项目文件数据保存成功!');
68 return 0; 107 return 0;
69 } 108 }
70 109
71 /** 110 /**
72 - * 数据生成并保存  
73 - * @param array $items  
74 - * @return void 111 + * Execute the console command.
  112 + *
  113 + * @return int
75 */ 114 */
76 - public function dataPush(array $items)  
77 - {  
78 - foreach ($items as $item) {  
79 - $project_id = $item->project_id;  
80 - $user_id = $item->user_id;  
81 - $friend_id = $item->friend_id;  
82 - // todo 根据项目查询数据  
83 - $project_data = [];  
84 - $html = $this->html($project_data);  
85 - $filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);  
86 - $file_path = $this->savePDF($html, $filename);  
87 - var_dump($file_path);  
88 - $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);  
89 - }  
90 - }  
91 -  
92 -  
93 - public function get_data($page = 1, $perPage = 20)  
94 - {  
95 - $data = $this->AiccWechat->allData($page, $perPage);  
96 - # 总条数  
97 - $total = $data['total'];  
98 - if (empty($total)) {  
99 - $this->error('暂无绑定AICC微信数据');  
100 - return 0;  
101 - }  
102 - # 详细数据  
103 - $items = $data['items'];  
104 - # 总分页  
105 - $totalPage = $data['totalPage'];  
106 - # 当前页  
107 - $currentPage = $data['currentPage'];  
108 - return compact('total', 'items', 'totalPage', 'currentPage');  
109 - } 115 +// public function handle()
  116 +// {
  117 +// # 0 - 未生成
  118 +// # 1 - 已生成
  119 +// # 2 - 其它问题
  120 +// $is_pdf = 0;
  121 +// $lists = $this->ProjectAssociation::query()->where('is_pdf', $is_pdf)
  122 +// ->where('project_id', '!=', 0)
  123 +// ->where('friend_id', '!=', 0)
  124 +// ->where('user_id', '!=', 0)
  125 +// ->where('created_at', 'like', $this->time . '%')->first();
  126 +//
  127 +// if (is_null($lists)) {
  128 +// $this->error('没有任务,等待中');
  129 +// sleep(60);
  130 +// return 0;
  131 +// }
  132 +// $key = $this->signature . '-' . $lists->id;
  133 +// $count = redis_get($key);
  134 +// $project_id = $lists->project_id;
  135 +// $user_id = $lists->user_id;
  136 +// $friend_id = $lists->friend_id;
  137 +// // todo 根据项目查询数据
  138 +// $project_data = [];
  139 +// $html = $this->html($project_data);
  140 +// $filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);
  141 +//
  142 +// $file_path = $this->savePDF($html, $filename);
  143 +// if (empty($file_path)) {
  144 +// $this->debug_echo('pdf生成失败!');
  145 +// return 0;
  146 +// }
  147 +// $file_path = $file_path['data'];
  148 +// $isRes = $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
  149 +// if (!$isRes) {
  150 +// if ($count == 2) {
  151 +// $lists->status = 2;
  152 +// $lists->save();
  153 +// $this->error('项目文件数据保存失败! - 其他原因 - ' . $key);
  154 +// } else {
  155 +// redis_add($key, $count + 1);
  156 +// $this->error('项目文件数据保存失败! - ' . $key);
  157 +// }
  158 +// }
  159 +// $lists->status = 1;
  160 +// $lists->save();
  161 +// $this->info('项目文件数据保存成功!');
  162 +// return 0;
  163 +// }
110 164
111 public function savePDF($html, $filename) 165 public function savePDF($html, $filename)
112 { 166 {
@@ -136,18 +190,14 @@ class ProjectFilePDF extends Command @@ -136,18 +190,14 @@ class ProjectFilePDF extends Command
136 $dompdf->render(); 190 $dompdf->render();
137 191
138 // 获取PDF内容 192 // 获取PDF内容
139 - $pdfContent = $dompdf->output();  
140 -  
141 - $fileController = new FileController(); 193 + $pdfContent = $dompdf->output();
  194 + $this->fileController->path = '/V6/PDF';
142 195
143 // 将PDF内容保存到文件 196 // 将PDF内容保存到文件
144 @file_put_contents($savePath, $pdfContent); 197 @file_put_contents($savePath, $pdfContent);
145 -  
146 // 创建一个文件实例 198 // 创建一个文件实例
147 $file = new File($savePath); 199 $file = new File($savePath);
148 -var_dump($file->getFilename());  
149 -exit();  
150 - return $fileController->single($file); 200 + return $this->fileController->api_upload_single($file);
151 } 201 }
152 202
153 /** 203 /**
@@ -160,7 +210,7 @@ exit(); @@ -160,7 +210,7 @@ exit();
160 $html = '<html>'; 210 $html = '<html>';
161 $html .= '<body style="font-family:arial">'; 211 $html .= '<body style="font-family:arial">';
162 $html .= '<h1>Hello, World!</h1>'; 212 $html .= '<h1>Hello, World!</h1>';
163 - $html .= '<p>中文内容</p>'; 213 + $html .= '<p>中文内容ffffff</p>';
164 $html .= '</body>'; 214 $html .= '</body>';
165 $html .= '</html>'; 215 $html .= '</html>';
166 return $html; 216 return $html;
@@ -35,28 +35,85 @@ class WebsiteData extends Command @@ -35,28 +35,85 @@ class WebsiteData extends Command
35 */ 35 */
36 public function __construct() 36 public function __construct()
37 { 37 {
38 - $this->time = date('y-d'); 38 + $this->time = date('Y-m');
39 parent::__construct(); 39 parent::__construct();
40 } 40 }
41 41
  42 + /**
  43 + * Execute the console command.
  44 + *
  45 + * @return int
  46 + */
  47 +// public function handle()
  48 +// {
  49 +// # 0 - 未推送
  50 +// # 1 - 已推送
  51 +// # 2 - 其他问题
  52 +// $status = 0;
  53 +// $lists = DataFile::query()->where('status', $status)
  54 +// ->where('created_at', 'like', $this->time . '%')->first();
  55 +// if (is_null($lists)) {
  56 +// $this->error('没有任务,等待中');
  57 +// sleep(30);
  58 +// return 0;
  59 +// }
  60 +//
  61 +// $key = $this->signature . '-' . $lists->id;
  62 +// $count = redis_get($key);
  63 +// $data = $lists;
  64 +//
  65 +// $url = env('AICC_URL');
  66 +// $msg = http_post($url, json_encode(compact('data')));
  67 +// $status_code = $msg['status'];
  68 +// if ($status_code != 200) {
  69 +// if ($count == 2) {
  70 +// $lists->status = 2;
  71 +// $lists->save();
  72 +// $this->error('项目文件数据保存失败! - 其他原因 - ' . $key);
  73 +// } else {
  74 +// redis_add($key, $count + 1);
  75 +// $this->error('项目文件数据保存失败! - ' . $key);
  76 +// }
  77 +// }
  78 +// $lists->status = 1;
  79 +// $lists->save();
  80 +// return 0;
  81 +// }
42 82
43 public function start(): int 83 public function start(): int
44 { 84 {
  85 + # 0 - 未推送
  86 + # 1 - 已推送
  87 + # 2 - 其他问题
45 $status = 0; 88 $status = 0;
46 $lists = DataFile::query()->where('status', $status) 89 $lists = DataFile::query()->where('status', $status)
47 ->where('created_at', 'like', $this->time . '%')->first(); 90 ->where('created_at', 'like', $this->time . '%')->first();
48 if (is_null($lists)) { 91 if (is_null($lists)) {
49 $this->debug_echo('没有任务,等待中'); 92 $this->debug_echo('没有任务,等待中');
50 - sleep(60); 93 + sleep(30);
51 return 0; 94 return 0;
52 } 95 }
53 - var_dump($lists);  
54 - exit();  
55 - $data = $lists['items'];  
56 -  
57 - $url = env('AICC_URL');  
58 - $msg = http_post($url, json_encode(compact('data')));  
59 96
  97 + $key = $this->signature . '-' . $lists->id;
  98 + $count = redis_get($key);
  99 + $data = $lists;
  100 + $url = env('AICC_URL');
  101 + $msg = http_post($url, json_encode(compact('data')));
  102 + $status_code = $msg['status'];
  103 + if ($status_code != 200) {
  104 + if ($count == 2) {
  105 + $lists->status = 2;
  106 + $lists->save();
  107 + $this->debug_echo('项目文件数据保存失败! - 其他原因 - ' . $key);
  108 + } else {
  109 + redis_add($key, $count + 1);
  110 + $this->debug_echo('项目文件数据保存失败! - ' . $key);
  111 + }
  112 + }
  113 + $lists->status = 1;
  114 + $lists->save();
  115 + $this->info('项目文件数据保存成功!');
  116 + return 0;
60 } 117 }
61 118
62 /** 119 /**
@@ -9,6 +9,7 @@ use GuzzleHttp\Client; @@ -9,6 +9,7 @@ use GuzzleHttp\Client;
9 use GuzzleHttp\Exception\GuzzleException; 9 use GuzzleHttp\Exception\GuzzleException;
10 use Illuminate\Support\Carbon; 10 use Illuminate\Support\Carbon;
11 use App\Models\File\File; 11 use App\Models\File\File;
  12 +use Illuminate\Support\Facades\Redis;
12 13
13 define('HTTP_OPENAI_URL', 'http://openai.waimaoq.com/'); 14 define('HTTP_OPENAI_URL', 'http://openai.waimaoq.com/');
14 /** 15 /**
@@ -616,3 +617,29 @@ function getRouteMap($source,$source_id){ @@ -616,3 +617,29 @@ function getRouteMap($source,$source_id){
616 } 617 }
617 return $route; 618 return $route;
618 } 619 }
  620 +
  621 +function redis_get($key){
  622 + return Redis::connection()->client()->get($key);
  623 +}
  624 +function redis_del(...$key){
  625 + return Redis::connection()->client()->del(...$key);
  626 +}
  627 +
  628 +function redis_set($key,$val,$ttl=3600){
  629 + return Redis::connection()->client()->set($key,$val,$ttl);
  630 +}
  631 +
  632 +/**
  633 + * 添加缓存,存在则失败
  634 + * @param $key
  635 + * @param $val
  636 + * @param int $ttl
  637 + * @return mixed
  638 + * @author:dc
  639 + * @time 2023/10/25 9:48
  640 + */
  641 +function redis_add($key,$val,$ttl=3600){
  642 + return Redis::connection()->client()->eval(
  643 + "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])", [$key, $val, $ttl], 1
  644 + );
  645 +}
@@ -106,9 +106,8 @@ class FileController @@ -106,9 +106,8 @@ class FileController
106 * @time :2023/6/17 16:32 106 * @time :2023/6/17 16:32
107 */ 107 */
108 public function single(&$files){ 108 public function single(&$files){
109 -// $hash = hash_file('md5', $files->getPathname()); 109 + $hash = hash_file('md5', $files->getPathname());
110 $name = $files->getClientOriginalName(); 110 $name = $files->getClientOriginalName();
111 - $name = $files->getFilename();  
112 //查看文件是否存在 111 //查看文件是否存在
113 $fileModel = new File(); 112 $fileModel = new File();
114 //查看图片是否已上传 113 //查看图片是否已上传
@@ -122,7 +121,6 @@ class FileController @@ -122,7 +121,6 @@ class FileController
122 } 121 }
123 $url = $this->config['root'].$this->path; 122 $url = $this->config['root'].$this->path;
124 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 123 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
125 -// $fileName = uniqid().rand(10000,99999).'.'.$files->getExtension();  
126 //同步数据到cos 124 //同步数据到cos
127 if($this->upload_location == 1){ 125 if($this->upload_location == 1){
128 $cosService = new CosService(); 126 $cosService = new CosService();
@@ -134,11 +132,57 @@ class FileController @@ -134,11 +132,57 @@ class FileController
134 } 132 }
135 } 133 }
136 $this->saveMysql($fileModel,$files->getSize(),$files->getClientOriginalExtension(),$fileName,$hash,$this->upload_location,$files->getMimeType(),$name); 134 $this->saveMysql($fileModel,$files->getSize(),$files->getClientOriginalExtension(),$fileName,$hash,$this->upload_location,$files->getMimeType(),$name);
137 -// $this->saveMysql($fileModel,$files->getSize(),$files->getExtension(),$fileName,$hash,$this->upload_location,$files->getMimeType(),$name);  
138 return $this->response('资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name)); 135 return $this->response('资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name));
139 } 136 }
140 137
141 /** 138 /**
  139 + * 接口上传单文件
  140 + * @param $files
  141 + * @return array
  142 + */
  143 + public function api_upload_single(&$files)
  144 + {
  145 + $hash = hash_file('md5', $files->getPathname());
  146 + $name = $files->getFilename();
  147 + //查看文件是否存在
  148 + $fileModel = new File();
  149 + //查看图片是否已上传
  150 + $param = ['hash' => $hash, 'refer' => $this->param['refer'] ?? 0];
  151 + if (isset($this->cache['project_id']) && !empty($this->cache['project_id'])) {
  152 + $param['project_id'] = $this->cache['project_id'];
  153 + }
  154 + $file_hash = $fileModel->read($param);
  155 + if ($file_hash !== false) {
  156 + return [
  157 + 'message' => '资源',
  158 + 'code' => Code::SUCCESS,
  159 + 'data' => $this->responseData($file_hash['path'], $name)
  160 + ];
  161 + }
  162 + $url = $this->config['root'] . $this->path;
  163 + $fileName = uniqid() . rand(10000, 99999) . '.' . $files->getExtension();
  164 + //同步数据到cos
  165 + if ($this->upload_location == 1) {
  166 + $cosService = new CosService();
  167 + $cosService->uploadFile($files, $this->path, $fileName);
  168 + } else {
  169 + $res = $files->move($url, $fileName);
  170 + if ($res === false) {
  171 + return [
  172 + 'message' => $files->getError(),
  173 + 'code' => Code::USER_ERROR
  174 + ];
  175 + }
  176 + }
  177 + $this->saveMysql($fileModel, $files->getSize(), $files->getExtension(), $fileName, $hash, $this->upload_location, $files->getMimeType(), $name);
  178 + return [
  179 + 'message' => '资源',
  180 + 'code' => Code::SUCCESS,
  181 + 'data' => $this->responseData($this->path . '/' . $fileName, $name)
  182 + ];
  183 + }
  184 +
  185 + /**
142 * @remark :保存数据库 186 * @remark :保存数据库
143 * @name :saveMysql 187 * @name :saveMysql
144 * @author :lyh 188 * @author :lyh