作者 lyh

gx

@@ -53,7 +53,7 @@ class AiBlogAuthorTask extends Command @@ -53,7 +53,7 @@ class AiBlogAuthorTask extends Command
53 continue; 53 continue;
54 } 54 }
55 $info = $info->toArray(); 55 $info = $info->toArray();
56 - echo '开始->project_id:' . $info['project_id'] . PHP_EOL . date('Y-m-d H:i:s'); 56 + echo date('Y-m-d H:i:s').'开始->project_id:' . $info['project_id'] . PHP_EOL;
57 //获取配置 57 //获取配置
58 $aiSettingInfo = $this->getSetting($info['project_id']); 58 $aiSettingInfo = $this->getSetting($info['project_id']);
59 if(empty($aiSettingInfo)){ 59 if(empty($aiSettingInfo)){
@@ -82,7 +82,7 @@ class AiBlogAuthorTask extends Command @@ -82,7 +82,7 @@ class AiBlogAuthorTask extends Command
82 DB::disconnect('custom_mysql'); 82 DB::disconnect('custom_mysql');
83 //修改任务状态 83 //修改任务状态
84 $aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]); 84 $aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]);
85 - echo '结束->任务id:' . $info['id'] . PHP_EOL . date('Y-m-d H:i:s'); 85 + echo date('Y-m-d H:i:s').'结束->任务id:' . $info['id'] . PHP_EOL;
86 } 86 }
87 return true; 87 return true;
88 } 88 }
@@ -144,7 +144,7 @@ class AiBlogAuthorTask extends Command @@ -144,7 +144,7 @@ class AiBlogAuthorTask extends Command
144 $aiBlogService->key = $aiSettingInfo['key']; 144 $aiBlogService->key = $aiSettingInfo['key'];
145 $aiBlogService->updateAuthorInfo(['author_id'=>$param['author_id'],'route'=>$param['route'],'title'=>$param['title'],'picture'=>$param['image'],'description'=>$param['description']]); 145 $aiBlogService->updateAuthorInfo(['author_id'=>$param['author_id'],'route'=>$param['route'],'title'=>$param['title'],'picture'=>$param['image'],'description'=>$param['description']]);
146 }catch (\Exception $e){ 146 }catch (\Exception $e){
147 - echo 'error:'.$e->getMessage(); 147 + echo 'error:'.$e->getMessage().PHP_EOL;
148 continue; 148 continue;
149 } 149 }
150 } 150 }
@@ -40,6 +40,8 @@ class AiBlogTask extends Command @@ -40,6 +40,8 @@ class AiBlogTask extends Command
40 40
41 public $updateProject = [];//需更新的列表 41 public $updateProject = [];//需更新的列表
42 42
  43 +
  44 +
43 /** 45 /**
44 * The console command description. 46 * The console command description.
45 * 47 *
@@ -50,66 +52,88 @@ class AiBlogTask extends Command @@ -50,66 +52,88 @@ class AiBlogTask extends Command
50 public function handle(){ 52 public function handle(){
51 while (true){ 53 while (true){
52 //获取任务id 54 //获取任务id
53 - $task_id = $this->getTaskId(); 55 + $task_id = $this->getTaskId(2);
54 if(empty($task_id)){ 56 if(empty($task_id)){
55 sleep(300); 57 sleep(300);
56 - }  
57 - $aiBlogTaskModel = new AiBlogTaskModel();  
58 - $item = $aiBlogTaskModel->read(['id'=>$task_id]);  
59 - echo '开始->任务id:' . $item['task_id'] . PHP_EOL . date('Y-m-d H:i:s');  
60 - //获取配置  
61 - $aiSettingInfo = $this->getSetting($item['project_id']);  
62 - $aiBlogService = new AiBlogService();  
63 - $aiBlogService->mch_id = $aiSettingInfo['mch_id'];  
64 - $aiBlogService->key = $aiSettingInfo['key'];  
65 - $aiBlogService->task_id = $item['task_id'];  
66 - $result = $aiBlogService->getDetail();  
67 - if(!isset($result['status'])){  
68 - if($item['sort'] <= 5){  
69 - $aiBlogTaskModel->edit(['sort'=>$item['sort'] + 1],['id'=>$item['id']]);  
70 - }else{  
71 - $aiBlogTaskModel->edit(['status'=>9],['id'=>$item['id']]);  
72 - }  
73 - echo json_encode($result,true).PHP_EOL;  
74 - continue;  
75 - }  
76 - if($result['status'] != 200){  
77 - echo '错误状态码:'.$result['status'].PHP_EOL;  
78 continue; 58 continue;
79 } 59 }
80 - //保存当前项目ai_blog数据  
81 - ProjectServer::useProject($item['project_id']);  
82 - $aiBlogModel = new AiBlog();  
83 - $aiBlogInfo = $aiBlogModel->read(['task_id'=>$item['task_id']],['id']);  
84 - if($aiBlogInfo === false){  
85 - echo '任务id不存在:'.$item['task_id'].PHP_EOL;  
86 - $aiBlogTaskModel->edit(['status'=>2],['id'=>$item['id']]); 60 + $result = $this->sendRequest($task_id);
  61 + if($result == false){
87 continue; 62 continue;
88 } 63 }
89 - if (!in_array($result['data']['author_id'], $this->updateProject[$item['project_id']] ?? [])) {  
90 - $this->updateProject[$item['project_id']][] = $result['data']['author_id'];  
91 - }  
92 - //拿到返回的路由查看是否重复  
93 - $route = RouteMap::setRoute($result['data']['url'], RouteMap::SOURCE_AI_BLOG, $aiBlogInfo['id'], $item['project_id']);  
94 - if($route != $result['data']['url']){  
95 - $aiBlogService->updateDetail(['route'=>$route,'task_id'=>$item['task_id']]); 64 +
  65 + }
  66 + return true;
  67 + }
  68 +
  69 + /**
  70 + * @remark :请求方法
  71 + * @name :sendRequest
  72 + * @author :lyh
  73 + * @method :post
  74 + * @time :2025/3/19 16:48
  75 + */
  76 + public function sendRequest($task_id){
  77 + $aiBlogTaskModel = new AiBlogTaskModel();
  78 + $item = $aiBlogTaskModel->read(['id'=>$task_id]);
  79 + echo date('Y-m-d H:i:s') . '开始->任务id:' . $item['task_id'] . PHP_EOL;
  80 + //获取配置
  81 + $aiSettingInfo = $this->getSetting($item['project_id']);
  82 + $aiBlogService = new AiBlogService();
  83 + $aiBlogService->mch_id = $aiSettingInfo['mch_id'];
  84 + $aiBlogService->key = $aiSettingInfo['key'];
  85 + $aiBlogService->task_id = $item['task_id'];
  86 + //拉取文章数据
  87 + $result = $aiBlogService->getDetail();
  88 + if(!isset($result['status'])){
  89 + if($item['sort'] <= 5){
  90 + $aiBlogTaskModel->edit(['sort'=>$item['sort'] + 1],['id'=>$item['id']]);
  91 + }else{
  92 + $aiBlogTaskModel->edit(['status'=>9],['id'=>$item['id']]);
  93 + // 钉钉通知
96 } 94 }
97 - $aiBlogModel->edit(['new_title'=>$result['data']['title'], 'image'=>$result['data']['thumb'], 'text'=>$result['data']['section'], 'author_id'=>$result['data']['author_id'],'seo_title'=>$result['data']['title'],'seo_keyword'=>$result['data']['keyword'],'seo_description'=>$result['data']['description'], 'route'=>$route ,'status'=>2], ['task_id'=>$item['task_id']]);  
98 - DB::disconnect('custom_mysql'); 95 + echo date('Y-m-d H:i:s'). '错误信息:'.json_encode($result,true).PHP_EOL;
  96 + return false;
  97 + }
  98 + if($result['status'] != 200){
  99 + echo '未拉取到数据---状态码:'.$result['status'].PHP_EOL;
  100 + return false;
  101 + }
  102 + //保存当前项目ai_blog数据
  103 + ProjectServer::useProject($item['project_id']);
  104 + $aiBlogModel = new AiBlog();
  105 + $aiBlogInfo = $aiBlogModel->read(['task_id'=>$item['task_id']],['id']);
  106 + if($aiBlogInfo === false){
  107 + echo '任务id不存在:'.$item['task_id'].PHP_EOL;
99 $aiBlogTaskModel->edit(['status'=>2],['id'=>$item['id']]); 108 $aiBlogTaskModel->edit(['status'=>2],['id'=>$item['id']]);
100 - echo '结束->任务id:' . $item['task_id'] . PHP_EOL . date('Y-m-d H:i:s'); 109 + DB::disconnect('custom_mysql');
  110 + return false;
101 } 111 }
  112 + if (!in_array($result['data']['author_id'], $this->updateProject[$item['project_id']] ?? [])) {
  113 + $this->updateProject[$item['project_id']][] = $result['data']['author_id'];
  114 + }
  115 + //拿到返回的路由查看是否重复
  116 + $route = RouteMap::setRoute($result['data']['url'], RouteMap::SOURCE_AI_BLOG, $aiBlogInfo['id'], $item['project_id']);
  117 + if($route != $result['data']['url']){
  118 + $aiBlogService->updateDetail(['route'=>$route,'task_id'=>$item['task_id']]);
  119 + }
  120 + $aiBlogModel->edit(['new_title'=>$result['data']['title'], 'image'=>$result['data']['thumb'], 'text'=>$result['data']['section'], 'author_id'=>$result['data']['author_id'],'seo_title'=>$result['data']['title'],'seo_keyword'=>$result['data']['keyword'],'seo_description'=>$result['data']['description'], 'route'=>$route ,'status'=>2], ['task_id'=>$item['task_id']]);
  121 + DB::disconnect('custom_mysql');
  122 + $aiBlogTaskModel->edit(['status'=>2],['id'=>$item['id']]);
  123 + echo date('Y-m-d H:i:s').'结束->任务id:' . $item['task_id'] . PHP_EOL;
102 return true; 124 return true;
103 } 125 }
104 126
  127 +
105 /** 128 /**
106 * @remark :获取任务id 129 * @remark :获取任务id
107 * @name :getTaskId 130 * @name :getTaskId
108 * @author :lyh 131 * @author :lyh
109 * @method :post 132 * @method :post
110 * @time :2025/3/19 16:16 133 * @time :2025/3/19 16:16
  134 + * @param :
111 */ 135 */
112 - public function getTaskId() 136 + public function getTaskId($finish_at = 1)
113 { 137 {
114 $task_id = Redis::rpop('ai_blog_task'); 138 $task_id = Redis::rpop('ai_blog_task');
115 if (empty($task_id)) { 139 if (empty($task_id)) {
@@ -118,7 +142,8 @@ class AiBlogTask extends Command @@ -118,7 +142,8 @@ class AiBlogTask extends Command
118 $this->updateProject = []; 142 $this->updateProject = [];
119 } 143 }
120 $aiBlogTaskModel = new AiBlogTaskModel(); 144 $aiBlogTaskModel = new AiBlogTaskModel();
121 - $ids = $aiBlogTaskModel->formatQuery(['status'=>1,'type'=>2])->limit(1000)->pluck('id'); 145 + $finish_at = date('Y-m-d H:i:s', strtotime('-' . $finish_at . ' hour'));
  146 + $ids = $aiBlogTaskModel->formatQuery(['status'=>1,'type'=>2,'created_at'=>['<=',$finish_at]])->limit(1000)->pluck('id');
122 if(!empty($ids)){ 147 if(!empty($ids)){
123 foreach ($ids as $id) { 148 foreach ($ids as $id) {
124 Redis::lpush('ai_blog_task', $id); 149 Redis::lpush('ai_blog_task', $id);