作者 lyh

变更数据

@@ -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 }
@@ -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