AiBlogAuthorId.php
5.6 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
<?php
/**
* @remark :
* @name :AiBlogAuthorId.php
* @author :lyh
* @method :post
* @time :2025/5/26 15:57
*/
namespace App\Console\Commands\Ai;
use App\Helper\Arr;
use App\Models\Com\Notify;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainInfo;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use App\Models\Ai\AiBlogAuthor as AiBlogAuthorModel;
use App\Models\Project\Project;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class AiBlogAuthorId extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'save_ai_blog_author_id';
/**
* The console command description.
*
* @var string
*/
protected $description = '拉取对应作者的页面';
public $route = [];
public function handle(){
while (true){
//获取任务id
$task_id = $this->getTaskId();
if(empty($task_id)){
sleep(300);
continue;
}
$this->_action($task_id);
}
}
public function getTaskId()
{
$task_id = Redis::rpop('ai_blog_author_id');
if (empty($task_id)) {
$aiBlogTaskModel = new AiBlogTaskModel();
$ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING, 'type'=>$aiBlogTaskModel::TYPE_AUTHOR_ID])->pluck('id');
if(!empty($ids)){
foreach ($ids as $id) {
Redis::lpush('ai_blog_author_id', $id);
}
}
$task_id = Redis::rpop('ai_blog_author_id');
}
return $task_id;
}
/**
* @remark :执行方法
* @name :_action
* @author :lyh
* @method :post
* @time :2025/5/26 16:06
*/
public function _action($task_id){
$aiBlogTaskModel = new AiBlogTaskModel();
$item = $aiBlogTaskModel->read(['id'=>$task_id]);
if($item === false){
echo '当前数据不存在.'.$item['id'].PHP_EOL;
return true;
}
$aiBlogService = new AiBlogService($item['project_id']);
ProjectServer::useProject($item['project_id']);
$aiBlogService->author_id = $item['task_id'];
$result = $aiBlogService->getAuthorDetail();
if(isset($result['status']) && $result['status'] == 200){
//当前作者的页面
$aiBlogAuthorModel = new AiBlogAuthorModel();
$authorInfo = $aiBlogAuthorModel->read(['author_id'=>$item['task_id']],['id','route']);
if($authorInfo !== false && !empty($result['data']['section'])){
$this->route[] = $authorInfo['route'];
$aiBlogAuthorModel->edit(['text'=>$result['data']['section']],['author_id'=>$item['task_id']]);
}
}
DB::disconnect('custom_mysql');
$aiBlogTaskModel->edit(['status'=>2],['id'=>$task_id]);
$this->sendCPost($item['project_id']);
return true;
}
/**
* @remark :通知C端
* @name :sendCPost
* @author :lyh
* @method :post
* @time :2025/5/26 16:21
*/
public function sendCPost($project_id){
//获取项目所在服务器
$project_model = new Project();
$project_info = $project_model->read(['id'=>$project_id],['serve_id','is_upgrade', 'main_lang_id']);
if(!$project_info){
return false;
}
$serve_ip_model = new ServersIp();
$serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);
$servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;
if($servers_id == ServerConfig::SELF_SITE_ID){
//自建站服务器:如果项目已经上线,不请求C端接口,数据直接入库
$domain_model = new DomainInfo();
$domain_info = $domain_model->read(['project_id'=>$project_id],['domain']);
if($domain_info){
//判断是否已有更新进行中
$notify_model = new Notify();
$data = [
'project_id' => $project_id,
'type' => 1,
'route' => 3,
'server_id' => ServerConfig::SELF_SITE_ID,
'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]
];
$notify = $notify_model->read($data,['id']);
if(!$notify){
$domain = $domain_info['domain'];
$data['data'] = Arr::a2s(['domain'=>$domain,'url'=>$this->route,'language'=>[]]);
$data['status'] = Notify::STATUS_INIT;
$data['is_pull_html_zip'] = Notify::IS_PULL_HTML_ZIP_FALSE;
$data['sort'] = 1;
$notify_model->add($data);
}
}
}else{
$domainModel = new DomainInfo();
$domain = $domainModel->getProjectIdDomain($project_id);
$c_url = $domain.'api/update_page/';
$param = [
'project_id' => $project_id,
'type' => 1,
'route' => 3,
'url' => $this->route,
'language'=> [],
'is_sitemap' => 0
];
$res = http_post($c_url, json_encode($param,true));
echo 'notify: project id: ' . $project_id . ', result: ' . json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
}
}