合并分支 'lyh-server' 到 'master'
Lyh server 查看合并请求 !3277
正在显示
7 个修改的文件
包含
212 行增加
和
20 行删除
| @@ -26,7 +26,7 @@ use function Symfony\Component\String\s; | @@ -26,7 +26,7 @@ use function Symfony\Component\String\s; | ||
| 26 | 26 | ||
| 27 | /*** | 27 | /*** |
| 28 | * @remark :根据项目更新blog列表 | 28 | * @remark :根据项目更新blog列表 |
| 29 | - * @name :AiBlogListTask | 29 | + * @name :AiBlogListProjectTask |
| 30 | * @author :lyh | 30 | * @author :lyh |
| 31 | * @method :post | 31 | * @method :post |
| 32 | * @time :2025/3/6 9:45 | 32 | * @time :2025/3/6 9:45 |
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * @remark : | ||
| 4 | + * @name :AiBlogTask.php | ||
| 5 | + * @author :lyh | ||
| 6 | + * @method :post | ||
| 7 | + * @time :2025/2/14 11:14 | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | +namespace App\Console\Commands\Ai; | ||
| 11 | + | ||
| 12 | +use App\Models\Ai\AiBlog; | ||
| 13 | +use App\Models\Ai\AiBlogAuthor; | ||
| 14 | +use App\Models\Ai\AiBlogList; | ||
| 15 | +use App\Models\Domain\DomainInfo; | ||
| 16 | +use App\Models\Project\ProjectAiSetting; | ||
| 17 | +use App\Models\RouteMap\RouteMap; | ||
| 18 | +use App\Services\AiBlogService; | ||
| 19 | +use App\Services\ProjectServer; | ||
| 20 | +use Illuminate\Console\Command; | ||
| 21 | +use App\Models\Project\AiBlogTask as AiBlogTaskModel; | ||
| 22 | +use Illuminate\Support\Facades\Cache; | ||
| 23 | +use Illuminate\Support\Facades\DB; | ||
| 24 | +use function Symfony\Component\String\s; | ||
| 25 | + | ||
| 26 | +/*** | ||
| 27 | + * @remark :根据项目更新blog列表 | ||
| 28 | + * @name :AiBlogListProjectTask | ||
| 29 | + * @author :lyh | ||
| 30 | + * @method :post | ||
| 31 | + * @time :2025/3/6 9:45 | ||
| 32 | + */ | ||
| 33 | +class AiBlogListProjectTask extends Command | ||
| 34 | +{ | ||
| 35 | + /** | ||
| 36 | + * The name and signature of the console command. | ||
| 37 | + * | ||
| 38 | + * @var string | ||
| 39 | + */ | ||
| 40 | + protected $signature = 'save_ai_blog_list {project_id}'; | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * The console command description. | ||
| 44 | + * | ||
| 45 | + * @var string | ||
| 46 | + */ | ||
| 47 | + protected $description = '根据项目生成blog列表'; | ||
| 48 | + | ||
| 49 | + public function handle(){ | ||
| 50 | + $project_id = $this->argument('project_id'); | ||
| 51 | + @file_put_contents(storage_path('logs/lyh_error.log'), var_export('执行的项目id->'.$project_id, true) . PHP_EOL, FILE_APPEND); | ||
| 52 | + ProjectServer::useProject($project_id); | ||
| 53 | + $projectAiSettingModel = new ProjectAiSetting(); | ||
| 54 | + $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]); | ||
| 55 | + $this->updateBlogList($aiSettingInfo); | ||
| 56 | + $this->curlDelRoute($project_id); | ||
| 57 | + DB::disconnect('custom_mysql'); | ||
| 58 | + return true; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + /** | ||
| 62 | + * @remark :更新列表页数据 | ||
| 63 | + * @name :updateBlogList | ||
| 64 | + * @author :lyh | ||
| 65 | + * @method :post | ||
| 66 | + * @time :2025/3/5 11:07 | ||
| 67 | + */ | ||
| 68 | + public function updateBlogList($aiSettingInfo){ | ||
| 69 | + $aiBlogService = new AiBlogService(); | ||
| 70 | + $aiBlogService->mch_id = $aiSettingInfo['mch_id']; | ||
| 71 | + $aiBlogService->key = $aiSettingInfo['key']; | ||
| 72 | + $page = 1; | ||
| 73 | + $saveData = []; | ||
| 74 | + $result = $aiBlogService->getAiBlogList($page,15); | ||
| 75 | + if(!isset($result['status']) || $result['status'] != 200){ | ||
| 76 | + echo '请示失败。'.json_encode($result, JSON_UNESCAPED_UNICODE); | ||
| 77 | + return true; | ||
| 78 | + } | ||
| 79 | + $total_page = $result['data']['total_page']; | ||
| 80 | + //组装数据保存 | ||
| 81 | + $saveData[] = [ | ||
| 82 | + 'route'=>$page, | ||
| 83 | + 'text'=>$result['data']['section'], | ||
| 84 | + ]; | ||
| 85 | + while ($total_page > $page){ | ||
| 86 | + $page++; | ||
| 87 | + $result = $aiBlogService->getAiBlogList($page,15); | ||
| 88 | + if(isset($result['status']) && $result['status'] == 200){ | ||
| 89 | + $saveData[] = [ | ||
| 90 | + 'route'=>$page, | ||
| 91 | + 'text'=>$result['data']['section'], | ||
| 92 | + ]; | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + $aiBlogListModel = new AiBlogList(); | ||
| 96 | + if(!empty($saveData)){ | ||
| 97 | + //写一条路由信息 | ||
| 98 | + $aiBlogListModel->truncate(); | ||
| 99 | + $aiBlogListModel->insertAll($saveData); | ||
| 100 | + } | ||
| 101 | + return true; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + /** | ||
| 105 | + * @remark :通知C端生成界面 | ||
| 106 | + * @name :sendNotice | ||
| 107 | + * @author :lyh | ||
| 108 | + * @method :post | ||
| 109 | + * @time :2025/3/6 11:51 | ||
| 110 | + */ | ||
| 111 | + public function curlDelRoute($project_id){ | ||
| 112 | + $domainModel = new DomainInfo(); | ||
| 113 | + //获取项目域名 | ||
| 114 | + $domain = $domainModel->getProjectIdDomain($project_id); | ||
| 115 | + if(!empty($domain)){ | ||
| 116 | + $c_url = $domain.'api/update_page/'; | ||
| 117 | + $param = [ | ||
| 118 | + 'project_id' => $project_id, | ||
| 119 | + 'type' => 1, | ||
| 120 | + 'route' => 3, | ||
| 121 | + 'url' => ['top-blog'], | ||
| 122 | + 'language'=> [], | ||
| 123 | + 'is_sitemap' => 0 | ||
| 124 | + ]; | ||
| 125 | + http_post($c_url, json_encode($param)); | ||
| 126 | + } | ||
| 127 | + return true; | ||
| 128 | + } | ||
| 129 | +} |
| @@ -21,11 +21,12 @@ use Illuminate\Console\Command; | @@ -21,11 +21,12 @@ use Illuminate\Console\Command; | ||
| 21 | use App\Models\Project\AiBlogTask as AiBlogTaskModel; | 21 | use App\Models\Project\AiBlogTask as AiBlogTaskModel; |
| 22 | use Illuminate\Support\Facades\Cache; | 22 | use Illuminate\Support\Facades\Cache; |
| 23 | use Illuminate\Support\Facades\DB; | 23 | use Illuminate\Support\Facades\DB; |
| 24 | +use Illuminate\Support\Facades\Redis; | ||
| 24 | use function Symfony\Component\String\s; | 25 | use function Symfony\Component\String\s; |
| 25 | 26 | ||
| 26 | /*** | 27 | /*** |
| 27 | * @remark :根据项目更新blog列表 | 28 | * @remark :根据项目更新blog列表 |
| 28 | - * @name :AiBlogListTask | 29 | + * @name :AiBlogListProjectTask |
| 29 | * @author :lyh | 30 | * @author :lyh |
| 30 | * @method :post | 31 | * @method :post |
| 31 | * @time :2025/3/6 9:45 | 32 | * @time :2025/3/6 9:45 |
| @@ -37,24 +38,45 @@ class AiBlogListTask extends Command | @@ -37,24 +38,45 @@ class AiBlogListTask extends Command | ||
| 37 | * | 38 | * |
| 38 | * @var string | 39 | * @var string |
| 39 | */ | 40 | */ |
| 40 | - protected $signature = 'save_ai_blog_list {project_id}'; | 41 | + protected $signature = 'save_ai_blog_list_task'; |
| 41 | 42 | ||
| 42 | /** | 43 | /** |
| 43 | * The console command description. | 44 | * The console command description. |
| 44 | * | 45 | * |
| 45 | * @var string | 46 | * @var string |
| 46 | */ | 47 | */ |
| 47 | - protected $description = '生成blog列表'; | 48 | + protected $description = '生成blog列表页'; |
| 48 | 49 | ||
| 49 | public function handle(){ | 50 | public function handle(){ |
| 50 | - $project_id = $this->argument('project_id'); | ||
| 51 | - @file_put_contents(storage_path('logs/lyh_error.log'), var_export('执行的项目id->'.$project_id, true) . PHP_EOL, FILE_APPEND); | ||
| 52 | - ProjectServer::useProject($project_id); | ||
| 53 | - $projectAiSettingModel = new ProjectAiSetting(); | ||
| 54 | - $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]); | ||
| 55 | - $this->updateBlogList($aiSettingInfo); | ||
| 56 | - $this->curlDelRoute($project_id); | ||
| 57 | - DB::disconnect('custom_mysql'); | 51 | + while (true){ |
| 52 | + $task_id = $this->getTaskId(); | ||
| 53 | + if(empty($task_id)){ | ||
| 54 | + sleep(200); | ||
| 55 | + continue; | ||
| 56 | + } | ||
| 57 | + $aiBlogTaskModel = new AiBlogTaskModel(); | ||
| 58 | + $info = $aiBlogTaskModel->read(['id'=>$task_id]); | ||
| 59 | + if($info === false){ | ||
| 60 | + echo date('Y-m-d H:i:s').',当前数据不存在或者已被删除。'.PHP_EOL; | ||
| 61 | + } | ||
| 62 | + $project_id = $info['project_id']; | ||
| 63 | + echo '执行的项目ID:'.$info['project_id'].PHP_EOL; | ||
| 64 | + ProjectServer::useProject($project_id); | ||
| 65 | + $projectAiSettingModel = new ProjectAiSetting(); | ||
| 66 | + $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]); | ||
| 67 | + $res = $this->updateBlogList($aiSettingInfo); | ||
| 68 | + if($res){ | ||
| 69 | + $aiBlogTaskModel->edit(['status'=>2],['id'=>$task_id]); | ||
| 70 | + }else{ | ||
| 71 | + if($info['sort'] >= 5){ | ||
| 72 | + $aiBlogTaskModel->edit(['status'=>9],['id'=>$task_id]); | ||
| 73 | + }else{ | ||
| 74 | + $aiBlogTaskModel->edit(['status'=>9,'sort'=>($info['sort'] + 1)],['id'=>$task_id]); | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + $this->curlDelRoute($project_id); | ||
| 78 | + DB::disconnect('custom_mysql'); | ||
| 79 | + } | ||
| 58 | return true; | 80 | return true; |
| 59 | } | 81 | } |
| 60 | 82 | ||
| @@ -73,8 +95,8 @@ class AiBlogListTask extends Command | @@ -73,8 +95,8 @@ class AiBlogListTask extends Command | ||
| 73 | $saveData = []; | 95 | $saveData = []; |
| 74 | $result = $aiBlogService->getAiBlogList($page,15); | 96 | $result = $aiBlogService->getAiBlogList($page,15); |
| 75 | if(!isset($result['status']) || $result['status'] != 200){ | 97 | if(!isset($result['status']) || $result['status'] != 200){ |
| 76 | - echo '请示失败。'.json_encode($result, JSON_UNESCAPED_UNICODE); | ||
| 77 | - return true; | 98 | + echo '请求失败。'.json_encode($result, JSON_UNESCAPED_UNICODE); |
| 99 | + return false; | ||
| 78 | } | 100 | } |
| 79 | $total_page = $result['data']['total_page']; | 101 | $total_page = $result['data']['total_page']; |
| 80 | //组装数据保存 | 102 | //组装数据保存 |
| @@ -90,6 +112,9 @@ class AiBlogListTask extends Command | @@ -90,6 +112,9 @@ class AiBlogListTask extends Command | ||
| 90 | 'route'=>$page, | 112 | 'route'=>$page, |
| 91 | 'text'=>$result['data']['section'], | 113 | 'text'=>$result['data']['section'], |
| 92 | ]; | 114 | ]; |
| 115 | + }else{ | ||
| 116 | + echo '请求失败。'.json_encode($result, JSON_UNESCAPED_UNICODE); | ||
| 117 | + return false; | ||
| 93 | } | 118 | } |
| 94 | } | 119 | } |
| 95 | $aiBlogListModel = new AiBlogList(); | 120 | $aiBlogListModel = new AiBlogList(); |
| @@ -97,6 +122,7 @@ class AiBlogListTask extends Command | @@ -97,6 +122,7 @@ class AiBlogListTask extends Command | ||
| 97 | //写一条路由信息 | 122 | //写一条路由信息 |
| 98 | $aiBlogListModel->truncate(); | 123 | $aiBlogListModel->truncate(); |
| 99 | $aiBlogListModel->insertAll($saveData); | 124 | $aiBlogListModel->insertAll($saveData); |
| 125 | + return true; | ||
| 100 | } | 126 | } |
| 101 | return true; | 127 | return true; |
| 102 | } | 128 | } |
| @@ -126,4 +152,27 @@ class AiBlogListTask extends Command | @@ -126,4 +152,27 @@ class AiBlogListTask extends Command | ||
| 126 | } | 152 | } |
| 127 | return true; | 153 | return true; |
| 128 | } | 154 | } |
| 155 | + | ||
| 156 | + /** | ||
| 157 | + * 获取任务id | ||
| 158 | + * @param int $finish_at | ||
| 159 | + * @return mixed | ||
| 160 | + */ | ||
| 161 | + public function getTaskId($finish_at = 2) | ||
| 162 | + { | ||
| 163 | + $keys = 'ai_blog_list_task'; | ||
| 164 | + $task_id = Redis::rpop($keys); | ||
| 165 | + if (empty($task_id)) { | ||
| 166 | + $aiBlogTaskModel = new AiBlogTaskModel(); | ||
| 167 | + $finish_at = date('Y-m-d H:i:s', strtotime('-' . $finish_at . ' hour')); | ||
| 168 | + $ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING, 'type'=>$aiBlogTaskModel::TYPE_LIST, 'updated_at'=>['<=',$finish_at]])->pluck('id'); | ||
| 169 | + if(!empty($ids)){ | ||
| 170 | + foreach ($ids as $id) { | ||
| 171 | + Redis::lpush($keys, $id); | ||
| 172 | + } | ||
| 173 | + } | ||
| 174 | + $task_id = Redis::rpop($keys); | ||
| 175 | + } | ||
| 176 | + return $task_id; | ||
| 177 | + } | ||
| 129 | } | 178 | } |
| @@ -26,10 +26,8 @@ use App\Services\DingService; | @@ -26,10 +26,8 @@ use App\Services\DingService; | ||
| 26 | use App\Services\ProjectServer; | 26 | use App\Services\ProjectServer; |
| 27 | use Illuminate\Console\Command; | 27 | use Illuminate\Console\Command; |
| 28 | use App\Models\Project\AiBlogTask as AiBlogTaskModel; | 28 | use App\Models\Project\AiBlogTask as AiBlogTaskModel; |
| 29 | -use Illuminate\Support\Facades\Cache; | ||
| 30 | use Illuminate\Support\Facades\DB; | 29 | use Illuminate\Support\Facades\DB; |
| 31 | use Illuminate\Support\Facades\Redis; | 30 | use Illuminate\Support\Facades\Redis; |
| 32 | -use function Symfony\Component\String\s; | ||
| 33 | 31 | ||
| 34 | class AiBlogTask extends Command | 32 | class AiBlogTask extends Command |
| 35 | { | 33 | { |
| @@ -183,7 +181,7 @@ class AiBlogTask extends Command | @@ -183,7 +181,7 @@ class AiBlogTask extends Command | ||
| 183 | ProjectServer::useProject($project_id); | 181 | ProjectServer::useProject($project_id); |
| 184 | $aiSettingInfo = $this->getSetting($project_id); | 182 | $aiSettingInfo = $this->getSetting($project_id); |
| 185 | $this->output('sync: list start, project_id: ' . $project_id); | 183 | $this->output('sync: list start, project_id: ' . $project_id); |
| 186 | - $this->updateBlogList($aiSettingInfo); | 184 | + $this->updateBlogList($aiSettingInfo,$project_id); |
| 187 | $this->output('sync: list end'); | 185 | $this->output('sync: list end'); |
| 188 | //更新作者 | 186 | //更新作者 |
| 189 | $this->output('sync: author start, project_id: ' . $project_id); | 187 | $this->output('sync: author start, project_id: ' . $project_id); |
| @@ -249,7 +247,7 @@ class AiBlogTask extends Command | @@ -249,7 +247,7 @@ class AiBlogTask extends Command | ||
| 249 | * @param $aiSettingInfo | 247 | * @param $aiSettingInfo |
| 250 | * @return bool | 248 | * @return bool |
| 251 | */ | 249 | */ |
| 252 | - public function updateBlogList($aiSettingInfo){ | 250 | + public function updateBlogList($aiSettingInfo,$project_id = 0){ |
| 253 | $aiBlogService = new AiBlogService(); | 251 | $aiBlogService = new AiBlogService(); |
| 254 | $aiBlogService->mch_id = $aiSettingInfo['mch_id']; | 252 | $aiBlogService->mch_id = $aiSettingInfo['mch_id']; |
| 255 | $aiBlogService->key = $aiSettingInfo['key']; | 253 | $aiBlogService->key = $aiSettingInfo['key']; |
| @@ -257,6 +255,21 @@ class AiBlogTask extends Command | @@ -257,6 +255,21 @@ class AiBlogTask extends Command | ||
| 257 | $saveData = []; | 255 | $saveData = []; |
| 258 | $result = $aiBlogService->getAiBlogList($page,15); | 256 | $result = $aiBlogService->getAiBlogList($page,15); |
| 259 | if(!isset($result['status']) || $result['status'] != 200){ | 257 | if(!isset($result['status']) || $result['status'] != 200){ |
| 258 | + try { | ||
| 259 | + // 钉钉通知 | ||
| 260 | + $dingService = new DingService(); | ||
| 261 | + $body = [ | ||
| 262 | + 'keyword' => 'AI_BLOG列表页未生成拉取失败', | ||
| 263 | + 'msg' => '项目ID:' . $project_id . PHP_EOL . '返回信息:' . json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), | ||
| 264 | + 'isAtAll' => false, // 是否@所有人 | ||
| 265 | + ]; | ||
| 266 | + $dingService->handle($body); | ||
| 267 | + //写一条更新记录 | ||
| 268 | + $aiBlogTaskModel = new AiBlogTaskModel(); | ||
| 269 | + $aiBlogTaskModel->addReturnId(['project_id'=>$project_id,'task_id'=>$project_id,'status'=>$aiBlogTaskModel::STATUS_RUNNING,'type'=>$aiBlogTaskModel::TYPE_LIST]); | ||
| 270 | + }catch (\Exception $e){ | ||
| 271 | + $this->output('更新列表页失败同时通知失败--error:' . $e->getMessage()); | ||
| 272 | + } | ||
| 260 | return true; | 273 | return true; |
| 261 | } | 274 | } |
| 262 | $total_page = $result['data']['total_page']; | 275 | $total_page = $result['data']['total_page']; |
| @@ -27,7 +27,7 @@ use function Symfony\Component\String\s; | @@ -27,7 +27,7 @@ use function Symfony\Component\String\s; | ||
| 27 | 27 | ||
| 28 | /*** | 28 | /*** |
| 29 | * @remark :根据项目更新blog列表 | 29 | * @remark :根据项目更新blog列表 |
| 30 | - * @name :AiBlogListTask | 30 | + * @name :AiBlogListProjectTask |
| 31 | * @author :lyh | 31 | * @author :lyh |
| 32 | * @method :post | 32 | * @method :post |
| 33 | * @time :2025/3/6 9:45 | 33 | * @time :2025/3/6 9:45 |
| @@ -366,7 +366,7 @@ class TranslateController extends BaseController | @@ -366,7 +366,7 @@ class TranslateController extends BaseController | ||
| 366 | $count = 0; | 366 | $count = 0; |
| 367 | } else { | 367 | } else { |
| 368 | $cateRelateModel = new CategoryRelated(); | 368 | $cateRelateModel = new CategoryRelated(); |
| 369 | - $count = $cateRelateModel->whereIn('cateid', $ids)->distinct('product_id')->count(); | 369 | + $count = $cateRelateModel->whereIn('cate_id', $ids)->distinct('product_id')->count(); |
| 370 | } | 370 | } |
| 371 | } | 371 | } |
| 372 | $this->pageSixList($data,$count,$v,1,15); | 372 | $this->pageSixList($data,$count,$v,1,15); |
| @@ -27,4 +27,5 @@ class AiBlogTask extends Base | @@ -27,4 +27,5 @@ class AiBlogTask extends Base | ||
| 27 | const TYPE_AUTHOR = 1; | 27 | const TYPE_AUTHOR = 1; |
| 28 | const TYPE_BLOG = 2; | 28 | const TYPE_BLOG = 2; |
| 29 | const TYPE_AUTHOR_ID = 3;//根据对应id页面 | 29 | const TYPE_AUTHOR_ID = 3;//根据对应id页面 |
| 30 | + const TYPE_LIST = 4;//更新列表页 | ||
| 30 | } | 31 | } |
-
请 注册 或 登录 后发表评论