AiBlogListTask.php
5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* @remark :
* @name :AiBlogTask.php
* @author :lyh
* @method :post
* @time :2025/2/14 11:14
*/
namespace App\Console\Commands\Ai;
use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Ai\AiBlogList;
use App\Models\Domain\DomainInfo;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use function Symfony\Component\String\s;
/***
* @remark :根据项目更新blog列表
* @name :AiBlogListProjectTask
* @author :lyh
* @method :post
* @time :2025/3/6 9:45
*/
class AiBlogListTask extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'save_ai_blog_list_task';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成blog列表页';
public function handle(){
while (true){
$task_id = $this->getTaskId();
if(empty($task_id)){
sleep(200);
continue;
}
$aiBlogTaskModel = new AiBlogTaskModel();
$info = $aiBlogTaskModel->read(['id'=>$task_id,'type'=>$aiBlogTaskModel::TYPE_LIST]);
if($info === false){
echo date('Y-m-d H:i:s').',当前数据不存在或者已被删除。'.PHP_EOL;
}
$project_id = $info['project_id'];
echo '执行的项目ID:'.$info['project_id'].PHP_EOL;
ProjectServer::useProject($project_id);
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
$res = $this->updateBlogList($aiSettingInfo);
if($res){
$aiBlogTaskModel->edit(['status'=>2],['id'=>$task_id]);
}else{
if($info['sort'] >= 5){
$aiBlogTaskModel->edit(['status'=>9],['id'=>$task_id]);
}else{
$aiBlogTaskModel->edit(['status'=>9,'sort'=>($info['sort'] + 1)],['id'=>$task_id]);
}
}
$this->curlDelRoute($project_id);
DB::disconnect('custom_mysql');
}
return true;
}
/**
* @remark :更新列表页数据
* @name :updateBlogList
* @author :lyh
* @method :post
* @time :2025/3/5 11:07
*/
public function updateBlogList($aiSettingInfo){
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$page = 1;
$saveData = [];
$result = $aiBlogService->getAiBlogList($page,15);
if(!isset($result['status']) || $result['status'] != 200){
echo '请求失败。'.json_encode($result, JSON_UNESCAPED_UNICODE);
return false;
}
$total_page = $result['data']['total_page'];
//组装数据保存
$saveData[] = [
'route'=>$page,
'text'=>$result['data']['section'],
];
while ($total_page > $page){
$page++;
$result = $aiBlogService->getAiBlogList($page,15);
if(isset($result['status']) && $result['status'] == 200){
$saveData[] = [
'route'=>$page,
'text'=>$result['data']['section'],
];
}else{
echo '请求失败。'.json_encode($result, JSON_UNESCAPED_UNICODE);
return false;
}
}
$aiBlogListModel = new AiBlogList();
if(!empty($saveData)){
//写一条路由信息
$aiBlogListModel->truncate();
$aiBlogListModel->insertAll($saveData);
return true;
}
return true;
}
/**
* @remark :通知C端生成界面
* @name :sendNotice
* @author :lyh
* @method :post
* @time :2025/3/6 11:51
*/
public function curlDelRoute($project_id){
$domainModel = new DomainInfo();
//获取项目域名
$domain = $domainModel->getProjectIdDomain($project_id);
if(!empty($domain)){
$c_url = $domain.'api/update_page/';
$param = [
'project_id' => $project_id,
'type' => 1,
'route' => 3,
'url' => ['top-blog'],
'language'=> [],
'is_sitemap' => 0
];
http_post($c_url, json_encode($param));
}
return true;
}
/**
* 获取任务id
* @param int $finish_at
* @return mixed
*/
public function getTaskId($finish_at = 2)
{
$keys = 'ai_blog_list_task';
$task_id = Redis::rpop($keys);
if (empty($task_id)) {
$aiBlogTaskModel = new AiBlogTaskModel();
$finish_at = date('Y-m-d H:i:s', strtotime('-' . $finish_at . ' hour'));
$ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING, 'type'=>$aiBlogTaskModel::TYPE_LIST, 'updated_at'=>['<=',$finish_at]])->pluck('id');
if(!empty($ids)){
foreach ($ids as $id) {
Redis::lpush($keys, $id);
}
}
$task_id = Redis::rpop($keys);
}
return $task_id;
}
}